照例,先放结果图
代码如下:
1.导入数据
# 导入数据并查看数据集格式
rm(list = ls())##清除之前命令
setwd("D:\\R_base\\Rstudio_workfile\\Co_occurrence\\3_genderage")
phylum <- read.delim('profile_phylumtop20.txt', sep="\t", header=T, row.names=1)
head(phylum)
2.数据处理
# 将绝对丰度转化为百分比形式的相对丰度
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),] #排个序
# 选择丰度最高的10个门 剩下的放入others里
phylum.2 <- subset(phylum.2[1:10,], select=-phylum.ave)
# 统计others丰度
phylum.2 <- rbind(phylum.2, others=apply(phylum.2, 2, function(x){1-sum(x)}))
# 加一列行名 便于后续的长宽转换
phylum.2 <- cbind(PhylumID=row.names(phylum.2), phylum.2)
# 长宽转换
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('S', 132), rep('M', 154), rep('L', 132)) # 根据样本情况设置
3.绘图
# 为了复现文章中的图需要的颜色包
library(wesanderson) ##install.packages("wesanderson")
library(colortools) ##remotes::install_github("gastonstat/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)
##数据示例(OTU绝对丰度表)