使用填充,颜色和alpha对geom_bar反复使用geom_errorbar

时间:2015-04-18 02:36:02

标签: r ggplot2 alpha facet-wrap geom-bar

我试图制作一个带有误差条(se)的facet_wrap bar_graph,它清楚地显示了三个不同的分类变量(Treatment,Horizo​​n,Enzyme)和一个响应变量(AbundChangetoAvgCtl)。下面是一些虚拟数据的代码,后面是我到目前为止的ggplot代码。我已经制作的图表可以在以下链接中看到: 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)

所以这接近我想要的,但出于某种原因,&#34; alpha =治疗&#34;在ggplot中调用导致我的错误栏淡出(我不想要)以及bar_fill(我想要的)。我已经尝试过移动&#34; alpha =治疗&#34;到geom_bar调用,以及添加&#34; alpha = 1&#34;到geom_bar,但是当我这样做时,误差条全部移动到一个位置并重叠(图2)。

我最初想在facet_wrap中对条形图进行聚类,但在此网站上找到了alpha选项,这似乎也完成了我正在寻找的内容。任何帮助,将不胜感激。如果有更好的方式来代表所有这些,那么这些想法也是受欢迎的 此外,如果有一种方法可以浓缩和澄清我的传奇,那将是额外的奖励! 在此先感谢您的帮助!

麦克

1 个答案:

答案 0 :(得分:1)

您需要将Treatment分配给group命令中的ggplot()选项,然后将alpha=Treatment选项移至geom_bar()命令。然后,alpha的{​​{1}}值不会受到全局选项的影响,并且会变黑。像这样:

geom_errorbar

另外,我会检查设置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)) 是否对应于更透明,因为它等同于低治疗,而对高治疗则不太透明。至少那是我直观的理解,没有研究设计或数据的任何背景。

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