To view a phylogenetic tree, we first need to parse the tree file into R. The ggtree package supports many file formats including output files of commonly used software packages in evolutionary biology. For more details, plase refer to the Tree Data Import vignette.

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

Viewing a phylogenetic tree with ggtree

The ggtree package extends ggplot2 package to support viewing phylogenetic tree. It implements geom_tree layer for displaying phylogenetic tree, as shown below:

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

The function, ggtree, was implemented as a short cut to visualize a tree, and it works exactly the same as shown above.

ggtree takes all the advantages of ggplot2. For example, we can change the color, size and type of the lines as we do with ggplot2.

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

By default, the tree is viewed in ladderize form, user can set the parameter ladderize = FALSE to disable it.

ggtree(tree, ladderize=FALSE)

The branch.length is used to scale the edge, user can set the parameter branch.length = "none" to only view the tree topology (cladogram) or other numerical variable to scale the tree (e.g. dN/dS, see also in Tree Annotation vignette).

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

Layout

Currently, ggtree supports several layouts, including:

for Phylogram (by default) and Cladogram if user explicitly setting branch.length='none'. ggtree also supports unrooted layout.

Phylogram

rectangular

ggtree(tree) + ggtitle("(Phylogram) rectangular layout")

slanted

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

circular

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

fan

ggtree(tree, layout="fan", open.angle=180) + ggtitle("(Phylogram) circular layout")

Cladogram

rectangular

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

slanted

ggtree(tree, layout="slanted", branch.length='none') + ggtitle("(Cladogram) slanted layout")

circular

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

fan

ggtree(tree, layout="fan", open.angle=180, branch.length="none") + ggtitle("(Cladogram) circular layout")

Unrooted

Unrooted layout was implemented by the equal-angle algorithm that described in Inferring Phylogenies1.

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

Time-scaled tree

A phylogenetic tree can be scaled by time (time-scaled tree) by specifying the parameter, mrsd (most recent sampling date).

tree2d <- read.beast(system.file("extdata", "twoD.tree", package="ggtree"))
ggtree(tree2d, mrsd = "2014-05-01") + theme_tree2()

Two dimensional tree

ggtree implemented two dimensional tree. It accepts parameter yscale to scale the y-axis based on the selected tree attribute. The attribute should be numerical variable. If it is character/category variable, user should provides a name vector of mapping the variable to numeric by passing it to parameter yscale_mapping.

ggtree(tree2d, mrsd = "2014-05-01",
       yscale="NGS", yscale_mapping=c(N2=2, N3=3, N4=4, N5=5, N6=6, N7=7)) +
           theme_classic() + theme(axis.line.x=element_line(), axis.line.y=element_line()) +
               theme(panel.grid.major.x=element_line(color="grey20", linetype="dotted", size=.3),
                     panel.grid.major.y=element_blank()) +
                         scale_y_continuous(labels=paste0("N", 2:7))