欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

如何用R语言ggtree轻松画出进化树图指南

最编程 2024-02-17 19:43:41
...

绘制进化树的方法有很多,入门的MEGA。美化比较好用的,也是我经常用的工具如iTOL,evolview等。

今天测试了另外一个别的paper和笔记中经常用的的一个R包:ggtree,据说可以轻轻松松绘制高端进化树。所以闲暇之余就测试了一下。

======安装========

source("https://bioconductor.org/biocLite.R")

biocLite("ggtree")

或者:

# Bioconductor version

library(BiocManager)

BiocManager::install("ggtree")

 

# github version

devtools::install_github("YuLab-SMU/ggtree")

===例子测试======

library(ggtree)

library(treeio)

library(tidytree)

以这个test.tree作为测试:

tree <- read.newick("test.tree",node.label = "support")

注:也可以读取其它格式的tree文件

read.beast()       ## for parsing output of BEASE

read.codeml()      ## for parsing output of CODEML (rst andmlc files)

read.codeml_mlc()  ## for parsing mlc file (output of CODEML)

read.hyphy()       ## for parsing output of HYPHY

read.jplace()      ## for parsing jplace file includingoutput from EPA and pplacer

read.nhx()         ## for parsing NHX file includingoutput from PHYLODOG and RevBayes

read.paml_rst()    ## for parsing rst file (output of BASEMLand CODEML)

read.r8s()         ## for parsing output of r8s

read.raxml()       ## for parsing output of RAxML

//显示进化树

ggtree(tree)

ggtree(tree, color="firebrick", size=1, linetype="dotted")

# 默认情况下,树以ladderize形式查看,可以设置参数ladderize= FALSE以禁用它

ggtree(tree, ladderize=FALSE)

//设置branch.length="none"只查看树的拓扑结构

ggtree(tree,branch.length="none")

//layout参数设置不同的展示布局,默认为“rectangular”

ggtree(tree, layout="rectangular") +ggtitle("rectangular (phylogram)")

ggtree(tree, layout="rectangular",branch.length= "none") + ggtitle("rectangular (cladogram)")

ggtree(tree, layout="slanted") +ggtitle("slanted (phylogram)")

ggtree(tree, layout="circular") +ggtitle("circular (phylogram)")

ggtree(tree, layout="circular",branch.length ="none") + ggtitle("circular (cladogram)")

ggtree(tree, layout="fan", open.angle=160) +ggtitle("fan (phylogram)")

ggtree(tree, layout="equal_angle") +ggtitle("equal angle (unrooted)")

ggtree(tree, layout="daylight") +ggtitle("daylight (unrooted)")

ggtree(tree, layout="daylight",branch.length ="none") + ggtitle("daylight (cladogram)")

显示树的尺度和时间标度

ggtree(tree) + geom_treescale()

ggtree(tree) + theme_tree2()

ggtree(tree) + geom_treescale(fontsize=8, linesize=1, offset=-0.5) + theme_tree2()

显示树的节点和节点标签

# 使用geom_nodepoint,geom_tippoint或者geom_point添加内部节点

 ggtree(tree) + geom_point(aes(shape=isTip, color=isTip), size=3)

例如通过控制node,tip或者point来一起控制节点的美观:

ggtree(tree) + geom_nodepoint(color="red", alpha=1/4, size=5)+geom_tippoint(color="blue", alpha=1/4, size=10,shape=1)

使用geom_nodelab和geom_tiplab显示节点标签

ggtree(tree) + geom_nodepoint(color="blue",alpha=1/4, size=5) + geom_tiplab(size=3, color="red")

ggtree(tree) + geom_tiplab(size=3, color="red")  + geom_nodelab(aes(subset=!isTip,label=node),hjust=-.3,color="blue")

设置不同的主题

ggtree(tree, color="red") +theme_tree("black")  //就是换背景色

显示树上所有节点的序号

ggtree(tree) +geom_text2(aes(label=node),hjust=-.3,color="red")

 

当然,也可以显示内部节点的序号

ggtree(tree) +geom_text2(aes(subset=!isTip,label=node),hjust=-.3,color="red")

给指定节点的clade添加标签

ggtree(tree) + geom_text(aes(label=node),hjust=-.3,color="red") + geom_cladelabel(node=19,label="Group A",barsize = 2,color="blue") +geom_cladelabel(node=15,label="Group B",barsize = 2,color="green") 

高亮指定节点所对应的区域

ggtree(tree) + geom_text(aes(label=node),hjust=-.3,color="red") + geom_hilight(node=15,fill = "green",alpha = 0.6)+geom_cladelabel(node=15,label="Group B",barsize = 2,color="green") 

在两个节点之间添加线段和标记

ggtree(tree) + geom_text(aes(label=node),hjust=-.3,color="red") + geom_strip(17,15,barsize = 1,color="blue",label="A")

====其它数据和进化树关联起来=====

Kat Holt开发了plotTree(https://github.com/katholt/plotTree)包含了R和python两个版本,可以支持把一些相关的信息(比如taxa info,SNP位点,matrix等)和进化树关联起来,这样可以使图片更好看。

下载:

https://github.com/katholt/plotTree

然后导入包:

source("plotTree.R")

plotTree(tree="tree.nwk")

plotTree(tree="tree.nwk",ancestral.reconstruction=F,tip.colour.cex=1,cluster=T,tipColours=c("black","purple2","skyblue2","grey"),lwd=1,infoFile="info.csv",colourNodesBy="location",treeWidth=10,infoWidth=10,infoCols=c("name","location","year"))

//加入一些额外的描述信息,下为info.csv信息

plotTree(tree="tree.nwk",heatmapData="pan.csv",ancestral.reconstruction=F,tip.colour.cex=1,cluster=T,tipColours=c("black","purple2","skyblue2","grey"),lwd=1,infoFile="info.csv",colourNodesBy="location",treeWidth=5,dataWidth=20,infoCols=NA)  

//也可以加入heatmap数据

pan.csv里数据

plotTree(tree="tree.nwk",heatmapData="res_genes.csv",ancestral.reconstruction=F,tip.colour.cex=1,cluster=F,heatmap.colours=c("white","grey","seagreen3","darkgreen","green","brown","tan","red","orange","pink","magenta","purple","blue","skyblue3","blue","skyblue2"),tipColours=c("black","purple2","skyblue2","grey"),lwd=1,infoFile="info.csv",colourNodesBy="location",treeWidth=10,dataWidth=10,infoCols=c("name","year"),infoWidth=8)

//基因信息,其实也相当于离散型的heatmap,下面为res_genes.csv内容

plotTree(tree="tree.nwk",barData="bar.csv")

//也可以通过barData添加bar图

当然很好也可以利用facet_plot实现。

本文使用 文章同步助手 同步