如何在ggplot2中将geom_errorbar与镶嵌面_包裹一起使用

如何在ggplot2中将geom_errorbar与镶嵌面_包裹一起使用,r,ggplot2,facet-wrap,errorbar,R,Ggplot2,Facet Wrap,Errorbar,我在向绘图中添加错误条时遇到问题。我有这样一个数据框: > str(bank1) 'data.frame': 24 obs. of 4 variables: $ site : Factor w/ 12 levels "BED","BEU","EB",..: 8 9 10 3 11 1 6 7 5 4 ... $ canopy : Factor w/ 3 levels "M_Closed","M_Open",..: 3 3 3 3 2 2 2 2 1 1 ... $ va

我在向绘图中添加错误条时遇到问题。我有这样一个数据框:

> str(bank1)
'data.frame':   24 obs. of  4 variables:
 $ site    : Factor w/ 12 levels "BED","BEU","EB",..: 8 9 10 3 11 1 6 7 5 4 ...
 $ canopy  : Factor w/ 3 levels "M_Closed","M_Open",..: 3 3 3 3 2 2 2 2 1 1 ...
 $ variable: Factor w/ 2 levels "depth5","depth10": 1 1 1 1 1 1 1 1 1 1 ...
 $ value   : int  200 319 103 437 33 51 165 38 26 29 ...
gs1 <- ggplot(bank1, aes(x = canopy, y= value , fill = variable)) + 
  geom_bar(stat='identity', position = 'dodge', fill = 'darkgray')+
  xlab("Canopy cover")+ylab("Seed Bank")+ 
  facet_wrap(~variable,nrow=1)
gs1  
> head(cleandata)
# A tibble: 6 x 6
# Groups:   canopy [3]
  canopy   variable mean.value sd.value count se.mean
  <fct>    <fct>         <dbl>    <dbl> <int>   <dbl>
1 Open     depth5        265.    145.       4   72.4 
2 Open     depth10        20.5    12.8      4    6.41
3 M_Open   depth5         71.8    62.6      4   31.3 
4 M_Open   depth10         6.5     4.20     4    2.10
5 M_Closed depth5         20       8.98     4    4.49
6 M_Closed depth10         0.5     1        4    0.5   
我这样画:

> str(bank1)
'data.frame':   24 obs. of  4 variables:
 $ site    : Factor w/ 12 levels "BED","BEU","EB",..: 8 9 10 3 11 1 6 7 5 4 ...
 $ canopy  : Factor w/ 3 levels "M_Closed","M_Open",..: 3 3 3 3 2 2 2 2 1 1 ...
 $ variable: Factor w/ 2 levels "depth5","depth10": 1 1 1 1 1 1 1 1 1 1 ...
 $ value   : int  200 319 103 437 33 51 165 38 26 29 ...
gs1 <- ggplot(bank1, aes(x = canopy, y= value , fill = variable)) + 
  geom_bar(stat='identity', position = 'dodge', fill = 'darkgray')+
  xlab("Canopy cover")+ylab("Seed Bank")+ 
  facet_wrap(~variable,nrow=1)
gs1  
> head(cleandata)
# A tibble: 6 x 6
# Groups:   canopy [3]
  canopy   variable mean.value sd.value count se.mean
  <fct>    <fct>         <dbl>    <dbl> <int>   <dbl>
1 Open     depth5        265.    145.       4   72.4 
2 Open     depth10        20.5    12.8      4    6.41
3 M_Open   depth5         71.8    62.6      4   31.3 
4 M_Open   depth10         6.5     4.20     4    2.10
5 M_Closed depth5         20       8.98     4    4.49
6 M_Closed depth10         0.5     1        4    0.5   
这给出了这样一个图:

我的问题是,当我想添加错误条标准偏差时,代码不会运行。我使用以下代码:

bank2 <- bank1
bank2.mean = ddply(bank2, .(canopy, variable), summarize,
                  plant.mean = mean(value), plant.sd = sd(value))

gs1 <- ggplot(bank1, aes(x = canopy, y= value , fill = variable)) + 
  geom_bar(stat='identity', position = 'dodge', fill = 'darkgray')+ 
  geom_errorbar(aes(ymin=plant.mean-plant.sd, ymax = plant.mean +
                      plant.sd), width = 0.5)+
  xlab("Canopy cover")+ylab("Seed Bank")+ 
  facet_wrap(~variable,nrow=1)
gs1  
我寻求帮助,但我不知道如何继续。 请帮忙

在这里,我重复一个例子:

> set.seed(1)
> Data1 <- data.frame(
+   site= c("KOA","KOB","KOO","EB","PNS","BED","KB","KER","KAU","KAD","RO","BEU"),
+   variable = sample(c("depth5", "depth10"), 12, replace = TRUE),
+   canopy=sample(c("open", "M_open", "M_closed"), 12, replace = TRUE),
+   value=sample(c(100,500,50,20,112,200,230,250,300,150,160,400))
+ )
> Data1
   site variable   canopy value
1   KOA   depth5 M_closed    20
2   KOB   depth5   M_open   112
3   KOO  depth10 M_closed   100
4    EB  depth10   M_open   400
5   PNS   depth5 M_closed   230
6   BED  depth10 M_closed    50
7    KB  depth10   M_open   250
8   KER  depth10 M_closed   200
9   KAU  depth10 M_closed   500
10  KAD   depth5     open   150
11   RO   depth5   M_open   300
12  BEU   depth5     open   160
> gs1 <- ggplot(Data1, aes(x = canopy, y= value , fill = variable)) + 
+   geom_bar(stat='identity', position = 'dodge', fill = 'darkgray')+
+   xlab("Canopy cover")+ylab("Seed Bank")+ 
+   facet_wrap(~variable,nrow=1)
> gs1
> Data2 <- Data1
> data2.mean = ddply(Data2, .(canopy, variable), summarize,
+                    plant.mean = mean(value), plant.sd = sd(value))
> gs1 <- ggplot(Data2, aes(x = canopy, y= value , fill = variable)) + 
+   geom_bar(stat='identity', position = 'dodge', fill = 'darkgray')+ 
+   geom_errorbar(aes(ymin=plant.mean-plant.sd, ymax = plant.mean +
+                       plant.sd), width = 0.5)+
+   xlab("Canopy cover")+ylab("Seed Bank")+ 
+   facet_wrap(~variable,nrow=1)
> gs1
Error in FUN(X[[i]], ...) : object 'plant.mean' not found

我的原始数据也有同样的错误

我的问题的解决方案就在这里。我想要的方式。你需要这些包裹

library(ggplot2)
library(dplyr)
我的数据帧bank1通过管道传输到新的数据帧cleandata中,以计算平均值、sd和se,并总结结果

cleandata <- bank1 %>%
  group_by(canopy, variable) %>%
  summarise(mean.value = mean(value),
            sd.value = sd(value), count = n(),
            se.mean = sd.value/sqrt(count))
总结的结果如下所示:

> str(bank1)
'data.frame':   24 obs. of  4 variables:
 $ site    : Factor w/ 12 levels "BED","BEU","EB",..: 8 9 10 3 11 1 6 7 5 4 ...
 $ canopy  : Factor w/ 3 levels "M_Closed","M_Open",..: 3 3 3 3 2 2 2 2 1 1 ...
 $ variable: Factor w/ 2 levels "depth5","depth10": 1 1 1 1 1 1 1 1 1 1 ...
 $ value   : int  200 319 103 437 33 51 165 38 26 29 ...
gs1 <- ggplot(bank1, aes(x = canopy, y= value , fill = variable)) + 
  geom_bar(stat='identity', position = 'dodge', fill = 'darkgray')+
  xlab("Canopy cover")+ylab("Seed Bank")+ 
  facet_wrap(~variable,nrow=1)
gs1  
> head(cleandata)
# A tibble: 6 x 6
# Groups:   canopy [3]
  canopy   variable mean.value sd.value count se.mean
  <fct>    <fct>         <dbl>    <dbl> <int>   <dbl>
1 Open     depth5        265.    145.       4   72.4 
2 Open     depth10        20.5    12.8      4    6.41
3 M_Open   depth5         71.8    62.6      4   31.3 
4 M_Open   depth10         6.5     4.20     4    2.10
5 M_Closed depth5         20       8.98     4    4.49
6 M_Closed depth10         0.5     1        4    0.5   
最后,使用以下代码完成绘图:

gs1 <- ggplot(cleandata, aes(x=canopy, y=mean.value)) + 
  geom_bar(stat = "identity", color = "black", position = position_dodge())+  
  geom_errorbar(aes(ymin = mean.value - sd.value, ymax = mean.value + sd.value), 
                width=0.2)+
  xlab("Canopy cover")+ylab("Seed Bank")+ 
  facet_wrap(~variable,nrow=1)
gs1  
这给出了一个误差条标准偏差图,如下所示


问题解决了!干杯

你说它不跑是什么意思?会发生什么?我们没有您的数据样本,因此无法复制它以查看错误是什么,因为错误消息表明:数据中没有plant.mean。plant.mean在data2.mean中可用,但在data2中不可用。这是您提供给ggplot的Data2,由于它不在其中,因此无法找到它。Data1也没有具有该名称的列…您仍在寻找解决方案吗?恭喜。这与我打算做的基本相同。不需要打电话给plyr。你可以考虑清理一下你的问题,但这取决于你。