【发布时间】: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) 才能看到条形图。请参考下面发布的答案