簡體   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