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)
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)
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)
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
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)
Another way to print ASCII-art of phylogeny is to use the ascii()
device defined in the devout package. Here is an example:
#########################
#
##
##
#####################
##########################
# #
# #
# ##########
#
#
#
# ###############
# #
#####################################
#
#####################################
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)
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)
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')
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.