I need to connect the means for T across time points; and means for M across time points. The T and M have seperate mean and confidence intervals like shown. I tried using the group function but it didn't work as expected. Also, is there anyway to dodge the two groups for each time point?
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()
In the same way that you have used stat_summary
this can be used to add lines by group. Your code can be simplified by removing group = ab
from each stat_summary
call as it is defined in the ggplot(...aes(group = ab)
and use of the position argument can be used to dodge groups.
CODE:
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))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.