【问题标题】:How can I create a grouped bar plot in R using the ggplot and aes functions?如何使用 ggplot 和 aes 函数在 R 中创建分组条形图?
【发布时间】:2016-07-25 12:06:33
【问题描述】:

我正在尝试从以下数据(从 ddply 函数返回)创建分组条形图,其中 x 轴具有所有 4 个 CWD 变量(对于 2 个站点中的每一个),y 轴是平均值。

我的代码是:

library(plyr)
library(reshape2)
library(ggplot2)

 ddply(data, c("Site","Plot","Cover"), summarise, mean=mean(Height), sd=sd(Height), 
    sem=sd(Height)/sqrt(length(Height)))

BranchSize <- ddply(data, c("Site","CWD"), summarise, mean=mean(Volume),
    sd=sd(Volume), sem=sd(Volume)/sqrt(length(Volume)))

它返回这个表。此表是否已经是一个数据框,还是我需要将其设为一个才能使用它?

  Site    CWD        mean         sd        sem
1 High   Bark    975.7273   2603.077   554.9780
2 High Branch  36827.7735 107668.064 13056.6706
3 High   Cage 116041.4286 154934.888 58559.8832
4 High    Log  73463.3636 121054.372 25808.8788
5  Low   Bark     40.0000         NA         NA
6  Low Branch   1323.8280   2304.571   595.0377
7  Low   Cage    101.5000         NA         NA
8  Low    Log 102600.0000         NA         NA

然后使用此代码:

limits <- aes(ymax = BranchSize$mean + BranchSize&se, 
    ymin=BranchSize$mean - BranchSize&se)
CWDVol<-ggplot(data = BranchSize, 
    aes(x = factor(CWD), y = mean, fill = factor(Site)))
CWDVol

当我运行这个命令时,我的情节出现了,但没有条形图。

然后当我运行这个时:

CWDVol + geom_bar(stat = "identity", position_dodge(0.9)) + 
    geom_errorbar(limits, position = position_dodge(0.9), width = 0.25) + 
    labs(x = "CWD Type", y = "Average Volume") + 
    ggtitle("Average CWD Size in each Site") + 

    scale_fill_discrete(name = "Site")

我不断收到此错误: "错误:必须由aes()aes_() 创建映射"

任何提示将不胜感激。

【问题讨论】:

  • 试试stat = "count"?
  • geom_bar 中传递position = position_dodge(0.9),在limits 中,你这样做了:BranchSize$se,没有名为se 的变量。此外,您不必包含BranchSize$
  • 谢谢。我将统计信息更改为“计数”并通过将“se”更改为“sem”来纠正我的错误,但它仍然无法正常工作。当我运行 CWDVol 时,它会生成一个没有任何条形的图表
  • 是的,您需要在CWDVol 中添加一个层 (geom_bar) 才能看到条形图。请参考下面发布的答案

标签: r plot dataframe


【解决方案1】:

tl;dr您遗漏了position 参数的名称,因此geom_bar 假定position_dodge(0.9)mapping 参数。一旦你修复它,一切似乎都运行良好。

BranchSize <- read.table(header=TRUE,text="
Site    CWD        mean         sd        sem
High   Bark    975.7273   2603.077   554.9780
High Branch  36827.7735 107668.064 13056.6706
High   Cage 116041.4286 154934.888 58559.8832
High    Log  73463.3636 121054.372 25808.8788
Low   Bark     40.0000         NA         NA
Low Branch   1323.8280   2304.571   595.0377
Low   Cage    101.5000         NA         NA
Low    Log 102600.0000         NA         NA")

library(ggplot2)
limits <- aes(ymax=mean+sem, ymin=mean-sem)
CWDVol <- ggplot(data=BranchSize,
                 aes(x=factor(CWD),y=mean,fill=factor(Site)))
CWDVol + geom_bar(stat="identity",position=position_dodge(0.9))+
   geom_errorbar(limits, position = position_dodge(0.9), width = 0.25) + 
   labs(x = "CWD Type", y = "Average Volume") + 
   ggtitle("Average CWD Size in each Site")+
   scale_fill_discrete(name = "Site")+
   scale_y_log10()

其他一些建议:

  • 不要在映射中使用$,这只会带来麻烦
  • 是否可以使用 +/- 2 SEM(或 1.96 SEM)作为置信区间? (否则,您所有的观众可能都会尝试在他们的脑海中进行这种转变。)

代码运行结果如下

【讨论】:

  • 非常感谢您的帮助!对此,我真的非常感激。最后一件事 - 我将如何在每个条形上方添加样本量(例如:n = 2)?使用 text() 是唯一的方法(然后我手动添加标签)?我使用 length() 提取了样本大小
  • 关于堆栈溢出和更普遍的网络有很多这样的答案。如果找不到,请发布另一个问题...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-22
相关资源
最近更新 更多