Search code examples
rggplot2bar-chartconfidence-intervalaesthetics

Means barplot with confidence intervals?


I have a large dataset, where I have a variable Q1with 7 response/value options, and two groups (One and Two).

 Q1<- c(6,4,2,4,7,1,4,7,4,5,4,4,2,6,1)
 Group<- c(One, Two, One, Two,Two, Two, One, One, One, One, Two, One, One, Two, Two)    

I'm trying to convert a simple frequency plot (number of observations in each response category by group) and instead plot the means with confidence intervals (as in the image below).

enter image description here

df1<- filter(df, Q1!="-99",df$Group=="One"|df$Group=="Two") 
ggplot(data = df1, aes(x = Q1)) +
geom_bar(aes(fill = df1$Group), position = "dodge", stat="summary", fun.y="mean") + labs(title="Graph Title")

When i run this, I get the following error:

Error: stat_summary requires the following missing aesthetics: y

Any ideas are appreciated!


Solution

  • what about something like this

    `ggplot(df.df, aes(x=category, color=group)) + 
      stat_summary(aes(y = value),
                 fun.y = mean, na.rm = TRUE,
                 geom = "bar",
                 size = 3) + 
    stat_summary(aes(y = value),
               fun.data = mean_se, na.rm = TRUE,
               geom = "errorbar",
               width = 0.2) `