【问题标题】:How to plot multiple group means and the confidence intervals in ggplot2 (R)?如何在 ggplot2 (R) 中绘制多个组均值和置信区间?
【发布时间】:2017-04-23 04:49:12
【问题描述】:

我的数据如下所示:

A  B  C
8  5  2
9  3  1
1  2  3
3  1  2
4  3  1

我需要使用 ggplot2 绘制每一个的均值以及置信区间。我还想从数据本身导出置信区间(例如,使用 stat_summary(fun.data = mean_cl),但是我不确定如何绘制这种格式的数据的均值。

我尝试了以下代码,但它没有运行。我不确定第 2 行中的 y 需要输入什么内容。

pd <- position_dodge(0.78)
ggplot(dat, y = c(dat$A,dat$B,dat$C) + ylim(0,10) + theme_bw()) + 
  stat_summary(geom="bar", fun.y=mean, position = "dodge") + 
  stat_summary(geom="errorbar", fun.data=mean_cl_normal, position = pd)

我收到以下错误:

Warning messages:
1: Computation failed in `stat_summary()`:
object 'x' not found 
2: Computation failed in `stat_summary()`:
object 'x' not found

【问题讨论】:

  • 我已经更新了答案
  • 您似乎在 ggplot() 中遗漏了一个 ) .. 还请通过消除所有主题等使这个成为一个最小的可重现示例。另外,您使用的是什么版本的 ggplot2?
  • @Elin:我已经进行了更新。我正在使用 ggplot2 2.1.0
  • fun.data = "mean_cl_boot" or "mean_cl_normal", "mean_cl" 不是 ggplot 识别的函数,你必须引用它
  • @NathanDay 这两个都不起作用。我得到同样的错误

标签: r plot ggplot2 mean confidence-interval


【解决方案1】:

您的数据不是长格式,这意味着它应该如下所示:

thing<-data.frame(Group=factor(rep(c("A","B","C"),5)),
                  Y = c(8,9,1,3,4, 
                        5,3,2,1,3,
                        2,1,3,2,1)
                  )

您可以使用melt() 之类的函数来帮助获取在reshape2 包中格式化的数据。

一旦有了这些,您还必须计算数据的均值和 SE(在 ggplot 之前手动或通过 ggplot 中的 stat_summary 中的正确表达式)。您可能从示例中复制/粘贴,因为您正在使用的函数(例如,mean_cl_normal)可能未定义。

那就亲手做吧。

library(plyr)

cdata <- ddply(thing, "Group", summarise,
               N    = length(Y),
               mean = mean(Y),
               sd   = sd(Y),
               se   = sd / sqrt(N)
)
cdata

#Group N mean       sd       se
#1     A 5  4.0 2.236068 1.000000
#2     B 5  3.8 3.033150 1.356466
#3     C 5  1.8 1.788854 0.800000

现在您可以使用ggplot

pd <- position_dodge(0.78)

ggplot(cdata, aes(x=Group, y = mean, group = Group)) +
   #draws the means
      geom_point(position=pd) +
   #draws the CI error bars
      geom_errorbar(data=cdata, aes(ymin=mean-2*se, ymax=mean+2*se, 
      color=Group), width=.1, position=pd)

这给出了附加的情节。

【讨论】:

    【解决方案2】:

    就像大卫说的,你首先需要长格式,但你应该可以使用fun.data = "mean_cl_normal" 或者像这样插入各种其他的就好了:

    library(tidyr); library(ggplot2)
    dat <- gather(dat) # gather to long form
    
    ggplot(data = dat, aes(x = key, y = value)) +
        geom_point(size = 4, alpha = .5) + # always plot the raw data
        stat_summary(fun.data = "mean_cl_normal", geom = "crossbar") +
        labs(title = "95% Mean Confidence Intervals")
    

    如果您想手动构建相同的间隔,您只需要lmconfint 即可获得您所追求的信息:

    mod <- lm(value ~ 0 + key, data = dat)
    ci <- confint(mod)
    

    【讨论】:

    • 非常好的解决方案内森!请注意mean_cl_normal 中的参数如何必须匹配。错误“找不到对象'x'”意味着没有变量“x”供ggplot评估。上面 Nathan 的代码显示 ggplot 是 x = keyy = value,因此它可以在 stat_summary 中评估它。
    • 感谢 David,stat_summary 的默认 fun.data 是“mean_se”,因此如果您想在没有 ddply 的情况下复制您的示例,您可以调用 stat_summary(group = something appropriate, geom = "errorbar")。非常适合快速可视化
    猜你喜欢
    • 1970-01-01
    • 2017-04-25
    • 1970-01-01
    • 1970-01-01
    • 2019-01-18
    • 2020-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多