箱线图
箱线图用于展示 5
个统计量:最大值、最小值、中位数、第一分位数和第三分位数。
从箱线图中可以很容易的看出数据是否对称分布、以及是否包含离散数据,分布的离散程度。也可以用于比较不同变量的分布
示例
来个最简单的箱线图
p <- ggplot(mpg, aes(class, hwy))
p + geom_boxplot()

翻转方向
ggplot(mpg, aes(hwy, class)) + geom_boxplot()

设置凹槽
p + geom_boxplot(notch = TRUE)

凹槽的宽度可以通过 notchwidth
参数设置,默认为 0.5
p <- ggplot(mpg, aes(class, hwy))
p + geom_boxplot(notch = TRUE, notchwidth = 0.9)

默认情况下,每个箱子的宽度是一样的,我们可以设置 varwidth = TRUE
,使得宽度与组内观测值的平方根成正比
p + geom_boxplot(varwidth = TRUE)

为箱子设置颜色
p + geom_boxplot(fill = "white", colour = "#3366FF")

geom_boxplot
函数中有专门的几个参数用于设置离散值的属性:
-
outlier.colour = NULL
, -
outlier.color = NULL
, -
outlier.fill = NULL
, -
outlier.shape = 19
, -
outlier.size = 1.5
, -
outlier.stroke = 0.5
, -
outlier.alpha = NULL
,
例如
p + geom_boxplot(outlier.colour = "red", outlier.shape = 1)

设置透明度
p + geom_boxplot(outlier.fill = "blue", outlier.shape = 21, alpha = 0.5)

但是,当我们想要组合绘制箱线图和散点图时,可能需要将离散点删除
p + geom_boxplot(outlier.shape = NA) + geom_jitter(width = 0.2)

当我们绘制分组箱线图时,默认以并列的方式排列
p + geom_boxplot(aes(colour = drv))

而对于连续型 x
变量,需要指定分组。可以搭配 cut_width
使用
ggplot(diamonds, aes(carat, price)) +
geom_boxplot(aes(group = cut_width(carat, 0.5)))

如果数据中已经计算过这些统计量,那么也可以将这些变量传递进去。
例如
tibble(
x = rep(LETTERS[1:10], 10),
y = rnorm(100)
) %>% group_by(x) %>%
summarise(y0 = min(y), y25 = quantile(y, 0.25), y50 = median(y),
y75 = quantile(y, 0.75), y100 = max(y)) %>%
ggplot(aes(x)) +
geom_boxplot(
aes(ymin = y0, lower = y25, middle = y50, upper = y75, ymax = y100,
fill = x),
stat = "identity"
)

组合图形
- 添加均值标记点
ggplot(mpg, aes(class, hwy)) +
geom_boxplot(aes(fill = class)) +
stat_summary(fun = "mean", fill = "white", size = 2, geom = "point", shape = 23)

- 再添加误差线
ggplot(mpg, aes(class, hwy)) +
stat_boxplot(geom = "errorbar", width = 0.2) +
geom_boxplot(aes(fill = class)) +
stat_summary(fun = "mean", fill = "white",
size = 2, geom = "point", shape = 23)

- 为分组箱线图添加误差线
ggplot(mpg, aes(class, hwy)) +
stat_boxplot(aes(colour = drv), geom = "errorbar",
position = position_dodge2(preserve = 'single', padding = 0.5)) +
geom_boxplot(aes(fill = drv), position = position_dodge2(preserve = 'single'))

注意:由于每种类型的分组并不是都存在,所以会出现箱线图宽度不一致的情况,所以设置了 preserve = 'single'
同时,在添加分组误差线时,需要指定分组 colour = drv
。
- 定制箱线图
# 先绘制一个虚线箱线图
p1 <- ggplot(mpg, aes(class, hwy)) +
geom_boxplot(linetype = 'dashed', outlier.colour = "red")
# 再绘制带颜色的中心矩形,覆盖原来的矩形
p2 <- p1 +
stat_boxplot(aes(ymin = after_stat(lower), ymax = after_stat(upper),
fill = class))
# 设置上误差线,误差线的最小值设置为数据最大值
p3 <- p2 +
stat_boxplot(aes(ymin = after_stat(ymax)), geom = "errorbar",
width = 0.2, colour = "#4daf4a")
# 设置下误差线
p3 +
stat_boxplot(aes(ymax = after_stat(ymin)), geom = "errorbar",
width = 0.2, colour = "#377eb8")
plot_grid(p1, p2, p3, p4)

小提琴图
小提琴图用于显示数据的分布状态和概率密度,它同时具有箱线图和密度图的特征,用于显示数据的分布形状。
示例
来个最简单的例子
p <- ggplot(mtcars, aes(factor(cyl), mpg))
p + geom_violin()

更改方向
ggplot(mtcars, aes(mpg, factor(cyl))) +
geom_violin()

可以通过设置 scale
参数的值来更改图像大小,支持三个参数值:
-
area
:默认,保持所有图形大小一样 -
count
:设置最大宽度与样本大小成正比 -
width
:所有图形的最大宽度一样
p + geom_violin(scale = "count")

默认情况下,会删除图形的尾部数据,如果不想删除可以设置 trim = FALSE
p + geom_violin(trim = FALSE)

设置更小的 bandwidth(adjust)
来绘制更近似的拟合,默认为 1
p + geom_violin(adjust = .5)

分组小提琴图也是并列的方式排列
p <- ggplot(mtcars, aes(factor(cyl), mpg))
p1 <- p + geom_violin(aes(fill = cyl))
p2 <- p + geom_violin(aes(fill = factor(cyl)))
p3 <- p + geom_violin(aes(fill = factor(vs)))
p4 <- p + geom_violin(aes(fill = factor(am)))
plot_grid(p1, p2, p3, p4)

显示分位数线
p + geom_violin(draw_quantiles = c(0.25, 0.5, 0.75))

组合图形
- 添加中值点
ggplot(mpg, aes(class, displ)) +
geom_violin(aes(fill = class), show.legend = FALSE) +
stat_summary(fun = median, geom = "point", shape = 23,
size = 2, fill = "white")

- 添加均值和标准差
ggplot(mpg, aes(class, displ)) +
geom_violin(aes(fill = class), show.legend = FALSE) +
stat_summary(fun.data = "mean_sdl", fun.args = list(mult = 1),
colour = "white")

注意:如果在运行上述代码报错了
Hmisc package required for this function
需要安装一下 Hmisc
,因为 mean_sdl
函数来自 Hmisc
install.packages("Hmisc")
- 添加箱线图
ggplot(mpg, aes(class, displ)) +
geom_violin(aes(fill = class), show.legend = FALSE) +
geom_boxplot(width = 0.1)

- 添加抖动散点图
ggplot(mpg, aes(class, displ)) +
geom_violin(aes(fill = class), show.legend = FALSE) +
geom_jitter(width = 0.1)

- 转换为极坐标
ggplot(mpg, aes(class, displ)) +
geom_violin(aes(fill = class), show.legend = FALSE) +
geom_boxplot(width = 0.1) +
coord_polar()

网友评论