【问题标题】:Dodged geom_errorbar with geom_bar using fill, colour, and alpha使用填充、颜色和 alpha 使用 geom_bar 躲避 geom_errorbar
【发布时间】:2015-04-18 02:36:01
【问题描述】:

我正在尝试制作一个带有误差线 (se) 的 facet_wrap bar_graph,它清楚地显示了三个不同的分类变量(治疗、地平线、酶)和一个响应变量 (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")

(图一)

所以这接近我想要的,但是由于某种原因,ggplot 中的“alpha=Treatment”调用导致我的错误栏消失(我不想要)以及 bar_fill(我想要) .我已经尝试将“alpha=Treatment”移动到 geom_bar 调用,以及将“alpha=1”添加到 geom_bar,但是当我这样做时,误差条都会移动到一个位置并重叠(图 2)。

我最初想在 facet_wrap 中聚集条形图,但在这个网站上找到了 alpha 选项,这似乎也完成了我正在寻找的东西。任何帮助,将不胜感激。如果有更好的方式来代表所有这些,那么这些想法也是受欢迎的。
还有,如果有什么方法可以把我的传说浓缩和澄清,那将是额外的奖励! 提前感谢您的帮助!

迈克

【问题讨论】:

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


    【解决方案1】:

    您需要将Treatment 分配给ggplot() 命令中的group 选项,然后将alpha=Treatment 选项移动到geom_bar() 命令。那么alpha 的值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)) 
    

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

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

    【讨论】:

    • 我已经尝试了您的建议,但似乎没有用。我在上面添加了一些示例代码,为您提供一些可以使用的东西。感谢您的回复!
    • 感谢您提供可重现的示例。我已经编辑了我的答案以包含一个可行的解决方案。如果您遇到任何问题,请告诉我。
    • 非常感谢!!在这个问题上,我把头撞在墙上好几个小时。数字,这是如此简单的事情!非常感谢!
    • 很高兴我能帮上忙。也许您可以接受答案是正确的,这样未来的读者就会知道这是该问题的有效解决方案。
    猜你喜欢
    • 2016-10-05
    • 1970-01-01
    • 1970-01-01
    • 2018-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多