简体   繁体   中英

create a line graph using ggplot2 of the change in mean overtime with 95% CI

My dataset is something like this

df <- data.frame(time = c(0,0,1,1,2,2),
                 mean = c(8, 6, 7 , 6, 6, 5), 
                 group = c(1,0,1,0,1,0), 
                 lower = c(7, 5, 5, 4, 4, 4), 
                 upper = c(12, 9, 10, 8, 8, 8))

I want to create a plot of the changes in mean at the 3-time points by group, each of which has the corresponding 95% CI (lower, upper), something like this, y-axis is mean, and x-axis is time

在此处输入图像描述

Perhaps like this?

ggplot(df, aes(x = time, y = mean, group = group)) +
  geom_line(aes(lty = as.character(group)),
            position = position_dodge(width = 0.1)) +
  geom_errorbar(aes(ymax = upper, ymin = lower), width = 0.1,
                position = position_dodge(width = 0.1)) +
  geom_point(aes(shape = as.character(group)),
             position = position_dodge(width = 0.1)) +
  guides(lty = "none", shape = "none")

在此处输入图像描述

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM