用ggtree和ggimage对nwk进化树进行美化 2020-
2022-04-16 本文已影响0人
RashidinAbdu
image.png
一、对做好的nwk进化树进行簇标注、分组和美化:
-
ggtree教程参考网址:
http://yulab-smu.top/treedata-book/chapter1.html
1. 数据:做好的nwk进化树:
今天用做好的nwk进化树(即:用MEGA构建的系统进化树或基因树)为基础,下面的部分复制存到txt里,文件类型改为.nwk
,即可。
((((((((('Roseburia hominis A2-183T (AJ270482)','Roseburia intestinalis strain L1-82T (AJ312385)'),'Roseburia faecis M721T (AY305310)'),'Roseburia cecicola ATCC 33874T(L14676)'),'Roseburia inulinivorans strain A2-194T (AJ270473)'),('Agathobacter ruminis strain JK623T (KT229569)','Agathobacter rectalis ATCC 33656T (NR 074634)')),('Pseudobutyrivibrio xylanovorans strain Mz5T (AJ428548)','Pseudobutyrivibrio ruminis DSM 9787T (X95893)')),('Falcatimonas natans DSM 22923T (AB298726)','Lachnobacterium bovis strain LRC 5382T (AF298663)')),(Strain_2710,('Kineothrix alysoides strain KNHs209T (KX356505)','Eisenbergiella tayi strain B086562T (KF814111)'))),'Coprococcus comes strain ATCC 27758T (EF031542)',(('Coprococcus catus strain VPI-C6-61T (AB038359)','Coprococcus eutactus strain ATCC 27759T (EF031543)'),('Lachnospira pectinoschiza ATCC 49827T (L14675)','Lachnospira multipara ATCC 19207T (AH000856)')));
2. 需要的包的安装和具体实现:
-
安装ggtree:
install.packages("BiocManager")
BiocManager::install("ggtree")
library(ggtree)
安装其他附包:
install.packages('pacman')
install.packages('ggstar')
install.packages('ggnewscale')
install.packages('ggsci')
-
加载需要的包:
library(ggtree)
library( ggplot2)
library(ggstar )
library( ggtreeExtra)
library(treeio )
library(ggnewscale )
library(ggsci )
-
读取进化树(事先将进化树置于工作目录下):
tree <- read.newick("tree.nwk" )#读取进化树
ggtree(tree,branch.length = "none",layout = "circular",
linetype=1,size=1,ladderize = F)#查看进化树
image.png
-
显示进化树的结点(node):
p1<-ggtree(tree,branch.length = "none",layout = "circular", linetype=1,size=1,ladderize = F )
p1<-p1+geom_text(aes(label=node), hjust=-3,size=2)
p1
image.png
-
进行外圈分组标注(根据始-终node值):
pc1<-p1+geom_strip(30,31, label = "Group I", align = T, alpha=.8,family="Times", fontsize=4,offset =2, color = "brown",offset.text = 2 ,hjust="center",barsize = 4,extend = 0.3)+geom_strip(8,28, label = "Group II", align = T, alpha=.8,family="Times", fontsize=4,offset = 2, color = "red",offset.text = 2 ,hjust="center",barsize = 4,extend = 0.3)+geom_strip(11,19, label = "Group III", align = T, alpha=.8,family="Times", fontsize=4,offset = 2, color = "blue",offset.text = 2 ,hjust="center",barsize = 4,extend = 0.3)
pc1
image.png
-
添加对应簇的高亮标注(根据node值):
pc2<-pc1+geom_hilight(node=23, fill="red")+geom_hilight(node=32, fill="lightblue")+geom_hilight(node=34, fill="yellow")
pc2
image.png
-
也可以先着色:
-
先读取进化树:
p1<-ggtree(tree,branch.length = "none",layout = "circular", linetype=1,size=1,ladderize = F )
p1<-p1+geom_text(aes(label=node), hjust=-3,size=2)
p1
image.png
-
着色:
p2<-p1 +geom_hilight(node=29, fill="red")+geom_hilight(node=34, fill="green")
p2<-p2+geom_hilight(node=32, fill="blue")+geom_hilight(node=31, fill="#FF00FF")
p2<-p2+geom_hilight(node=30, fill="#32CD99")+geom_hilight(node=25, fill="#38B0DE")
p2
image.png
- 对不同分组进行外环标注(是基于分组起始位置,比如:node25---29):
p3<-p2+ geom_strip(31,30, label = "Group I", align = T, alpha=.8,fontsize=4,offset = 1.5,color = "red",offset.text =3 ,hjust="center",barsize = 2,extend = 1)
p3<-p3+ geom_strip(25,29, label = "Group II", align = T, alpha=1.8,fontsize=4,offset = 1.5,, color = "#2F2F4F",offset.text =3 ,hjust="center",barsize = 2,extend = 1)
p3<-p3+ geom_strip(27,27, label = "Group III", align = T, alpha=1.8,fontsize=4,offset = 1.5, color = "blue",offset.text =3 ,hjust="center",barsize = 2,extend = 1)
p3<-p3+ geom_strip(32,33, label = "Group IV", align = T, alpha=1.8,fontsize=4,offset = 1.5, color = "orange",offset.text =3 ,hjust="center",barsize = 2,,extend = 1)
p3<-p3+ geom_strip(34,36, label = "Group V", align = T, alpha=1.8,fontsize=4,offset = 1.5, color = "#DB7093",offset.text =3 ,hjust="center",barsize = 2,,extend = 1)
p3
得到:
image.png-
如果不需要node编号,那么:
-
展示不带node的树:
p1<-ggtree(tree,branch.length = "none",layout = "circular", linetype=1,size=1,ladderize = F )
p1
image.png
- 背景着色分组:
p2<-p1 +geom_hilight(node=29, fill="red")+geom_hilight(node=34, fill="green")
p2<-p2+geom_hilight(node=32, fill="blue")+geom_hilight(node=31, fill="#FF00FF")
p2<-p2+geom_hilight(node=30, fill="#32CD99")+geom_hilight(node=25, fill="#38B0DE")
p2
image.png
- 外圈标注分组:
p3<-p2+ geom_strip(31,30, label = "Group I", align = T, alpha=.8,fontsize=4,offset = 1.5,color = "red",offset.text =3 ,hjust="center",barsize = 2,extend = 1)
p3<-p3+ geom_strip(25,29, label = "Group II", align = T, alpha=1.8,fontsize=4,offset = 1.5,, color = "#2F2F4F",offset.text =3 ,hjust="center",barsize = 2,extend = 1)
p3<-p3+ geom_strip(27,27, label = "Group III", align = T, alpha=1.8,fontsize=4,offset = 1.5, color = "blue",offset.text =3 ,hjust="center",barsize = 2,extend = 1)
p3<-p3+ geom_strip(32,33, label = "Group IV", align = T, alpha=1.8,fontsize=4,offset = 1.5, color = "orange",offset.text =3 ,hjust="center",barsize = 2,,extend = 1)
p3<-p3+ geom_strip(34,36, label = "Group V", align = T, alpha=1.8,fontsize=4,offset = 1.5, color = "#DB7093",offset.text =3 ,hjust="center",barsize = 2,,extend = 1)
p3
image.png
二、对做好的nwk进化树用图片进行标注(比如物种类型等):
- 先安装ggimage, gridSVG包
install.packages('ggimage')
,install.packages('gridSVG')
install.packages('ggimage')#安装ggimage包
install.packages('gridSVG')
1.作树:
layout选项(即进化树展示类型):
'rectangular', 'dendrogram', 'slanted', 'ellipse', 'roundrect', 'fan', 'circular', 'inward_circular', 'radial', 'equal_angle', 'daylight' ,'ape'
-
横向:
p1<-ggtree(tree,branch.length = "none",layout ='rectangular' , linetype=1,size=1,ladderize = F )
p1
image.png
-
竖向:
p1<-ggtree(tree,branch.length = "none",layout ='dendrogram' , linetype=1,size=1,ladderize = F )
p1
image.png
- 或者:
p1<-ggtree(tree,branch.length = "none",layout ='slanted' , linetype=1,size=1,ladderize = F )
p1
image.png
-
2. ggtree添加图片(注意:先需要在制定文件夹中
以每个分支的名称为文件名准备好.jpg
图): -
图片的准备:
library(ggimage)
library(ggtree)
nwk <- "((((bufonidae, dendrobatidae), ceratophryidae), (centrolenidae, leptodactylidae)), hylidae);"
x = read.tree(text = nwk)
pg<-ggtree(x) + xlim(NA, 7) + ylim(NA, 6.2)
pg1<-pg+ geom_tiplab(aes(image=paste0("C:/Users/Mr.R/Documents/Rfigures/", label, '.jpg')), geom="image", offset=2, align=2, size=.2)
pg1<-pg1+geom_tiplab(geom='label', offset=1, hjust=.5)
pg1<-pg1+geom_image(x=.8, y=5.5, image="C:/Users/Mr.R/Documents/Rfigures/frog.jpg", size=0.21)#添加图上标签,其实可以不加
pg1
得到:
image.png-
不加图上的标签:
library(ggimage)
library(ggtree)
nwk <- "((((bufonidae, dendrobatidae), ceratophryidae), (centrolenidae, leptodactylidae)), hylidae);"
x = read.tree(text = nwk)
pg<-ggtree(x) + xlim(NA, 7) + ylim(NA, 6.2)
pg1<-pg+ geom_tiplab(aes(image=paste0("C:/Users/Mr.R/Documents/Rfigures/", label, '.jpg')), geom="image", offset=2, align=2, size=.2)
pg1<-pg1+geom_tiplab(geom='label', offset=1, hjust=.5)
pg1
得到:
image.png-
那么我们对建立好的nwk进化树也进行标注:
library(ggtree)
nwk <- "((((((((('Roseburia hominis A2-183T (AJ270482)','Roseburia intestinalis strain L1-82T (AJ312385)'),'Roseburia faecis M721T (AY305310)'),'Roseburia cecicola ATCC 33874T(L14676)'),'Roseburia inulinivorans strain A2-194T (AJ270473)'),('Agathobacter ruminis strain JK623T (KT229569)','Agathobacter rectalis ATCC 33656T (NR 074634)')),('Pseudobutyrivibrio xylanovorans strain Mz5T (AJ428548)','Pseudobutyrivibrio ruminis DSM 9787T (X95893)')),('Falcatimonas natans DSM 22923T (AB298726)','Lachnobacterium bovis strain LRC 5382T (AF298663)')),(Strain_2710,('Kineothrix alysoides strain KNHs209T (KX356505)','Eisenbergiella tayi strain B086562T (KF814111)'))),'Coprococcus comes strain ATCC 27758T (EF031542)',(('Coprococcus catus strain VPI-C6-61T (AB038359)','Coprococcus eutactus strain ATCC 27759T (EF031543)'),('Lachnospira pectinoschiza ATCC 49827T (L14675)','Lachnospira multipara ATCC 19207T (AH000856)')));
"
x = read.tree(text = nwk)
pg<-ggtree(x)
pg1<-pg+ geom_tiplab(aes(image=paste0("C:/Users/Mr.R/Documents/Rfigures/", label, '.jpg')), geom="image", offset=11, align=2)
pg1<-pg1+geom_tiplab(geom='label', offset=1, hjust=0.12)
pg1<-pg1+xlim(-11, 21) + ylim(NA, 22)
pg1
得到:
image.png或者可以直接读取nwk文件,然后添加图片:
- 读取进化树nwk文件:
library(ggtree)
tree <- read.newick("tree.nwk" )#读取进化树
-
绘制进化树并进行图片标注:
#加载安装好的包
library(ggimage)
library(ggtree)
pg<-ggtree(tree)#绘制基本进化树并展示
pg
pg1<-pg+ geom_tiplab(aes(image=paste0("C:/Users/Mr.R/Documents/Rfigures/", label, '.jpg')), geom="image", offset=7, align=2) #制定标注图片的位置,offset来制定与树枝的距离
pg1<-pg1+geom_tiplab(geom='label', offset=1, hjust=0.12)
pg1<-pg1+xlim(-1, 19) + ylim(-1, 19) #制定图片的大小
pg1