前两天在公众号植物与根际微生物生态
看到一篇转载的文章,关于聚类与物种组成柱状图的:
翻看了作者的代码,基本是用R base
写的,我想着能不能用Y数的ggtree
结合ggplot2
来完成。数据使用的是这个公众号提供的数据(表示感谢)。
绘制聚类数
library(tidyverse)
library(ggplot2)
library(ggtree)
library(treeio)
library(ggsci)
library(cowplot)
# 导入OTU文件
otu = read.table('phylum_top10.txt', header = T)
# 计算距离后均值聚类并建树及可视化
tree = hclust(vegan::vegdist(t(otu), method = 'bray'),
method = 'average') %>%
as.phylo()
# 选择节点,方便后续分开上色
tree = groupClade(tree, .node=c(16))
# 绘制聚类图
p1 = ggtree(tree, aes(color=group, linetype=group)) +
geom_tiplab(aes(color=group))
# +geom_text2(aes(subset=!isTip, label=node), hjust=-.3)</pre>
2.png
绘制物种组成柱状图
p2 = otu %>%
mutate(phylum = rownames(otu)) %>%
reshape2::melt(id.vars = 'phylum') %>%
ggplot(aes(variable, value, fill = phylum))+
geom_bar(stat = 'identity', position = 'fill')+
scale_x_discrete(limits = c('t1','t2','t5','t4','t3','t6',
'c1','c2','c6','c4','c3','c5'))+
scale_fill_igv()+
scale_y_continuous(expand = c(0,0))+
scale_y_continuous(labels = scales::percent)+
coord_flip()+
theme_classic()+
theme(axis.ticks.y = element_blank(),
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.line = element_blank())+
labs(y = 'Percentage')
3.png
拼图
ggdraw()+
draw_plot(p1, 0, 0.06, 0.5, 0.95)+
draw_plot(p2, 0.49, 0, 0.5, 1)</pre>
1.png
参考文献
[1] Yu, Guangchuang, et al. "ggtree: an R package for visualization and annotation of phylogenetic trees with their covariates and other associated data." Methods in Ecology and Evolution 8.1 (2017): 28-36.
点击下载示例数据与完整代码
网友评论