導(dǎo)讀
ggtree由R語言大神Y叔紙筆,于2018年發(fā)表在Molecular Biology and Evolution雜志上,現(xiàn)引用已達(dá)407(2019.10.10 google scholar)。ggtree是ggplot2的拓展包,它不僅能畫進(jìn)化樹,而且還能對進(jìn)化樹進(jìn)行豐富多彩的美化和添加多組數(shù)據(jù)或元素。使用ggtree給可以輕松給10分以上的文章畫進(jìn)化樹。
一、安裝
R地址:Bioconductor - ggtree
github地址:https://github.com/YuLab-SMU/ggtree
R
# 在linux中進(jìn)入R
if (!requireNamespace("BiocManager", quietly = TRUE))
install.packages("BiocManager")
BiocManager::install("ggtree")
# 安裝ggtree
二、畫圖
繪圖代碼參考原文:PhyloPhlAn
數(shù)據(jù)來源和繪圖方法見:Phylophlan(二)種間進(jìn)化分析
dir() # 查看工作目錄
[1] "example_corynebacteria.tree.nwk"
[2] "example_corynebacteria.tree.reroot.xml"
library(ggplot2) # 加載ggplot2
library(ggtree) # 加載ggtree
tree=read.tree("example_corynebacteria.tree.nwk") # 讀取nwk文件
data=fortify(tree)
tregraph=ggtree(tree, layout="rectangular", size=0.8, col="deepskyblue3") +
# 樹體:樹文件、樹形、粗細(xì)、顏色
geom_tiplab(size=3, color="purple4", hjust=-0.05) +
# 枝名:大小、顏色、高度
geom_tippoint(size=1.5, color="deepskyblue3") +
# 端點(diǎn):大小、顏色
geom_nodepoint(color="orange", alpha=1/4, size=2) +
# 末節(jié)點(diǎn):顏色、透明度、大小
theme_tree2() +
# x軸標(biāo)尺
xlim(NA, max(data$x)*1.3)
# x軸寬度
pdf("tregraph.pdf")
tregraph
dev.off()
打開tregraph.pdf查看結(jié)果,如下:
圖片.png
三、圈圖
#!/usr/bin/env Rscript
library(ggplot2) # 加載ggplot2
library(ggtree) # 加載ggtree
tree=read.tree(list.files()[grepl("denovo.tree.nwk", list.files())]) # 讀取nwk文件
map=read.table("map.txt", header=T, sep="\t", comment.char="")
data=fortify(tree)
tregraph=ggtree(tree, layout="circular", ladderize=FALSE, size=0.8, branch.length="none", aes(col=Phylum)) %<+% map +
# 樹體:樹文件、樹形、粗細(xì)、顏色
geom_tiplab2(hjust=-0.3) +
# 枝名:大小、顏色、高度
geom_tippoint(size=3)+
# 端點(diǎn)顏色、大小
theme(legend.title=element_text(face="bold"), legend.position="bottom", legend.box="horizontal", legend.text=element_text(size=rel(0.8)))
# 圖例位置、文字大小
ggsave(tregraph, file="tree_bins.pdf", width=9, height=9)
gra=ggtree(data, color="black", layout="fan", branch.length="none", size=1, open.angle=10) %<+% map +
#gra=ggtree(data, color="black", layout="fan", size=1, open.angle=10) %<+% map +
geom_tippoint(aes(col=Phylum), size=5) +
theme(legend.title=element_text(face="bold", size=15), legend.position="right", legend.text=element_text(size=13)) +
theme(text=element_text(family="serif")) +
scale_color_manual(
values = c("Actinobacteriota" = "#33a02c",
"Bacteroidota" = "#EE1289",
"Desulfobacterota" = "#b2df8a",
"Firmicutes" = "#009ACD",
"Fusobacteriota" = "#EEAD0E",
"Proteobacteria" = "#EE6363",
"Synergistota" = "#EEB4B4",
"Verrucomicrobiota" = "#9F79EE"))
ggsave(gra, file="tree2.pdf", height=30, width=30)
推薦閱讀:
[1] ggtree美化進(jìn)化樹
[2] Y叔: facet_plot: 關(guān)聯(lián)數(shù)據(jù)和進(jìn)化樹的通用方法
[3] Y叔: 進(jìn)化樹圖形注釋