When plotting phylogenetic trees it is often useful to collapse and/or label clades for more informative figures. ggtree
can do both, and this can be very useful for drawing large trees with taxonomic context and to avoid tiny+unreadable tip labels. Here’s a brief walkthrough:
Lets use the caniform phylogeny bundled with geiger
and work with genera for the labels and collapsed nodes. This example is for genera, simply obtained by splitting the scientific names at the tree tips, but we can join the tabular representation of our trees with any other data containing higher taxonomic information (tribes, families, orders, etc).
It is easy to split the scientific names and get the genera (or do other joins) if we work with a tabular representation of the phylo object.
A few extra steps to discard monotypic genera and prepare a list of nodes needed for the next stage:
Our two outputs look like this (a randomly chosen example):
These various wrangling steps ultimately feed a wrapper function that gets the most recent common ancestors (MRCA) for our clades of interest. I took advantage of purrr::map2
to iterate through the two lists.
The type-specific map produces this nice tibble:
The tree looks like this:
To collapse a single node in a ggtree
object, the collapse()
function has an argument for the node of interest, and options for how to draw the newly collapsed branches. mode = max
and align = TRUE
is perhaps the most popular option for this.
Node 140 is for the genus Canis.
Rather than repeating this call for all the nodes of interest using for loops, let’s reduce
, with the canif_tree
ggtree object as the value for .init
(so we collapse clades iteratively starting from here).
The syntax can be confusing at first but I learned how to use reduce with these two posts, by June Choe:
and Maëlle Salmon:
If we want to label the genera using geom_cladelab
instead, we can use reduce2
and some nice anonymous formula notation. After that add some minor tweaks to the space to let the labels show.
It is also possible to label a collapsed tree in the same way but the output seems off (the bars do not show up no matter what I’ve tried and the label allignment seems to be relative to the max node so it does not look too centered to me).
Hope this helps!