是指具有置信区间的条形图?

时间:2019-10-31 20:33:51

标签: r ggplot2 bar-chart confidence-interval aesthetics

我有一个很大的数据集,其中有一个变量Q1,具有7个响应/值选项以及两个组(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)    

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

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

运行此命令时,出现以下错误:

Error: stat_summary requires the following missing aesthetics: y

任何想法都值得赞赏!

2 个答案:

答案 0 :(得分:1)

这样的事情

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

答案 1 :(得分:0)

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

moment.min.js:1 Deprecation warning: value provided is not in a recognized RFC2822 or ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions. Non RFC2822/ISO date formats are discouraged and will be removed in an upcoming major release. Please refer to http://momentjs.com/guides/#/warnings/js-date/ for more info.
Arguments: 
[0] _isAMomentObject: true, _isUTC: false, _useUTC: false, _l: en, _i: T17:30:00, _f: null, _strict: false, _locale: [object Object]
Error
    at Function.createFromInputFallback (https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js:1:3368)
    at Yt (https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js:1:21353)
    at Ot (https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js:1:22064)
    at Tt (https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js:1:22146)
    at c (https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js:1:209)
    at k.r.getMoment (https://cdnjs.cloudflare.com/ajax/libs/tempusdominus-bootstrap-4/5.1.2/js/tempusdominus-bootstrap-4.min.js:6:14482)
    at k.r._parseInputDate (https://cdnjs.cloudflare.com/ajax/libs/tempusdominus-bootstrap-4/5.1.2/js/tempusdominus-bootstrap-4.min.js:6:12293)
    at k.r.date (https://cdnjs.cloudflare.com/ajax/libs/tempusdominus-bootstrap-4/5.1.2/js/tempusdominus-bootstrap-4.min.js:6:15573)
    at String.<anonymous> (https://cdnjs.cloudflare.com/ajax/libs/tempusdominus-bootstrap-4/5.1.2/js/tempusdominus-bootstrap-4.min.js:6:15189)
    at Function.each (https://code.jquery.com/jquery-3.3.1.min.js:2:2623)

输出

Output

更新

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