我尝试根据以下数据(从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")
我一直收到这个错误:
&#34;错误:映射必须由aes()
或aes_()
&#34;
任何提示都会非常感激。
答案 0 :(得分:2)
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()
其他一些建议:
$
,它只会制造麻烦