Skip to main content

Data Integration, Manipulation and Visualization of Phylogenetic Trees

Table of contents

  • Preface
  • About the Author
  • Part I: Tree data input, output, and manipulation
  • 1 Importing Tree with Data
  • 2 Manipulating Tree with Data
  • 3 Exporting tree with data
  • Part II: Tree data visualization and annotation
  • 4 Phylogenetic Tree Visualization
  • 5 Phylogenetic Tree Annotation
  • 6 Visual Exploration of Phylogenetic Trees
  • 7 Plotting tree with data
  • 8 Annotating Tree with Silhouette Images and Sub-plots
  • 9 ggtree for Other Tree-like Objects
  • Part III: ggtree extensions
  • 10 ggtreeExtra for Presenting Data on a Circular Layout
  • 11 Other ggtree Extensions
  • Part IV: Miscellaneous topics
  • 12 ggtree Utilities
  • 13 Gallery of Reproducible Examples
  • Appendix
  • A Frequently Asked Questions
  • B Related Tools
  • C Figures and Tables
  • D Publications of the ggtree Package Suite
  • References

B Related Tools

B.1 MicrobiotaProcess: Convert Taxonomy Table to a treedata Object

Taxonomy (genus, family, …) data are widely used in microbiome or ecology. Hierarchical taxonomies are the tree-like structure that organizes items into subcategories and can be converted to a tree object (see also the phylog object). The MicrobiotaProcess supports converting a taxonomyTable object, defined in the phyloseq package, to a treedata object, and the taxonomic hierarchical relationship can be visualized using ggtree (Figure B.1). When there are taxonomy names that are confused and missing, the as.treedata() method for taxonomyTable objects will complete their upper-level taxonomic information automatically.

library(MicrobiotaProcess)
library(ggtree)

# The original kostic2012crc is a MPSE object
data(kostic2012crc)

taxa <- tax_table(kostic2012crc)
#The rownames (usually is OTUs or other features ) of the taxa will be 
# served as the tip labels if include.rownames = TRUE
tree <- as.treedata(taxa, include.rownames=TRUE)
# Or extract the taxa tree (treedata) with mp_extract_tree, because the 
# taxonomy information is stored as treedata in the MPSE class (kostic2012crc).
# tree <- kostic2012crc %>% mp_extract_tree()

ggtree(tree, layout="circular", size=0.2) +
     geom_tiplab(size=1)
Convert a taxonomyTable object to a treedata object.

FIGURE B.1: Convert a taxonomyTable object to a treedata object.

B.2 rtol: An R Interface to Open Tree API

The rtol (Michonneau et al., 2016) is an R package to interact with the Open Tree of Life data APIs. Users can use it to query phylogenetic trees and visualize the trees with ggtree to explore species relationships (Figure B.2).

## example from: https://github.com/ropensci/rotl
library(rotl)
apes <- c("Pongo", "Pan", "Gorilla", "Hoolock", "Homo")
(resolved_names <- tnrs_match_names(apes))
##   search_string unique_name approximate_match ott_id
## 1         pongo       Pongo             FALSE 417949
## 2           pan         Pan             FALSE 417957
## 3       gorilla     Gorilla             FALSE 417969
## 4       hoolock     Hoolock             FALSE 712902
## 5          homo        Homo             FALSE 770309
##   is_synonym          flags number_matches
## 1      FALSE                             2
## 2      FALSE sibling_higher              2
## 3      FALSE sibling_higher              1
## 4      FALSE                             1
## 5      FALSE sibling_higher              1
tr <- tol_induced_subtree(ott_ids = ott_id(resolved_names))
ggtree(tr) + geom_tiplab() + xlim(NA, 5)
Get an induced subtree from the big Open Tree.

FIGURE B.2: Get an induced subtree from the big Open Tree.

B.3 Convert a ggtree object to a plotly object

One way to make a quick interactive phylogenetic tree is using ggtree with the plotly package. The ggplotly() is able to convert ggtree object to a plotly object. Note that the ggtree package also supports interactive manipulation of the phylogenetic tree via the identify() method.

# example from https://twitter.com/drandersgs/status/965996335882059776

# LOAD LIBS ---------------------------------------------------------------
library(ape)
library(ggtree)
library(plotly)
# CREATE A TREE -------------------------------------------------------------
n_samples <- 20
n_grp <- 4
tree <- ape::rtree(n = n_samples)
# CREATE SOME METADATA ----------------------------------------------------
id <- tree$tip.label
set.seed(42)
grp <- sample(LETTERS[1:n_grp], size = n_samples, replace = T)
dat <- tibble::tibble(id = id,
                      grp = grp)
# PLOT THE TREE -----------------------------------------------------------
p1 <- ggtree(tree)
metat <- p1$data %>%
  dplyr::inner_join(dat, c('label' = 'id'))
p2 <- p1 +
  geom_point(data = metat,
             aes(x = x,
                 y = y,
                 colour = grp,
                 label = id))
plotly::ggplotly(p2)
Interactive phylogenetic tree by combining ggtree with plotly.

FIGURE B.3: Interactive phylogenetic tree by combining ggtree with plotly.

B.4 Comic (xkcd-like) phylogenetic tree

library(htmltools)    
library(XML)    
library(gridSVG)    
library(ggplot2)    
library(ggtree)
library(comicR)    

p <- ggtree(rtree(30), layout="circular")  +
    geom_tiplab(aes(label=label), color="purple")
print(p)
svg <- grid.export(name="", res=100)$svg
## need to convert it to png or pdf for pdfbook
tagList(
    tags$div(
             id = "ggtree_comic",
             tags$style("#ggtree_comic text {font-family:Comic Sans MS;}"),
             HTML(saveXML(svg)),
             comicR("#ggtree_comic", ff=5)
         )
) # %>% html_print
t10t24t1t27t5t26t19t25t20t13t28t23t29t22t17t11t16t12t7t21t3t14t4t15t9t18t2t30t6t8

B.5 Print ASCII-art Rooted Tree

library(data.tree)
tree <- rtree(10)
d <- as.data.frame(as.Node(tree))
names(d) <- NULL
print(d, row.names=FALSE)
                       
 11                    
  ¦--12                
  ¦   ¦--13            
  ¦   ¦   ¦--t4        
  ¦   ¦   °--t7        
  ¦   °--14            
  ¦       ¦--15        
  ¦       ¦   ¦--t1    
  ¦       ¦   °--16    
  ¦       ¦       ¦--t6
  ¦       ¦       °--t5
  ¦       °--t8        
  °--17                
      ¦--t3            
      °--18            
          ¦--19        
          ¦   ¦--t10   
          ¦   °--t9    
          °--t2        

It is neat to print ASCII-art of the phylogeny. Sometimes, we don’t want to plot the tree, but just take a glance at the tree structure without leaving the focus from the R console. However, it is not a good idea to print the whole tree as ASCII text if the tree is large. Sometimes, we just want to look at a specific portion of the tree and its immediate relatives. In this scenario, we can use treeio::tree_subset() function (see session 2.4) to extract selected portion of a tree. Then we can print ASCII-art of the tree subset to explore the evolutionary relationship of the species of our interest in the R console.

The ggtree supports parsing tip labels as emoji to create phylomoji. With the data.tree and emojifont packages, we can also print phylomoji as ASCII text (Figure B.4).

library(data.tree)
library(emojifont)

tt <- '((snail,mushroom),(((sunflower,evergreen_tree),leaves),green_salad));'
tree <- read.tree(text = tt)
tree$tip.label <- emoji(tree$tip.label)
d <- as.data.frame(as.Node(tree))
names(d) <- NULL
print(d, row.names=FALSE)
Print phylomoji as ASCII text.

FIGURE B.4: Print phylomoji as ASCII text.

Another way to print ASCII-art of phylogeny is to use the ascii() device defined in the devout package. Here is an example:

library(devout)
ascii(width=80)
ggtree(rtree(5))
invisible(dev.off())
                                                                                
                             #########################                          
                             #                                                  
                            ##                                                  
                            ##                                                  
                            #####################                               
   ##########################                                                   
   #                        #                                                   
   #                        #                                                   
   #                        ##########                                          
   #                                                                            
   #                                                                            
   #                                                                            
   #                                   ###############                          
   #                                   #                                        
   #####################################                                        
                                       #                                        
                                       #####################################    
                                                                                
                                                                                 

B.6 Zoom in on the Selected Portion

In addition to using viewClade() function, users can use the ggforce package to zoom in on a selected clade (Figure B.5).

set.seed(2019-08-05)
x <- rtree(30)
nn <- tidytree::offspring(x, 43, self_include=TRUE)
ggtree(x) + ggforce::facet_zoom(xy = node %in% nn)
Zoom in on a selected clade.

FIGURE B.5: Zoom in on a selected clade.

B.7 Tips for Using ggtree with ggimage

The ggtree supports annotating a tree with silhouette images via the ggimage package. The ggimage provides the grammar of graphic syntax to work with image files. It allows processing images on the fly via the image_fun parameter, which accepts a function to process magick-image objects (Figure B.6). The magick package provides several functions, and these functions can be combined to perform a particular task.

B.7.1 Example 1: Remove background of images

library(ggimage)

imgdir <- system.file("extdata/frogs", package = "TDbook")

set.seed(1982)
x <- rtree(5)
p <- ggtree(x) + theme_grey()
p1 <- p + geom_nodelab(image=paste0(imgdir, "/frog.jpg"), 
                        geom="image", size=.12) + 
      ggtitle("original image")
p2 <- p + geom_nodelab(image=paste0(imgdir, "/frog.jpg"), 
            geom="image", size=.12,
            image_fun= function(.) magick::image_transparent(., "white")) + 
      ggtitle("image with background removed")
plot_grid(p1, p2, ncol=2)
Remove image background. Plotting silhouette images on a phylogenetic tree with background not removed (A) and removed (B).

FIGURE B.6: Remove image background. Plotting silhouette images on a phylogenetic tree with background not removed (A) and removed (B).

B.7.2 Example 2: Plot tree on a background image

The geom_bgimage() adds a layer of the image and puts the layer to the bottom of the layer stack. It is a normal layer and doesn’t change the structure of the output ggtree object. Users can add annotation layers without the background image layer (Figure B.7).

ggtree(rtree(20), size=1.5, color="white") + 
  geom_bgimage('img/blackboard.jpg') + 
  geom_tiplab(color="white", size=5, family='xkcd')
Use an image file as a tree background.

FIGURE B.7: Use an image file as a tree background.

B.8 Run ggtree in Jupyter Notebook

If you have Jupyter notebook installed on your system, you can install IRkernel with the following command in R:

install.packages("IRkernel")
IRkernel::installspec()

Then you can use ggtree and other R packages in the Jupyter notebook (Figure B.8). Here is a screenshot of recreating Figure 8.5 in the Jupyter notebook.

ggtree in Jupyter notebook. Running ggtree in Jupyter notebook via R kernel.

FIGURE B.8: ggtree in Jupyter notebook. Running ggtree in Jupyter notebook via R kernel.

A Frequently Asked Questions
C Figures and Tables

On this page

  • B Related Tools
  • B.1 MicrobiotaProcess: Convert Taxonomy Table to a treedata Object
  • B.2 rtol: An R Interface to Open Tree API
  • B.3 Convert a ggtree object to a plotly object
  • B.4 Comic (xkcd-like) phylogenetic tree
  • B.5 Print ASCII-art Rooted Tree
  • B.6 Zoom in on the Selected Portion
  • B.7 Tips for Using ggtree with ggimage
    • B.7.1 Example 1: Remove background of images
    • B.7.2 Example 2: Plot tree on a background image
  • B.8 Run ggtree in Jupyter Notebook

"Data Integration, Manipulation and Visualization of Phylogenetic Trees" was written by Guangchuang Yu. It was last built on 2022-07-13.

This book was built by the bookdown R package.