R语言可视化 ggplot2—几何对象
对于一维的分布,几何对象的选择是由变量的类型指定的:
重量与价格的散点图中加入了平滑曲线。左图为dsmall数据集,右图为完整数据集。
qplot(carat, price, data = dsmall, geom = c("point", "smooth"))
qplot(carat, price, data = diamonds, geom = c("point", "smooth"))
左图是 span=0.2,右图是 span=1
qplot(carat, price, data = dsmall,
geom = c("point", "smooth"), span = 0.2)
qplot(carat, price, data = dsmall,
geom = c("point", "smooth"), span = 1)
library(mgcv)
qplot(carat, price, data = dsmall,
geom = c("point", "smooth"), method = "gam",
formula = y ~ s(x))
qplot(carat, price, data = dsmall,
geom = c("point", "smooth"), method = "gam",
formula = y ~ s(x, bs = "cs"))
数据量超过1000时使用第二个
左图是formula=y~s(x),右图是formula=y~s(x,bs='cs')。
library(splines)
qplot(carat, price, data = dsmall, geom = c("point", "smooth"),
method = "lm")
qplot(carat, price, data = dsmall, geom = c("point", "smooth"),
method = "lm", formula = y ~ ns(x, 5))
qplot(color, price/carat, data = diamonds, geom = "jitter")
qplot(color, price/carat, data = diamonds, geom = "boxplot")
qplot(color, price/carat, data = diamonds,
geom = "jitter", alpha = I(1/5))
qplot(color, price/carat, data = diamonds,
geom = "jitter", alpha = I(1/50))
qplot(color, price/carat, data = diamonds,
geom = "jitter", alpha = I(1/200))
展示钻石重量的分布。左图使用的是 geom='histogram',右图使用的是 geom=' density'。
qplot(carat, data = diamonds, geom = "histogram")
qplot(carat, data = diamonds, geom = "density")
qplot(carat, data = diamonds, geom = "histogram",
binwidth = 1, xlim = c(0, 3))
qplot(carat, data = diamonds, geom = "histogram",
binwidth = 0.1, xlim = c(0, 3))
qplot(carat, data = diamonds, geom = "histogram",
binwidth = 0.01, xlim = c(0, 3))
qplot(carat, data = diamonds, geom = "density", colour = color)
qplot(carat, data = diamonds, geom = "histogram", fill = color)
qplot(color, data = diamonds, geom = "bar")
qplot(color, data = diamonds, geom = "bar", weight = carat)
+ scale_y_continuous("carat")
线条图和路径图常用于可视化时间序列数据。线条图将点从左到右进行连接,而路径图则按照点在数据集中的顺序对其进行连接(线条图就等价于将数据按照x取值进行排序,然后绘制路径图)。线条图的x轴一般是时间,它展示了单个变量随时间变化的情况。路径图则展示了两个变量随时间联动的情况,时间反映在点的顺序上。
qplot(date, unemploy/pop, data = economics, geom = "line")
qplot(date, uempmed, data = economics, geom = "line")
year <- function(x) as.POSIXlt(x)$year + 1900
qplot(unemploy/pop, uempmed, data = economics,
geom = c("point", "path"))
qplot(unemploy/pop, uempmed, data = economics,
geom = "path", colour = year(date))