我正在尝试根据数据中各个组的方法创建一个 ggplot。方法是使用聚合找到的;
f1 <- function(x) c(Mean = mean(x), std_error=std.error(x)) #create function for mean and standard error coverboard_stat<-aggregate(no_count ~ EEM.or.NCOS + Species+Period..Oct.Sep.+Habitat, data = coverboard, f1)
所以它现在看起来像这样(作为例子;真实的数据集要大得多)
团体 | no_count[,"均值" | no_count[,"std_error" |
---|---|---|
类型 1 | 1 | .05 |
类型 2 | 2 | .75 |
这是我的条形图代码:
ggplot(aes(x = Species, y = no_count[,"Mean"]), data = post_rest) +
geom_bar(aes(fill=EEM.or.NCOS), stat = "identity", position = "dodge") +
scale_y_continuous(expand = expansion(mult = c(0, 0.1))) +
theme_classic() +
theme(axis.text.x = element_text(angle = 45, vjust = 0.9, hjust=1)) +
labs(fill="Site") +
scale_fill_brewer(palette = "YlOrRd", labels=c("South Parcel", "North Campus Open Space")) +
labs(x="", y="Encounter rate") +
theme(legend.position="bottom")+
geom_errorbar(aes(ymin=no_count[,"Mean"]-no_count[,"std_error"],
ymax=no_count[,"Mean"]+no_count[,"std_error"],
fill=EEM.or.NCOS),
width=.2,
position=position_dodge(.9))
这是我得到的结果: 显然这是不正确的;我只想为每个条设置一个误差条。
我尝试以不同的方式格式化 (aes) 并使用摘要而不是聚合(这让我陷入了一个完全不同的错误虫洞)。
编辑: 这是我的更多数据:
EEM.or.NCOS Species Period..Oct.Sep. Habitat no_count.Mean no_count.std_error
1 EEM Alligator.Lizard 2020-2021 Coastal Sage Scrub 0.011019284 0.005486764
2 NCOS Alligator.Lizard 2020-2021 Coastal Sage Scrub 0.000000000 0.000000000
3 EEM Garter.Snake 2020-2021 Coastal Sage Scrub 0.016528926 0.006701142
4 NCOS Garter.Snake 2020-2021 Coastal Sage Scrub 0.012820513 0.006379264
5 EEM Gopher.Snake 2020-2021 Coastal Sage Scrub 0.002754821 0.002754821