使用 ggplot 绘制扩增子 rank abundance 曲线 Posted on 2017-04-20 | Edited on 2019-04-21 | In R ggplot2实现微生物rank abundance绘图,另发现了reshape2能方便的进行dataframe格式整理,长表格,宽表格相互转换 具体代码实现如下: 123456789101112131415161718192021222324252627282930library(ggplot2)library(reshape2)# otu_table 抽平后的otu table,第一列为OTU编号,最后一列为tax注释otu_table <- read.table(otu_table, sep="\t", header=TRUE)otu_table <- otu_table[,2:(length(otu_table)-1)]for(i in 1:length(otu_table)){ # 计算等级相对丰度 sample <- otu_table[,i] otu_table[,i]<- rev(sort(sample/sum(sample)))}rank_abundance <- otu_tablerank_abundance$rank <- 1:dim(rank_abundance)[1] # 添加等级# 宽表格整理成长表格dat <- melt(rank_abundance, id.vars = c('rank'), variable.name='sample')# 整合样品分组信息group <- read.table(group, sep='\t', col.names=c('sample', 'group'))dat <- merge(dat, group, by=c('sample'), all=TRUE)# 移除0值dat <- dat[dat$value>0,]g <- ggplot(dat, aes(rank, log10(dat$value), group=sample)) + geom_step(aes(color=group)) + #ylim(-4,0) + xlim(0,2000) + labs(x="Species Rank", y="Relative Abundance") + # 设置y轴指示刻度,针对不同数据,可能需要调整 scale_y_continuous(breaks=c(-4,-3,-2,-1), labels=c('1e-4','1e-3','1e-2','1e-1')) + # 调整y轴指示刻度显示方法 theme(axis.text.y = element_text(angle=90, hjust=0.5, vjust=1)) + ggsave("rank_abundance.pdf") 结果展示: ---------本文结束,感谢您的阅读---------