意味着具有置信区间的条形图?

Means barplot with confidence intervals?

我有一个大数据集,其中我有一个变量 Q1,有 7 个 response/value 选项,还有两个组(OneTwo)。

 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)    

我正在尝试转换一个简单的频率图(按组划分的每个响应类别中的观察数),而是绘制具有置信区间的均值(如下图所示)。

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")

当我 运行 执行此操作时,出现以下错误:

Error: stat_summary requires the following missing aesthetics: y

欢迎任何想法!

这样的事情怎么样

`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) `

这是一个例子。您需要自己预先计算 CI:

library(dplyr)
library(ggplot2)

set.seed(123)

df <- data.frame(g = c(rep("A",10),rep("B",10),rep("C",10)),
           val = c(rnorm(10,100,5), rnorm(10,200,10), rnorm(10,300,50)))

df <- df %>% group_by(g) %>% summarise(m = mean(val),
                                       stdv = sd(val))

ggplot(df, aes(g,m,fill=g)) + 
  geom_bar(stat="identity", color="black", 
           position=position_dodge()) +
  geom_errorbar(aes(ymin=m-stdv, ymax=m+stdv), width=.2,
                position=position_dodge(.9)) 

输出

更新

df <- data.frame(
  Q1 = c(6,4,2,4,7,1,4,7,4,5,4,4,2,6,1),
  Group = sample(c("One","Two"), 15, TRUE),
  stringsAsFactors = FALSE)

df <- df %>% group_by(Group) %>% summarise(m = mean(Q1),
                                       stdv = sd(Q1))

ggplot(df, aes(Group,m,fill=Group)) + 
  geom_bar(stat="identity", color="black", 
           position=position_dodge()) +
  geom_errorbar(aes(ymin=m-stdv, ymax=m+stdv), width=.2,
                position=position_dodge(.9))