美文网首页
【R绘图】物种丰度堆积柱状图

【R绘图】物种丰度堆积柱状图

作者: 花生学生信 | 来源:发表于2023-12-25 10:08 被阅读0次

物种丰度堆积柱状图是一种用于展示不同物种在样本中的相对丰度和组成的图表。它通过堆积多个柱状图来显示不同物种的相对贡献,并以整体的柱状图表示样本的总丰度。

#import data and modify it
phylum <- read.table('count_2Phylum.txt', sep="\t", header=T, row.names=1)
head(phylum)
phylum_per <- as.data.frame(lapply(phylum, function(x) x / sum(x)))
row.names(phylum_per) <- row.names(phylum)
phylum.ave <- apply(phylum_per, 1, FUN=mean)
phylum.2 <- cbind(phylum_per, phylum.ave)[order(-phylum.ave),]

#sum(phylum.2$phylum.ave[1:4])  #check the summary of top 4 taxonomies
phylum.2 <- subset(phylum.2[1:10,], select=-phylum.ave)
phylum.2 <- rbind(phylum.2, others=apply(phylum.2, 2, function(x){1-sum(x)}))

#apply(phylum.2, 2, function(x){sum(x)})  #check the summary of each column
phylum.2 <- cbind(PhylumID=row.names(phylum.2), phylum.2)
#convert the data format
library(reshape2)
phylum.2$PhylumID <- factor(phylum.2$PhylumID, levels = rev(phylum.2$PhylumID))
phylum.gg <- melt(phylum.2, id.vars="PhylumID", variable.name="SampleID", value.name="Abundance")
head(phylum.gg)

phylum.gg$group <- c(rep('KO', 66), rep('OE', 66), rep('WT', 66))
library(wesanderson)
library(colortools)
library(ggpubr)

ggbarplot(phylum.gg, x = "SampleID", y="Abundance", color="black", fill="PhylumID",
          legend="right", 
          legend.title="Top Phylum", main="Relative counts per Phylum",
          font.main = c(14,"bold", "black"), font.x = c(12, "bold"), 
          font.y=c(12,"bold")) + 
  theme_bw() +
  rotate_x_text() + 
  scale_fill_manual(values=c("gray",rev(wheel("skyblue3")[1:10]))) +
  facet_grid(~ group, scales = "free_x", space='free') + 
  labs(x = "Sample", y = "Relative proportion") + 
  theme(axis.text.x=element_blank(),
        axis.ticks.x=element_blank(),
        axis.title = element_text(face = "bold"), 
        plot.title = element_text(face = "bold"), 
        legend.title = element_text(face = "bold")) 
ggsave(filename = "relative_counts.pdf", device="pdf", width=8, height=4)
结果图 数据格式

在物种丰度堆积柱状图中,每个柱子表示一个样本,柱子的高度表示该物种在该样本中的相对丰度。不同物种的丰度通过堆积在一起的方式显示在柱子上,形成一个由不同颜色组成的柱状堆积图。
堆积柱状图的优点是可以直观地展示不同物种的相对丰度和它们在样本中的分布情况。通过比较不同样本的柱状堆积图,可以观察到物种组成的差异,找出特定样本中相对丰度较高的物种。此外,堆积柱状图还可以显示物种的排序,从而更好地理解物种之间的相对丰度关系。
然而,堆积柱状图也有一些限制。由于每个柱子代表一个样本,当样本数量较多时,柱状堆积图可能会变得非常拥挤,难以解读。此外,如果物种数量较多,也可能导致颜色的使用过多,使图表难以阅读。因此,在设计堆积柱状图时,需要考虑样本数量和物种数量的平衡,以及如何选择合适的颜色方案。

数据及代码:

链接:https://pan.baidu.com/s/1HToLhT9jWK2Zk4H2Hlt8OA?pwd=90uk
提取码:90uk

相关文章

网友评论

      本文标题:【R绘图】物种丰度堆积柱状图

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