[英]How to connect group means in a 2 x 2 factorial design in ggplot2 R?
我需要跨时间点连接 T 的均值; 和 M 跨时间点的平均值。 T 和 M 具有单独的均值和置信区间,如图所示。 我尝试使用 group 功能,但没有按预期工作。 另外,有没有办法在每个时间点躲避这两个群体?
ab <- rep(c("T","M"), time = 10)
time <- rep(c("J","F"), each = 5)
ab.val <- c(1:20)
df <- data.frame(time,ab,ab.val)
df$ab <- as.factor(df$ab)
df$time <- as.factor(df$time)
ggplot(aes(x= time , y = ab.val, color = ab, group = ab), data = df) +
geom_point() +
stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=0.2, colour="black", aes(group = ab)) +
stat_summary(fun = mean, color = "black", geom ="point", size = 3,show.legend = FALSE, aes(group = ab)) +
geom_line()
与您使用stat_summary
方式相同,这可用于按组添加行。 您可以通过从每个stat_summary
调用中删除group = ab
来简化您的代码,因为它是在ggplot(...aes(group = ab)
定义的,并且可以使用 position 参数来躲避组。
代码:
library(ggplot2)
library(Hmisc)# for mean_cl_boot function
ab <- rep(c("T","M"), time = 10)
time <- rep(c("J","F"), each = 5)
ab.val <-1:20
df <- data.frame(time,ab,ab.val)
df$ab <- as.factor(df$ab)
df$time <- as.factor(df$time)
ggplot(aes(x = time, y = ab.val, color = ab, group = ab), data = df) +
geom_point(position = position_dodge(0.25)) +
stat_summary(fun.data = mean_cl_boot, geom = "errorbar",
width = 0.2, colour = "black",
position = position_dodge(0.25)) +
stat_summary(fun = mean, color = "black",
geom = "point", size = 3,show.legend = FALSE,
position = position_dodge(0.25)) +
stat_summary(fun = mean,
geom = "line", show.legend = FALSE,
position = position_dodge(0.25))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.