### 加载所需R包
# if (!requireNamespace("BiocManager", quietly = TRUE))
# install.packages("BiocManager")
# BiocManager::install("ggtree", version = "3.8")
library(ggplot2)
library(ggtree)
### 设置工作路径
setwd("/Users/liang/Desktop/")
# 清除当前环境中的变量
rm(list=ls())
# 使用read.newick函数读取nwk格式的树文件
# nodel.label="support"参数在读取nwk格式的树文件时可以存入bootstrap值
tree <- read.newick("RAxML_bipartitions.concatenation_out.nwk",node.label = "support")
ggtree(tree)

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

image.png
# 默认情况下,树以ladderize形式查看,可以设置参数ladderize = FALSE以禁用它
ggtree(tree, ladderize=FALSE)

image.png
设置branch.length="none"只查看树的拓扑结构
ggtree(tree, branch.length="none")

image.png
# 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)")

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png

image.png
显示树的尺度和时间标度
ggtree(tree) + geom_treescale()
ggtree(tree) + theme_tree2()
ggtree(tree) + geom_treescale(fontsize=8, linesize=1, offset=-0.5) + theme_tree2()

image.png

image.png

image.png
# 显示树的节点和节点标签
# 使用geom_nodepoint,geom_tippoint或者geom_point添加内部节点
ggtree(tree) + geom_point(aes(shape=isTip, color=isTip), size=3)

image.png
ggtree(tree) + geom_nodepoint(color="#b5e521", alpha=1/4, size=10)

image.png
ggtree(tree) + geom_tippoint(color="#FDAC4F", shape=8, size=3)

image.png
ggtree(tree) + geom_nodepoint(color="#b5e521", alpha=1/4, size=10)+ geom_tippoint(color="#FDAC4F", shape=8, size=3)

image.png
# 使用geom_nodelab和geom_tiplab显示节点标签
ggtree(tree) + geom_nodepoint(color="#b5e521", alpha=1/4, size=10) + geom_tiplab(size=3, color="purple") + xlim(0,0.5)

image.png
ggtree(tree) + geom_tiplab(size=3, color="purple") + xlim(0,0.5) + geom_nodelab(aes(subset=!isTip,label=node),hjust=-.3,color="red")

image.png
# 设置不同的主题
ggtree(tree, color="red") + theme_tree("steelblue")

image.png
ggtree(tree, color="purple") + theme_tree("black")

image.png
# 显示树上所有节点的序号
ggtree(tree) + geom_text2(aes(label=node),hjust=-.3,color="red")

image.png
# 只显示内部节点的序号
ggtree(tree) + geom_text2(aes(subset=!isTip,label=node),hjust=-.3,color="red")

image.png
# 给指定节点的clade添加标签
ggtree(tree) + geom_text(aes(label=node),hjust=-.3,color="red") + geom_cladelabel(node=19,label="A",barsize = 1,color="blue")

image.png
# 高亮指定节点所对应的区域
ggtree(tree) + geom_text(aes(label=node),hjust=-.3,color="red") + geom_hilight(node=16,fill = "blue",alpha = 0.6)

image.png
ggtree(tree) + geom_hilight(node=19,fill = "blue",alpha = 0.6) + geom_hilight(node=14,fill = "red",alpha = 0.6)

image.png
# 在两个节点之间添加线段和标记
ggtree(tree) + geom_text(aes(label=node),hjust=-.3,color="red") + geom_strip(13,19,barsize = 2,color="red",label="A")

image.png
# 添加bootstrap值和节点标记
ggtree(tree,color="black",linetype=1,size=1.5,ladderize = T) + xlim(0,0.6) +
geom_tiplab(hjust = -0.05,size=5,fontface="italic") +
geom_text2(aes(subset=!isTip,label=support,color=support, hjust=-0.5),size=4) +
geom_point2(aes(subset=!isTip),fill="red",shape=21,size=4) +
scale_color_gradient(high='red', low='darkgreen') + theme(legend.position='right')

image.png
sessionInfo()
## R version 3.5.1 (2018-07-02)
## Platform: x86_64-apple-darwin15.6.0 (64-bit)
## Running under: OS X El Capitan 10.11.3
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib
##
## locale:
## [1] zh_CN.UTF-8/zh_CN.UTF-8/zh_CN.UTF-8/C/zh_CN.UTF-8/zh_CN.UTF-8
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] bindrcpp_0.2.2 ggtree_1.12.7 ggplot2_3.0.0
##
## loaded via a namespace (and not attached):
## [1] Rcpp_0.12.18 pillar_1.3.0 compiler_3.5.1 plyr_1.8.4
## [5] bindr_0.1.1 tools_3.5.1 digest_0.6.16 jsonlite_1.5
## [9] tidytree_0.1.9 evaluate_0.11 tibble_1.4.2 gtable_0.2.0
## [13] nlme_3.1-137 lattice_0.20-35 pkgconfig_2.0.2 rlang_0.2.2
## [17] rstudioapi_0.7 rvcheck_0.1.0 parallel_3.5.1 yaml_2.2.0
## [21] treeio_1.4.3 withr_2.1.2 dplyr_0.7.6 stringr_1.3.1
## [25] knitr_1.20 rprojroot_1.3-2 grid_3.5.1 tidyselect_0.2.4
## [29] glue_1.3.0 R6_2.2.2 rmarkdown_1.10 tidyr_0.8.1
## [33] purrr_0.2.5 magrittr_1.5 backports_1.1.2 scales_1.0.0
## [37] htmltools_0.3.6 assertthat_0.2.0 colorspace_1.3-2 ape_5.1
## [41] labeling_0.3 stringi_1.2.4 lazyeval_0.2.1 munsell_0.5.0
## [45] crayon_1.3.4
网友评论