Search code examples
rggplot2combinedchart

ggplot2 to plot mean and sd of a variable side by side


I am trying to create a plot of the mean and sd (side by side) of a variable for two different groups in R to get something like this.

enter image description here

where blue bars are means and orange bars are SDs.

I use the ggplot2 package in R for this. If I use these codes separately

ggplot(data, aes(x=factor(grouping variable), y=my variable)) + stat_summary(fun.y="mean", geom="bar", col="blue")

ggplot(data, aes(x=factor(grouping variable), y=my variable)) + stat_summary(fun.y="sd", geom="bar", col="orange")

they function well but produce the mean and sd in two different graphs.

So I tried to combine them in one graph by using

stat = "summary", fun.y = "mean" and stat = "summary", fun.y = "sd"

and what I got

ggplot(data, aes(x=factor(grouping variable)) + geom_bar(aes(y=my variable), stat = "summary", fun.y = "mean", position="dodge",col="blue") + geom_bar(aes(y=my variable), stat = "summary", fun.y = "sd", position="dodge",col="orange")

and the following error has emerged

Error: unexpected symbol in:
"ggplot(data, aes(x=factor(grouping variable)) + geom_bar(aes(y=my variable), stat = "summary", fun.y = "mean", position="dodge",col="blue") + geom_bar(aes(y=my variable), stat = "summary", fun.y = "sd", positi ggplot"

Could you help to fix the error or maybe there is another way to do this?

Updated information: the sample of my data looks like enter image description here

I run the following code on these data to plot mean tTTO and sd tTTO for both interviewers:

ggplot(timeTTO, aes(x=interviewer, y=tTTO)) + 
  theme_light() + 
  labs(title = "Figure 3. Time taken to complete a single TTO task, by interviewer", x=NULL, y=NULL) + 
  theme(plot.title = element_text(face = "bold")) + 
  geom_bar(stat = "summary", fun.y = "mean",width=0.25, fill = "blue") + 
  geom_bar(stat = "summary", fun.y = "sd", width=0.25,fill = "orange") 

and I got something like this where blue bars are the means and orange bars are SDs: enter image description here

Actually, I have tried with position="dodge" put it in both geom_bar() functions, it did not work


Solution

  • It seems position="dodge" is for geom's of the same x, but not for stat's. I came up with two solutions.

    In the first, I kept your stat_summary's and used position_nudge to manually put the bars in your specified positions. Notice how the legend doesn't work either because there is no actual plot data, just stat layers.

    In the second, I did the data analysis before ggplot, using group_by, summarize, and then gather to make the data long. Then we can use the regular geom_col now that the data has already been processed.

    library(tidyverse)
    tibble(interviewer = c("i2", "i1", "i1", "i2", "i1"), tTTO = c(245, 251, 99, 85, 101)) %>%
      ggplot(aes(x=interviewer, y=tTTO)) + 
      theme_light() + 
      labs(title = "Figure 3. Time taken to complete a single TTO task, by interviewer", x=NULL, y=NULL) + 
      theme(plot.title = element_text(face = "bold"), legend.position = "bottom") + 
      geom_bar(stat = "summary", fun.y = "mean", position = position_nudge(x = -0.125, y = 0), width = 0.25, fill = "blue") + 
      geom_bar(stat = "summary", fun.y = "sd", position = position_nudge(x = 0.125, y = 0), width = 0.25, fill = "orange")
    

      # Notice that the legend does not work for stat geoms
    
    tibble(interviewer = c("i2", "i1", "i1", "i2", "i1"), tTTO = c(245, 251, 99, 85, 101)) %>%
      group_by(interviewer) %>%
      summarize(mean(tTTO), sd(tTTO)) %>%
      gather(key = "type", value = "value", 2:3) %>%
      ggplot(aes(x=interviewer, y=value, fill=type)) + 
      theme_light() + 
      labs(title = "Figure 3. Time taken to complete a single TTO task, by interviewer", x=NULL, y=NULL) + 
      theme(plot.title = element_text(face = "bold"), legend.position = "bottom") + 
      geom_col(position = "dodge", width = 0.25) +
      scale_fill_manual(values = c("blue","orange"))
    

    Created on 2019-03-04 by the reprex package (v0.2.1)