如何在具有多个组的箱线图顶部创建单独的线
Posted
技术标签:
【中文标题】如何在具有多个组的箱线图顶部创建单独的线【英文标题】:How to create individual lines on top of a boxplot with multiple groups 【发布时间】:2020-01-07 00:46:59 【问题描述】:在我的研究中,重要的是要展示每个人如何适应训练,而不仅仅是群体平均值和中位数的变化。 作为 R 的初学者,我很高兴我目前的箱线图有 3 个组,我通过 geom_point 添加了单个点,但我似乎无法让 geom_line 连接每个组内的点之间的线。 高度赞赏所有帮助。
我尝试关注类似的帖子建议,但它没有回复我的数据,Connect ggplot boxplots using lines and multiple factor
我不知道我是否应该将我的 data.frame 粘贴到这里 基本上第 1 列是哪个“组”(Heavy、Optimal、Control),第 2 列“Time_point”是其前后测量值(F0_pre、F0_post)和第 3 列“F0”是值
ggplot(Studydata, aes(Group,F0,fill = Time_point)) +
geom_boxplot() +
stat_summary(fun.y = mean, geom = "point", size=3, shape=23,
position = position_dodge(width = .75)) +
geom_point(position=position_dodge(width=0.75),aes(group=Time_point)) +
scale_y_continuous("F0 (N/kg)",limits=c(5,10),breaks=c(5,6,7,8,9,10),
expand = c(0,0)) +
theme(axis.line = element_line(color = "black",size = 1, linetype = "solid"))+
theme_classic() +
scale_fill_manual(values=c("#999999", "#FFFFFF"), name = "Time point", labels = c("Pre", "Post"))
structure(list(Group = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L), .Label = c("Control", "Heavy", "Optimal"), class = "factor"),
Time_point = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L), .Label = c("F0_pre", "F0_post"), class = "factor"),
F0 = c(7.30353192, 7.16108594, 7.662873671, 7.319494415,
7.690339929, 6.640005807, 6.848095385, 6.1605622, 8.300462597,
6.906034443, 7.644367174, 7.021959506, 7.042100127, 7.375865657,
8.506645287, 6.373721759, 7.507468154, 7.057438325, 7.147624225,
7.958957761, 7.439431197, 7.974165294, 8.125949745, 6.532471264,
7.481686188, 7.542614257, 7.247552687, 6.91, 7.609185039,
7.809989766, 8.151059576, 7.847938658, 7.999819081, 7.935556724,
7.679970645, 6.761378005, 8.157705923, 7.545437794, 9.395395275,
7.455579962, 7.917317173, 7.465252201, 8.567501942, 7.786701877,
7.4971379, 7.649121924, 6.942119866, 7.466501673, 7.653161086,
8.220328678, 8.173918564, 7.431310356, 7.98999627, 7.529664586,
7.518519833, 6.905140493)), row.names = c(NA, -56L), class = "data.frame")
【问题讨论】:
请使用dput
提供一些数据以供使用,或者添加行(代码)与geom_line
不起作用。您可能必须将正确的变量添加为group=
esthetic
Combine geom_boxplot with geom_line的可能重复
@TobiO 我现在添加了 dput 信息,非常感谢您指出这一点!
@Jimbou 我试过这个命令,但它所做的只是在每组箱线图之间画一条垂直线
【参考方案1】:
您需要在数据框中有一个变量来指示每个人的观察结果(这样您就可以将每个人的F0_pre
和F0_post
关联起来)。我假设它们在两个时间点的顺序相同,所以我们添加列:
Studydata$id <- rep(1:28, 2)
下一步:由于您的 x 轴是组,因此每个组的每个箱线图都在完全相同的位置(您看起来它们并排,因为它在内部使用 position("dodge")
)。既然我们想用这个变量来连接线,我们就用它作为x轴,也把它转换成数值,用geom_line()
和因子变量是很痛苦的:
Studydata$Time_point <- as.numeric(as.factor(Studydata$Time_point)) - 1
现在您的专栏有 0
而不是 "F0_pre"
和 1
而不是 "F0_pre"
。构建情节:
ggplot(Studydata, aes(x = Time_point, y = F0)) +
geom_boxplot(aes(fill = factor(Time_point))) +
facet_grid(~Group) +
stat_summary(aes(group = 1), fun.y = mean, geom = "point", size=3, shape=23,
position = position_dodge(width = .75)) +
geom_point(alpha = 0.5) +
scale_y_continuous("F0 (N/kg)",limits=c(5,10),breaks=c(5,6,7,8,9,10),
expand = c(0,0)) +
scale_x_continuous("F0 (N/kg)",limits=c(-0.5,1.5),breaks=c(0,1)) +
theme(axis.line = element_line(color = "black",size = 1, linetype = "solid"))+
theme_classic() +
scale_fill_manual(values=c("#999999", "#FFFFFF"), name = "Time point", labels = c("Pre", "Post")) +
geom_line(aes(group = factor(id)), color = "green")
结果:
一些注意事项:
如果你有线条,你真的需要添加点吗?点使图形变得杂乱,也使得很难区分箱线图中哪些点被认为是异常值(我尝试通过使用小的alpha = 0.5
来解决这个问题,这使得非异常点更加透明),而线条可以显示相同信息。
我再次使用绿线来区分这些线和箱线图生成的线。我强烈推荐它们有不同的颜色/类型。
【讨论】:
以上是关于如何在具有多个组的箱线图顶部创建单独的线的主要内容,如果未能解决你的问题,请参考以下文章