使用填充、颜色和 alpha 用 geom_bar 减淡 geom_errorbar

Dodged geom_errorbar with geom_bar using fill, colour, and alpha

我正在尝试制作一个带有误差线 (se) 的 facet_wrap bar_graph,它清楚地显示三个不同的分类变量(治疗、Horizon、酶)和一个响应变量( AbundChangetoAvgCtl)。下面是一些虚拟数据的代码,后面是我目前拥有的 ggplot 代码。我制作的图表可以在这个 link 看到: bargraph figures

Enzyme <- c("Arabinosides","Arabinosides","Arabinosides","Arabinosides","Arabinosides","Arabinosides","Cellulose","Cellulose","Cellulose","Cellulose","Cellulose","Cellulose","Chitin","Chitin","Chitin","Chitin","Chitin","Chitin","Lignin","Lignin","Lignin","Lignin","Lignin","Lignin")
Treatment <- c("Deep","Deep","Int","Int","Low","Low","Deep","Deep","Int","Int","Low","Low","Deep","Deep","Int","Int","Low","Low","Deep","Deep","Int","Int","Low","Low")
Horizon <- c("Org","Min","Org","Min","Org","Min","Org","Min","Org","Min","Org","Min","Org","Min","Org","Min","Org","Min","Org","Min","Org","Min","Org","Min")
AbundChangetoAvgCtl <- rnorm(24,mean=0,sd=1)
se <- rnorm(24, mean=0.5, sd=0.25)
notrans_noctl_enz_toCtl_summary <- data.frame(Enzyme,Treatment,Horizon,AbundChangetoAvgCtl,se)
ggplot(notrans_noctl_enz_toCtl_summary, aes(x=Horizon, y=AbundChangetoAvgCtl, fill=Horizon, alpha=Treatment)) +
  geom_bar(position=position_dodge(), colour="black", stat="identity", aes(fill=Horizon)) +
  geom_errorbar(aes(ymin=AbundChangetoAvgCtl-se, ymax=AbundChangetoAvgCtl+se),
            width=.2,
            position=position_dodge(.9)) + 
  scale_fill_brewer(palette = "Set1") + theme_bw() +
  geom_hline(yintercept=0) +
  labs(y = "Rel Gene Abundance Change / Control", x="") +
  theme(axis.ticks = element_blank(), 
        axis.text.x = element_blank(),
        strip.text.x = element_text(size=20),
        plot.title = element_text(size=22, vjust=2, face="bold"),
        axis.title.y = element_text(size=18),
        legend.key.size = unit(.75, "in"),
        legend.text = element_text(size = 15),
        legend.title = element_text(size = 18)) +
  facet_wrap(~Enzyme, scales="free")

(图 1)

所以这接近我想要的,但是由于某种原因,ggplot 中的 "alpha=Treatment" 调用导致我的错误栏消失(我不想要)以及 bar_fill (我确实想要)。我试过将 "alpha=Treatment" 移动到 geom_bar 调用,以及将 "alpha=1" 添加到 geom_bar,但是当我这样做时,错误栏全部移动到一个位置和重叠(图 2)。

我最初想将条形集中在 facet_wrap 内,但在该站点上找到了 alpha 选项,它似乎也能满足我的要求。任何帮助,将不胜感激。如果有更好的方式来表示所有这些,也欢迎提出这些想法。
还有,若是有什么办法,能够将我的传说,凝练澄清,那就是额外的加分项! 预先感谢您的帮助!

麦克

您需要将Treatment分配给ggplot()命令中的group选项,然后将alpha=Treatment选项移动到geom_bar()命令。那么geom_errorbaralpha值就不会受到全局选项的影响,会变成黑色。像这样:

ggplot(notrans_noctl_enz_toCtl_summary, aes(x=Horizon, y=AbundChangetoAvgCtl, fill=Horizon, group = Treatment)) +
  geom_bar(position=position_dodge(), colour="black", stat="identity", aes(fill=Horizon, alpha = Treatment)) 

此外,我会检查设置 alpha=Treatment 是否对应于更透明,相当于低处理,而不是高处理。至少这是我的直觉理解,没有任何研究设计或数据背景。

有关格式化图例的信息,请参阅 here