美文网首页
ggplot2绘制richness(物种丰富度)指数图

ggplot2绘制richness(物种丰富度)指数图

作者: R语言数据分析指南 | 来源:发表于2021-04-15 11:04 被阅读0次

这一节我们来根据OTU表计算样本的richness即丰富度指数,统计样本中不为0的数目累加即可

点击原文链接获取数据

richness(物种丰富度指数计算)

加载所需R包

rm(list=ls())
library(tidyverse)
library(magrittr)
library(ggsci)
library(ggpubr)

计算richness

otu <- read.delim("otu_table.tsv",header = T,sep="\t",
                  check.names = F,row.names = 1)

richness <- NULL
for (i in seq_len(ncol(otu))) {
  richness <- c(richness,which(otu[,i] !=0) %>% length())
}

names(richness) <- otu %>% colnames() 

richness <- richness %<>% data.frame(sample= names(.),value = .)

richness
#p <- richness %<>% data.frame(names(.),.) %>% 
#  set_colnames(c("sample","value"))

group <- read.delim("group.xls",header = T,sep="\t",check.names = F)

colnames(richness)[1] <- colnames(group[1])

上述代码统计样本中read数不为0的个数进行累加

连接数据并可是化

将richness信息与样本分组信息整合传递给ggplot2进行数据可视化

full_join(richness,group) %>%
  mutate(V4="richness") %>%
  ggplot(aes(group,value,fill=group))+
  scale_fill_nejm()+
  geom_violin(trim=FALSE)+
  geom_boxplot(width=0.1,fill="white")+
  facet_grid(. ~V4,scales = "free",space="free_x")+
  xlab(NULL)+ylab(NULL)+
  theme_bw()+
  theme(legend.position = "none",
        axis.text.x=element_text(angle =0,hjust=0.5,vjust=0.5,
                                 colour = "black",size=8),
        strip.text.x = element_text(colour ="black",size=13))+
  stat_compare_means(method = "anova",label.y =1000,label.x = 2,size=5)

原文链接:https://mp.weixin.qq.com/s/nB7cPb0ij0srVc_v_KG0AQ

相关文章

网友评论

      本文标题:ggplot2绘制richness(物种丰富度)指数图

      本文链接:https://www.haomeiwen.com/subject/fkjptltx.html