INTRODUCTION

R package for visualization and annotations of phylogenetic trees

library(ggtree)
## ggtree v1.14.2  For help: https://guangchuangyu.github.io/software/ggtree
## 
## If you use ggtree in published research, please cite the most appropriate paper(s):
## 
## <U+25C9> Guangchuang Yu, David Smith, Huachen Zhu, Yi Guan, Tommy Tsan-Yuk Lam. ggtree: an R package for visualization and annotation of phylogenetic trees with their covariates and other associated data. Methods in Ecology and Evolution 2017, 8(1):28-36, doi:10.1111/2041-210X.12628
## 
## <U+25C9> Guangchuang Yu, Tommy Tsan-Yuk Lam, Huachen Zhu, Yi Guan. Two methods for mapping and visualizing associated data on phylogeny using ggtree. Molecular Biology and Evolution 2018, accepted. doi: 10.1093/molbev/msy194
library(ggplot2)
#library(treeio)

To view a phylogenetic tree, we first need to parse the tree file into R. The ggtree (Yu et al. 2017) package supports many file formats via the treeio package, including output files of commonly used software packages in evolutionary biology.

TREE VISUALIZATION

set.seed(11282018)
tree <- rtree(50)
ggtree(tree)

can use ggplot2 by implementing a geom_tree layer

ggplot(tree, aes(x, y)) + geom_tree() + theme_tree()

Tree layout

Rectangular (by default) slanted circular fan

Phylogram and Cladogram (in phylogram branch length represents time)

ggtree(tree, layout="slanted") # phylogram

ggtree(tree, layout="circular") # phylogram

ggtree(tree, layout="fan", open.angle=120) # phylogram

ggtree(tree, layout="equal_angle") # unrooted

ggtree(tree, layout="daylight") # unrooted
## Average angle change [1] 0.133843760721467
## Average angle change [2] 0.0530980905146852
## Average angle change [3] 0.0309079136251543

ggtree(tree, branch.length='none') # rectangular cladogram

ggtree(tree, branch.length='none', layout='circular') # circular cladogram

ggtree(tree, layout="daylight", branch.length='none') # daylight cladogram
## Average angle change [1] 0.133843760721467
## Average angle change [2] 0.0530980905146852
## Average angle change [3] 0.0309079136251543

Displaying tree scale (evolution distance) using geom_treescale layer

ggtree(tree) + geom_treescale()

Parameters in geom_treescale

  • x and y for tree scale position
  • width for the length of the tree scale
  • fontsize for the size of the text
  • linesize for the size of the line
  • offset for relative position of the line and the text
  • color for color of the tree scale
ggtree(tree) + geom_treescale(x=0, y=12, width=6, color='red')

#ggtree(tree) + geom_treescale(x=0, y=0, width=4, color='red')
ggtree(tree) + geom_treescale(fontsize=8, linesize=2, offset=-1)

Displaying nodes/ tips

# use geom_nodepoint, geom_tippoint or geom_point

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

p <- ggtree(tree) + geom_nodepoint(color="#b5e521", alpha=1/4, size=10)
p + geom_tippoint(color="#FDAC4F", shape=8, size=3)

For circular and unrooted layout, ggtree supports rotating node labels according to the angles of the branches.

ggtree(tree, layout="circular") + geom_tiplab(aes(angle=angle), color='blue')

themes

ggtree(rtree(30), color="red") + theme_tree("steelblue")

ggtree(rtree(20), color="white") + theme_tree("black")

list of trees using multiPhylo; load ggplot2 for facet_wrap

trees <- lapply(c(10, 20, 40), rtree)
class(trees) <- "multiPhylo"
ggtree(trees) + facet_wrap(~.id, scale="free") + geom_tiplab()

zoom on a portion of a tree

library("ape")
## 
## Attaching package: 'ape'
## The following object is masked from 'package:ggtree':
## 
##     rotate
data(chiroptera)
library("ggtree")
gzoom(chiroptera, grep("Plecotus", chiroptera$tip.label))

TREE MANIPULATION

nwk <- system.file("extdata", "sample.nwk", package="treeio")
tree <- read.tree(nwk)
ggtree(tree)

ggtree(tree) + geom_text2(aes(subset=!isTip, label=node), hjust=-.5) + geom_tiplab()

View clade

p <- ggtree(tree)
viewClade(p+geom_tiplab(), node=21)

Group clades

use internal node or vector of internal nodes to cluster clades

tree <- groupClade(tree, .node=21)
ggtree(tree, aes(color=group, linetype=group))

#ggtree(tree, aes(color=group, linetype=group)) + geom_text2(aes(label=node))

Highlight selected taxa

tree <- groupClade(tree, .node=c(21, 17))
ggtree(tree, aes(color=group, linetype=group)) + geom_tiplab(aes(subset=(group==1)))

#ggtree(tree, aes(color=group, linetype=group)) + geom_tiplab(aes(subset=(group==2)))
# ggtree(tree, aes(color=group, linetype=group)) + geom_tiplab(aes(label=node))

Collapse a clade

cp <- collapse(p, node=21)
cp + geom_point2(aes(subset=(node == 21)), size=5, shape=23, fill="steelblue")

Expand collapsed clade

cp %>% expand(node=21)

p1 <- ggtree(tree)
p2 <- collapse(p1, 21) + geom_point2(aes(subset=(node==21)), size=5, shape=23, fill="blue")
p3 <- collapse(p2, 17) + geom_point2(aes(subset=(node==17)), size=5, shape=23, fill="red")
p4 <- expand(p3, 17)
p5 <- expand(p4, 21)

library(cowplot)
## 
## 
## *******************************************************
## Note: cowplot does not change the default ggplot2 theme
## anymore. To recover the previous behavior, execute:
##   theme_set(theme_cowplot())
## *******************************************************
plot_grid(p1, p2, p3, p4, p5, ncol=5)

Flip clades - should share a same parent

plot_grid(p, flip(p, 17, 21), ncol=2)

Rotate a circular tree

for (angle in seq(0, 270, 30)) {
  print(rotate_tree(p, angle) + ggtitle(paste("rotate angle:", angle)))
}

TREE ANNOTATION

Annotate a clade

tree <- rtree(30)
p <- ggtree(tree) + xlim(NA, 6)
#print(p) + geom_text2(aes(label=node))
p + geom_cladelabel(node=42, label="test label") +
  geom_cladelabel(node=55, label="another clade")

Adjust position

Use align = TRUE, to align the clade label, and use the parameter, offset, to adjust the position.

p + geom_cladelabel(node=42, label="test label", align=TRUE, offset=.3) +
  geom_cladelabel(node=55, label="another clade", align=TRUE, offset=.3)

change color

p + geom_cladelabel(node=42, label="test label", align=T, color='red') +
  geom_cladelabel(node=55, label="another clade", align=T, color='blue')

change angle

p + geom_cladelabel(node=42, label="test label", align=T, angle=270, hjust='center', offset.text=.5) +
  geom_cladelabel(node=55, label="another clade", align=T, angle=45)

change the size of bar and text

p + geom_cladelabel(node=42, label="test label", align=T, angle=270, hjust='center', offset.text=.5, barsize=1.5) +
  geom_cladelabel(node=55, label="another clade", align=T, angle=45, fontsize=8)

geom label can be used to label the text

p + geom_cladelabel(node=55, label="another clade", align=T, geom='label', fill='lightblue')

annotate clades for unrooted trees using geom_clade2

pg <- ggtree(tree, layout="daylight")
## Average angle change [1] 0.195496208341812
## Average angle change [2] 0.0715680268737278
## Average angle change [3] 0.0330818367838394
pg + geom_cladelabel2(node=42, label="test label", angle=10) +
  geom_cladelabel2(node=55, label="another clade", angle=305)

Highlight clades

ggtree(tree) + geom_hilight(node=21, fill="steelblue", alpha=.6) +
  geom_hilight(node=54, fill="darkgreen", alpha=.6)

For more details on ggtree, please refer to the webiste [http://bioconductor.org/packages/release/bioc/html/ggtree.html]

THANK YOU