ggplot2绘制多样性指数图
今天继续来进行微生物多样性的后续可视化,来绘制多样性指数图,喜欢的小伙伴可以在我的交流群中获取本文代码及数据
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
SamplesTypeTrialDayRepGroupReadCountSimpsonschrfctintintintchrintdbl1Water_01_01_01_CONRearing_WaterControl.Water_01_01_02_CONRearing_WaterControl.Water_01_01_03_CONRearing_WaterControl.Water_01_01_01_TREATRearing_WaterTreatment.Water_01_01_02_TREATRearing_WaterTreatment.数据可视化
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("lightblue","darkred"))+labs(y="SimpsonsDiversityIndex",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添加实验分组色条
g-ggplot_gtable(ggplot_build(p2))strips-which(grepl(strip-,g$layout$name))pal-c("#E64B35FF","#4DBBD5FF","#00AFF","#3CFF","#F39B7FFF","#B4FF","#91D1C2FF","#FF","#4DBBD5FF","#00AFF","#3CFF","#F39B7FFF","#B4FF","#91D1C2FF","#F8AFA8","#4DBBD5FF","#B09C85FF","#3CFF","#F39B7FFF","#B09C85FF","#91D1C2FF","#D3DDDC","#00AFF","#E6A0C4","#3CFF")for(iinseq_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)
此图最大的缺陷也许就在于没有添加P值,分面图自定义添加P值过程比较繁琐,本人水平有限目前没有好的解决方法,此等细节还是AI来的处理效率高。希望对各位有所帮助
ANERYAN
转载请注明:http://www.sonphie.com/jbzl/14199.html