Angus
Angus

Reputation: 355

Would like to plot mean of grouped data and add confidence limits

I have data for which I need to calculate and plot the mean and a 95% confidence limits, but I don't know where I'm going wrong with my code. The data is grouped by year and I thought about using stat_smooth() to draw a confidence interval region, but I just can't get it off the ground. If I try I get an error:

Error: geom_path: If you are using dotted or dashed lines, colour, size and linetype must be constant over the line

    library(ggplot2)
    library(dplyr) 
dat <- data.frame(cbind(rep(1:10,10),rnorm(100,600,sd=100),rnorm(100,400,sd=50),rnorm(100,700,sd=100)))
colnames(dat) <- c("year","S4","S5","S6")

    df<-data.frame(dat)
    ggplot(df)+
      geom_line(aes(x=year, y=mean(df$S4),colour=year)) +
      geom_line(aes(x=year, y=quantile(df$S4,0.95),colour=year),linetype="dotted") +
      geom_line(aes(x=year, y=quantile(df$S4,0.05),colour=year),linetype="dotted") 

Upvotes: 3

Views: 273

Answers (1)

Ben
Ben

Reputation: 30474

Could you use stat_summary:

ggplot(df, aes(x = year, y = S4)) +
  stat_summary(geom = "line", fun = mean, linetype = "solid") +
  stat_summary(geom = "ribbon", fun.data= mean_cl_normal, fun.args = list(conf.int=0.95), alpha=.1)

Plot

plot with mean and 95% CI

Edit:

With multiple variables/lines, would recommend putting data in "long" format first with pivot_longer, then plot. Perhaps could use different colors for different variables.

df <- dat %>%
  pivot_longer(cols = c(S4, S5, S6), names_to = "variable", values_to = "value")

ggplot(df, aes(x = year, y = value, color = variable)) +
  stat_summary(geom = "line", fun = mean, linetype = "solid") +
  stat_summary(geom = "ribbon", fun.data= mean_cl_normal, fun.args = list(conf.int=0.95), alpha=.1)

Upvotes: 6

Related Questions