如何在 ggplot2 R 的 2 x 2 因子设计中连接组均值?
How to connect group means in a 2 x 2 factorial design in ggplot2 R?
我需要跨时间点连接T的手段;和 M 跨时间点的意思。 T 和 M 具有单独的均值和置信区间,如图所示。我尝试使用组功能,但没有按预期工作。还有就是每个时间点都有闪避两组的方法吗?
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)
中定义,并且可以使用位置参数来躲避组。
代码:
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))
我需要跨时间点连接T的手段;和 M 跨时间点的意思。 T 和 M 具有单独的均值和置信区间,如图所示。我尝试使用组功能,但没有按预期工作。还有就是每个时间点都有闪避两组的方法吗?
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)
中定义,并且可以使用位置参数来躲避组。
代码:
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))