繁体   English   中英

如何在ggplot中叠加geom_rect和geom_boxplot?

[英]how to overlay geom_rect and geom_boxplot in ggplot?

我正在尝试使用ggplot在一些箱线图下覆盖geom_rect 我希望灰色矩形位于箱形图后面,但由于某种原因我不能这样做。

这是我正在使用的代码:

ggplot(data, aes(x = reorder(genotype, -Shann.div, FUN = median), y = Shann.div)) + 
  geom_jitter(color="black", size=0.3, alpha=0.9) + geom_boxplot(outlier.shape = NA, coef = 0) + 
  geom_hline(yintercept = avg, color="red") + 
  #geom_hline(yintercept = (avg + 2 * SE), linetype='dashed', color="black") + 
  #geom_hline(yintercept = (avg - 2 * SE), linetype='dashed', color="black") + 
  geom_rect(aes(xmin=0, xmax=Inf, ymin=avg - SD, ymax=avg + SD), fill="grey", alpha=0.01) +
  theme(panel.background = element_blank()) + 
  theme(axis.text.x=element_blank(), axis.ticks.x=element_blank()) +
  ggtitle('Microbial UTO - Shannon diversity median') + xlab('genotype')

在此处输入图像描述

ggplot package 按照您声明它们的顺序绘制geom图层。 如果你想要geom_rect层在后面,把它放在代码中的其他层之前:

ggplot(data, aes(x = reorder(genotype, -Shann.div, FUN = median), y = Shann.div)) + 
  geom_rect(aes(xmin=0, xmax=Inf, ymin=avg - SD, ymax=avg + SD), fill="grey", alpha=0.01) +
  geom_jitter(color="black", size=0.3, alpha=0.9) + geom_boxplot(outlier.shape = NA, coef = 0) + 
  geom_hline(yintercept = avg, color="red") + 
  #geom_hline(yintercept = (avg + 2 * SE), linetype='dashed', color="black") + 
  #geom_hline(yintercept = (avg - 2 * SE), linetype='dashed', color="black") + 
  theme(panel.background = element_blank()) + 
  theme(axis.text.x=element_blank(), axis.ticks.x=element_blank()) +
  ggtitle('Microbial UTO - Shannon diversity median') + xlab('genotype')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM