6 Visual Exploration of Phylogenetic Trees
The ggtree (Yu et al., 2017) supports many ways of manipulating the tree visually, including viewing selected clade to explore large tree (Figure 6.1), taxa clustering (Figure 6.5), rotating clade or tree (Figure 6.6B and 6.8), zoom out or collapsing clades (Figure 6.3A and 6.2), etc.. Details of the tree manipulation functions are summarized in Table 6.1.
Function | Description |
---|---|
collapse | Collapse a selecting clade |
expand | Expand collapsed clade |
flip | Exchange position of 2 clades that share a parent node |
groupClade | Grouping clades |
groupOTU | Grouping OTUs by tracing back to the most recent common ancestor |
identify | Interactive tree manipulation |
rotate | Rotating a selected clade by 180 degrees |
rotate_tree | Rotating circular layout tree by a specific angle |
scaleClade | Zoom in or zoom out selecting clade |
open_tree | Convert a tree to fan layout by specific open angle |
6.1 Viewing Selected Clade
A clade is a monophyletic group that contains a single ancestor and all of its descendants. We can visualize a specifically selected clade via the viewClade()
function as demonstrated in Figure 6.1B. Another solution is to extract the selected clade as a new tree object as described in session 2.5. These functions are developed to help users explore a large tree.
library(ggtree)
nwk <- system.file("extdata", "sample.nwk", package="treeio")
tree <- read.tree(nwk)
p <- ggtree(tree) + geom_tiplab()
viewClade(p, MRCA(p, "I", "L"))
Some of the functions, e.g., viewClade()
, work with clade and accept a parameter of an internal node number. To get the internal node number, users can use the MRCA()
function (as in Figure 6.1) by providing two taxa names. The function will return the node number of input taxa’s most recent common ancestor (MRCA). It works with a tree and graphic (i.e., the ggtree()
output) object. The tidytree package also provides an MRCA()
method to extract information from the MRCA node (see details in session 2.1.3).
6.2 Scaling Selected Clade
The ggtree provides another option to zoom out (or compress) selected clades via the scaleClade()
function. In this way, we retain the topology and branch lengths of compressed clades. This helps to save the space to highlight those clades of primary interest in the study.
tree2 <- groupClade(tree, c(17, 21))
p <- ggtree(tree2, aes(color=group)) + theme(legend.position='none') +
scale_color_manual(values=c("black", "firebrick", "steelblue"))
scaleClade(p, node=17, scale=.1)
If users want to emphasize important clades, they can use the scaleClade()
function by passing a numeric value larger than 1 to the scale
parameter. Then the selected clade will be zoomed in. Users can also use the groupClade()
function to assign selected clades with different clade IDs which can be used to color these clades with different colors as shown in Figure 6.2.
6.3 Collapsing and Expanding Clade
It is a common practice to prune or collapse clades so that certain aspects of a tree can be emphasized. The ggtree supports collapsing selected clades using the collapse()
function as shown in Figure 6.3A.
p2 <- p %>% collapse(node=21) +
geom_point2(aes(subset=(node==21)), shape=21, size=5, fill='green')
p2 <- collapse(p2, node=23) +
geom_point2(aes(subset=(node==23)), shape=23, size=5, fill='red')
print(p2)
expand(p2, node=23) %>% expand(node=21)
Here two clades were collapsed and labeled by the green circle and red square symbolic points. Collapsing is a common strategy to collapse clades that are too large for displaying in full or are not the primary interest of the study. In ggtree, we can expand (i.e., uncollapse) the collapsed branches back with the expand()
function to show details of species relationships as demonstrated in Figure 6.3B.
Triangles are often used to represent the collapsed clade and ggtree also supports it. The collapse()
function provides a “mode” parameter, which by default is “none” and the selected clade was collapsed as a “tip”. Users can specify the mode
to “max” that uses the farthest tip (Figure 6.4A), “min” that uses the closest tip (Figure 6.4B), and “mixed” that uses both (Figure 6.4C).
p2 <- p + geom_tiplab()
node <- 21
collapse(p2, node, 'max') %>% expand(node)
collapse(p2, node, 'min') %>% expand(node)
collapse(p2, node, 'mixed') %>% expand(node)
We can pass additional parameters to set the color and transparency of the triangles (Figure 6.4D).
collapse(p, 21, 'mixed', fill='steelblue', alpha=.4) %>%
collapse(23, 'mixed', fill='firebrick', color='blue')
We can combine scaleClade with collapse
to zoom in/out of the triangles (Figure 6.4E).
scaleClade(p, 23, .2) %>% collapse(23, 'min', fill="darkgreen")
6.4 Grouping Taxa
The groupClade()
function assigns the branches and nodes under different clades into different groups. It accepts an internal node or a vector of internal nodes to cluster clade/clades.
Similarly, the groupOTU()
function assigns branches and nodes to different groups based on user-specified groups of operational taxonomic units (OTUs) that are not necessarily within a clade but can be monophyletic (clade), polyphyletic or paraphyletic. It accepts a vector of OTUs (taxa name) or a list of OTUs and will trace back from OTUs to their most recent common ancestor (MRCA) and cluster them together as demonstrated in Figure 6.5.
A phylogenetic tree can be annotated by mapping different line types, sizes, colors, or shapes of the branches or nodes that have been assigned to different groups.
data(iris)
rn <- paste0(iris[,5], "_", 1:150)
rownames(iris) <- rn
d_iris <- dist(iris[,-5], method="man")
tree_iris <- ape::bionj(d_iris)
grp <- list(setosa = rn[1:50],
versicolor = rn[51:100],
virginica = rn[101:150])
p_iris <- ggtree(tree_iris, layout = 'circular', branch.length='none')
groupOTU(p_iris, grp, 'Species') + aes(color=Species) +
theme(legend.position="right")
We can group taxa at the tree level. The following code will produce an identical figure of Figure 6.5 (see more details described in session 2.2.3).
6.5 Exploring Tree Structure
To facilitate exploring the tree structure, ggtree supports rotating selected clade by 180 degrees using the rotate()
function (Figure 6.6B). Position of immediate descendant clades of the internal node can be exchanged via flip()
function (Figure 6.6C).
p1 <- p + geom_point2(aes(subset=node==16), color='darkgreen', size=5)
p2 <- rotate(p1, 16)
flip(p2, 17, 21)
Most of the tree manipulation functions are working on clades, while ggtree also provides functions to manipulate a tree, including open_tree()
to transform a tree in either rectangular or circular layout to the fan layout, and rotate_tree()
function to rotate a tree for specific angle in both circular or fan layouts, as demonstrated in Figures 6.7 and 6.8.
p3 <- open_tree(p, 180) + geom_tiplab()
print(p3)
rotate_tree(p3, 180)
The following example rotates four selected clades (Figure 6.9). It is easy to traverse all the internal nodes and rotate them one-by-one.
set.seed(2016-05-29)
x <- rtree(50)
p <- ggtree(x) + geom_tiplab()
## nn <- unique(reorder(x, 'postorder')$edge[,1])
## to traverse all the internal nodes
nn <- sample(unique(reorder(x, 'postorder')$edge[,1]), 4)
pp <- lapply(nn, function(n) {
p <- rotate(p, n)
p + geom_point2(aes(subset=(node == n)), color='red', size=3)
})
plot_list(gglist=pp, tag_levels='A', nrow=1)
Figure 6.10 demonstrates the usage of open_tree()
with different open angles.
set.seed(123)
tr <- rtree(50)
p <- ggtree(tr, layout='circular')
angles <- seq(0, 270, length.out=6)
pp <- lapply(angles, function(angle) {
open_tree(p, angle=angle) + ggtitle(paste("open angle:", angle))
})
plot_list(gglist=pp, tag_levels='A', nrow=2)
set.seed(123)
tr <- rtree(50)
p <- ggtree(tr, layout='circular')
angles <- seq(0, 270, length.out=6)
pp <- lapply(angles, function(angle) {
open_tree(p, angle=angle) + ggtitle(paste("open angle:", angle))
})
g <- plot_list(gglist=pp, tag_levels='A', nrow=2)
ggplotify::as.ggplot(g, vjust=-.1,scale=1.1)
Figure 6.11 illustrates a rotating tree with different angles.
Interactive tree manipulation is also possible via the identify()
method (see details described in Chapter 12).
6.6 Summary
A good visualization tool can not only help users to present the data, but it should also be able to help users to explore the data. Data exploration can allow users to better understand the data and also help discover hidden patterns. The ggtree provides a set of functions to allow visually manipulating phylogenetic trees and exploring tree structures with associated data. Exploring data in the evolutionary context may help discover new systematic patterns and generate new hypotheses.