磕盐——从入门到自闭

ggplot2绘制多样性指数图

2021-05-09  本文已影响0人  R语言数据分析指南

今天继续来进行微生物多样性的后续分析,来绘制多样性指数图,喜欢的小伙伴可以在我的交流群中获取本文代码及数据

library(tidyverse)
library(openxlsx)
library(vegan)
library(phyloseq)
library(scales)
library(ggh4x)

数据统计

转换数据格式计算simpson指数

data <- read.delim("otu.xls",sep="\t",check.names = F,row.names = 1)
meta_data <- read.delim("taxa.xls",sep="\t",check.names = F)
meta_data$Simpsons <- diversity(data,index="simpson")

meta_data$Type <- factor(meta_data$Type,
                         levels =c("Oyster_Larvae","Biofilm_Swab","Rearing_Water"))

meta_data
  Samples              Type          Trial   Day   Rep Group     ReadCount Simpsons
   <chr>                <fct>         <int> <int> <int> <chr>         <int>    <dbl>
 1 Water_01_01_01_CON   Rearing_Water     1     1     1 Control       70486    0.570
 2 Water_01_01_02_CON   Rearing_Water     1     1     2 Control      120040    0.565
 3 Water_01_01_03_CON   Rearing_Water     1     1     3 Control      106854    0.594
 4 Water_01_01_01_TREAT Rearing_Water     1     1     1 Treatment    106047    0.591
 5 Water_01_01_02_TREAT Rearing_Water     1     1     2 Treatment    138836    0.613

数据可视化

p2 <- ggplot(meta_data, aes(as.factor(Day), Simpsons)) +
  facet_nested(.~Type+Trial+Day,drop=T,
               scale="free",space="free_x",switch="x")+
  geom_boxplot(aes(fill=factor(Group)))+
  scale_fill_manual(values=c("light blue", "dark red")) + 
  labs(y="Simpson's Diversity Index",x=NULL,fill="Group")+
  theme_cleveland()+
  theme(axis.text.y = element_text(size=12), 
        axis.title.y = element_text(size=12), 
        legend.position = "right",
        panel.spacing = unit(1, "mm"),
        panel.background = element_blank(),
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank(),
        strip.text.x = element_text(size=12),
        strip.background = element_rect(fill="white",color="black"))+
  scale_y_continuous(limits=c(0,0.8))

p2
image

添加实验分组色条

g <- ggplot_gtable(ggplot_build(p2))

strips <- which(grepl('strip-', g$layout$name))

pal <- c("#E64B35FF","#4DBBD5FF","#00A087FF","#3C5488FF","#F39B7FFF","#8491B4FF","#91D1C2FF",
         "#FF0000","#4DBBD5FF","#00A087FF","#3C5488FF","#F39B7FFF","#8491B4FF","#91D1C2FF",
         "#F8AFA8","#4DBBD5FF","#B09C85FF","#3C5488FF","#F39B7FFF","#B09C85FF","#91D1C2FF",
         "#D3DDDC","#00A087FF","#E6A0C4","#3C5488FF")

for (i in seq_along(strips)) {
  k <- which(grepl('rect', g$grobs[[strips[i]]]$grobs[[1]]$childrenOrder))
  l <- which(grepl('titleGrob', g$grobs[[strips[i]]]$grobs[[1]]$childrenOrder))
  g$grobs[[strips[i]]]$grobs[[1]]$children[[k]]$gp$fill <- pal[i]
  # g$grobs[[strips[i]]]$grobs[[1]]$children[[l]]$children[[1]]$gp$col <- pal[i] #设置字体颜色
}

plot(g)
image

此图最大的缺陷也许就在于没有添加P值,分面图自定义添加P值过程比较繁琐,本人水平有限目前没有好的解决方法,此等细节还是AI来的处理效率高。希望各位老爷能够喜欢

上一篇下一篇

猜你喜欢

热点阅读