R 数据可视化 —— ggplot 误差图和边际线
2021-04-16 本文已影响0人
名本无名
误差图
设置垂直间隔的方式有很多,每种方式对应于一种图形,包括:
geom_crossbar
geom_errorbar
geom_linerange
geom_pointrange
示例
对于如下数据
df <- data.frame(
trt = factor(c(1, 1, 2, 2)),
resp = c(1, 5, 3, 4),
group = factor(c(1, 2, 1, 2)),
upper = c(1.1, 5.3, 3.3, 4.2),
lower = c(0.8, 4.6, 2.4, 3.6)
)
> df
trt resp group upper lower
1 1 1 1 1.1 0.8
2 1 5 2 5.3 4.6
3 2 3 1 3.3 2.4
4 2 4 2 4.2 3.6
绘制范围线条
p <- ggplot(df, aes(trt, resp, colour = group))
p + geom_linerange(aes(ymin = lower, ymax = upper))

设置点范围
p + geom_pointrange(aes(ymin = lower, ymax = upper))

设置矩形范围
p + geom_crossbar(aes(ymin = lower, ymax = upper), width = 0.2)

误差线,我们之前已经画过了
p + geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.1)

上面这些例子都是竖直方向上的,要绘制水平方向,只要将轴翻转一下即可
p + geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.1) +
coord_flip()

为分组添加连接线
p + geom_line(aes(group = group)) +
geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.1)

为条形图添加误差线
ggplot(df, aes(trt, resp, fill = group)) +
geom_col(position = "dodge2") +
geom_errorbar(
aes(ymin = lower, ymax = upper),
position = position_dodge2(width = 0.2, padding = 0.8)
) +
coord_flip()

边际分布
边际分布图用于在 2D
图形中,绘制数据在 X
和 Y
轴的映射,适用于较小的数据集
默认情况下,线条长度为整个图形的 3%
,而数据绘制区域距离两端的间隙为 5%
。因此,默认情况下边际分布不会和数据重叠
示例
例如,对于如下分布
p <- ggplot(mtcars, aes(wt, mpg)) +
geom_point()
p

我们可以添加数据的边际分布
p + geom_rug()

能够很容易的观察出数据在某一维度的分布情况
我们也可以只绘制一个维度
p + geom_rug(sides = 'l')

通过设置 sides
参数,该参数接受 t(top)
、r(right)
b(bottom)
l(left)
的任意组合
从上面的图可以看出,许多点映射到坐标轴后是重叠在一起的,我们可以设置 position
参数,尽量避免重叠情况
ggplot(mpg, aes(displ, cty)) +
geom_jitter() +
geom_rug(alpha = 1/2, position = "jitter")

让线条朝外
p + geom_rug(outside = TRUE) +
coord_cartesian(clip = "off")

与轴标签重叠了,将它们放到上面和右边去。同时,记得设置一下边距,不然线条会被遮盖
p + geom_rug(outside = TRUE, sides = 'tr') +
coord_cartesian(clip = "off") +
theme(plot.margin = margin(1, 1, 1, 1, "cm"))

如果要设置线条的长度,记得同时修改数据区域与轴线的间距,避免与数据点重叠
p + geom_rug(length = unit(0.05, "npc")) +
scale_y_continuous(expand = c(0.1, 0.1))

设置颜色与线条类型与大小
ggplot(mtcars, aes(wt, mpg)) +
geom_point() +
geom_rug(sides = 'l', colour = "#a65628", linetype = 2, size = 2) +
geom_rug(sides = 'b', colour = "#377eb8", linetype = 5, size = 2.5)
