R 将错误条添加到ggplot时出错

R 将错误条添加到ggplot时出错,r,ggplot2,bar-chart,errorbar,R,Ggplot2,Bar Chart,Errorbar,亲爱的Stackoverflow用户: 我想画一个有三个独立变量和误差条的分组条形图。我的图表基于堆叠溢出的示例(分组条中的堆叠条),使用带有geom_条的ggplot。当我根据帮助页面的示例添加geom_errorbar时,我得到以下错误: if(empty(data))中出错{:缺少需要TRUE/FALSE的值 这是我使用的脚本: treatment<-rep(c(rep(c(1),8),rep(c(2),8)),2) origin<-rep(c("A",&q

亲爱的Stackoverflow用户:

我想画一个有三个独立变量和误差条的分组条形图。我的图表基于堆叠溢出的示例(分组条中的堆叠条),使用带有geom_条的ggplot。当我根据帮助页面的示例添加geom_errorbar时,我得到以下错误:
if(empty(data))中出错{:缺少需要TRUE/FALSE的值

这是我使用的脚本:

treatment<-rep(c(rep(c(1),8),rep(c(2),8)),2)
origin<-rep(c("A","B"),16)
time<-c(rep(c(5),16),rep(c(10),16))
sulfide<-c(0,10,5,8,9,6,16,18,20,25,50,46,17,58,39,43,20,25,50,46,17,58,39,43,100,120,103,104,150,160,200,180)

Reed<-data.frame(treatment,origin,time,sulfide)

# specify factor types
Reed$treatment<-as.factor(Reed$treatment)
Reed$origin<-as.character(Reed$origin)
Reed$time<-as.factor(Reed$time)

library(ggplot2)
library(scales)

#draw plot
ggplot() +geom_bar(data=Reed, aes(y = sulfide, x = treatment, fill=origin), stat="identity",position="dodge") +theme_bw() + facet_grid( ~ time)+xlab("treatment") +ylab("Sulfide")+ggtitle("Time)")

treatment如果您想通过创建摘要数据集来构建错误栏,您只需要以正确的格式获取该数据集。有很多选项;我将使用dplyr。请注意,我将此数据集中绘图中的所有分组变量保持为“整洁”格式,每个变量位于单独的列中

library(dplyr)
meandat = Reed %>% 
    group_by(treatment, time, origin) %>%
    summarise(mean = mean(sulfide, na.rm = TRUE), se = SE(sulfide))

Source: local data frame [8 x 5]
Groups: treatment, time [?]

  treatment   time origin   mean        se
     (fctr) (fctr)  (chr)  (dbl)     (dbl)
1         1      5      A   7.50  3.378856
2         1      5      B  10.50  2.629956
3         1     10      A  31.50  7.858117
4         1     10      B  43.00  6.819091
5         2      5      A  31.50  7.858117
6         2      5      B  43.00  6.819091
7         2     10      A 138.25 23.552689
8         2     10      B 141.00 17.540429
现在可以通过
geom\u errorbar
添加错误条。您将看到我在
ggplot
中全局设置了美学,以避免自己必须重新键入其中一些错误条,但您可以根据需要进行更改。我使用
position\u dodge
将错误条正确放置在每个错误条上

ggplot(data = Reed, aes(y = sulfide, x = treatment, fill=origin)) +
    geom_bar(stat="identity", position="dodge") +
    theme_bw() + 
    facet_grid( ~ time)+
    xlab("treatment") +
    ylab("Sulfide")+
    ggtitle("Time")+ 
    geom_errorbar(data = meandat, aes(ymin = mean - se, ymax = mean + se, y = mean), 
                position = position_dodge(width = .9))

实际上,您可以通过
stat\u summary
来完成所有这一切,而不是“手工”计算汇总统计数据。例如。代码如下所示,并给出与上述相同的绘图

ggplot(data = Reed, aes(y = sulfide, x = treatment, fill=origin)) +
    geom_bar(stat="identity",position="dodge") +
    theme_bw() + 
    facet_grid( ~ time) +
    xlab("treatment") +
    ylab("Sulfide") +
    ggtitle("Time") + 
    stat_summary(geom = "errorbar", fun.data = mean_cl_normal, mult = 1, 
               position = position_dodge(width = .9))

我一直在使用ggplot2的开发版本,ggplot2_1.0.1.9003,发现我需要通过
fun.args
添加
stat_summary
函数参数。这看起来像
fun.args=list(mult=1)
获得1个标准误差的误差条。

暂时撇开误差条的问题不谈,你的绘图有一个更严重的问题。你有两个值,分别是
治疗
时间
,和
来源
,总共有8种组合,但是32个值是硫化物-因此e有4个值是硫化物ach组合。当您使用,例如

ggplot(data=Reed) +
  geom_bar(aes(y = sulfide, x = treatment, fill=origin), stat="identity",position="dodge") +
  facet_grid( ~ time)+xlab("treatment") +ylab("Sulfide")
您正在以相同的颜色绘制所有四个硫化物值的条形图。这只会显示最大值。很难相信这是您想要的,即使您这样做了,也有更好的方法。例如,如果您想绘制每个co的
硫化物
的平均值综合各种因素,你可以这样做

ggp <- ggplot(data=Reed, aes(y = sulfide, x = as.factor(treatment), group=origin)) +
  geom_bar(aes(fill=origin), stat="summary", fun.y=mean, position="dodge") +
  theme_bw() + 
  facet_grid( ~ time)+xlab("treatment") +ylab("Sulfide")+ggtitle("Time")
ggp

请注意,无需从外部聚合数据-
ggplot
会为您进行聚合

最后,这种方法有助于使用许多内置函数来生成具有更严格统计性的置信限

ggp+stat_summary(fun.data=mean_cl_normal, conf.int=0.95,
                 geom="errorbar",position=position_dodge(width=0.85), width=0.1)

因此,这里我们使用
ggplot
内置函数
mean\u cl\u normal
计算平均值的95%置信限,假设数据遵循正态分布(因此,平均值将遵循t分布)。我们使用参数
conf.int=…
来指定所需的置信区间,但默认值为0.95,因此在本例中确实不需要


此类型还有其他几种功能:请参阅和其中的链接以获取解释。

非常感谢,你们两位!@jlhoward
se <- function(y) sd(y)/length(y)   # to calculate standard error in the mean
ggp+stat_summary(geom="errorbar",position=position_dodge(width=0.85),
                 fun.data=function(y)c(ymin=mean(y)-se(y),ymax=mean(y)+se(y)), width=0.1)
ggp+stat_summary(fun.data=mean_cl_normal, conf.int=0.95,
                 geom="errorbar",position=position_dodge(width=0.85), width=0.1)