Creating a Publication Quality Phylogeny Using ggtree

A decade ago, circa 2012-2013, I used MEGA5 to infer phylogeny using simple Neighbour-Joining methods, and used the figure generated by MEGA5 to present and publish my results. Later, when I started learning other phylogeny reconstruction methods like Maximum Likelihood (ML) and Bayesian (which does not draw the tree for you), I started to explore many other tree software, including FigTree (which is a handy click-and-annotate phylogeny software). ML phylogeny seems to be the minimum requirement for inferring phylogeny, and using MLSA (multi-locus sequence analysis) is even better. However, how do you generate publication quality phylogeny can be a challenge for beginner.

Also, now a days, phylogeny comes with a large set of meta-data, which are different traits associated with the strain/isolate sample, and having a phylogeny that decorates those meta-data and/or traits, enhance the figure a ton. In almost all cases, I use ggtree in R to draw publication quality phylogeny. ggtree is a versatile package which is build up on ggplot, and uses the Grammar of Graphics philosophy.

`ggtree` has an excellent documentation, but sometime a beginner can get lost into the detail. Here, I’m going to show you how to effectively use ggtree.

Dataset Preparation

This tutorial involves a dataset that contains sequences and metadata from the viral hemorrhagic septicemia virus (VHSV). This virus is a fish novirhabdovirus (negative stranded RNA virus) with an unusually broad host spectra: it has been isolated from more than 80 fish species in locations
around the Northern hemisphere. This dataset contains two files. One contains sequences encoding the viral surface glycoprotein of VHSV in fasta format. The other contains key metadata information in comma separated format (csv).

You can download the sequence alignment and the meta data by using this link. It’s a good idea to check the alignment, or repeat the alignment. You can use the following command to align the file.

$ mafft input.fasta > output.fasta.aln

Let’s make a maximum likelihood tree using the sequence alignment file. For that, we’ll use IQTree. In command line, we’ll use the following in the command line (assuming UNIX/Linux-like environment):

$ iqtree2 -s input.fasta.aln -bb 1000 -alrt 1000

This essentially use 1,000 times bootstrapping method to estimate the branch support. It will generate several output files in the working directory, but we are interested with the file that has .treefile extension. This is basically a Newick file containing final tree structure and has following content:


The meta file look like this:

AU-8-95Rainbow troutFresh waterAUAY546570.11995
CH-FI262BFHRainbow troutFresh waterCHAY546571.11999
DK-1p40RocklingSea waterDKAY546575.11996

It is important that the first column of the meta file contains the same strain names in the fasta file.

First Inspection

Now that we have our Newick tree file, we can load it in R and visualize it right away. Lets do the following:

library(ggtree) # for plotting tree
library(phangorn) # for midpoint root of tree

vshv.tree <- read.tree('Tree_tutorial/Phylogeny_module_example_data.fasta.aln.treefile')

meta <- read.table('Tree_tutorial/Phylogeny_module_tutorial_meta_data.csv', sep=',', header=T)

vshv.tree <- midpoint(vshv.tree)

What we are doing here, is loading necessary packages (ggtree and phagorn), loading tree and meta data, and mid-point rooting the tree. But there is a problem with the read.tree command. Although it is very varsatile Newick file loader, but phylogeny reconstruction software like IQTree, BEAST often adds other tree-related info on the Newick file, and this command can not parse them very well. So, lets modify the script to use advanced IQTree specific parsing command:

library(ggtree) # for plotting tree
library(phangorn) # for midpoint root of tree
library(treeio) # for loading special tree 

vshv.tree <- read.tree('Tree_tutorial/Phylogeny_module_example_data.fasta.aln.treefile')
meta <- read.table('Tree_tutorial/Phylogeny_module_tutorial_meta_data.csv', sep=',', header=T)
vshv.tree <- midpoint(vshv.tree)

vshv.tree <- read.iqtree('Tree_tutorial/Phylogeny_module_example_data.fasta.aln.treefile')
vshv.tree@phylo <- midpoint(vshv.tree@phylo)

# Just tree

Notice how we are using read.iqtree command now and using the phylo object to specifically midpoint the tree, keeping other tree data coming with IQTree untouched. Finally, plotting it will produce something like this:

This looks mundane, nothing exciting. Let’s start using ggtree specific magics to ornament it.

Demystifying ggtree object

To associate the meta-data, which was loaded as a R data-frame, we use the %<+% sign, which allow us to use different columns of the meta data-frame to ornament different part of the tree. For example:

# Lets add tip point

t1 <- ggtree(vshv.tree) %<+% meta +

This will generate the following phylogeny, where tip-points shows the host associated with the meta.

IQTree Newick file comes with a bootstrap support value (--bb option) which has both SH_aLRT and UFboot values. We want to show branches that has high support for both of these (i.e. ≥ 70). But for that, we need to add an indicator for branches that satisfy both condition.

When we define a ggtree object using t1 <- ggtree(vshv.tree) %<+% meta + geom_tippoint(aes(color=Host)), it attaches the meta info as well as tree-branch and tips info coming from original Newick tree-file in the data placeholder, which we can use by t1$data. So let’s update this to have a new column of a variable that satisfy both bootstrap values by the following:

# Circular layout
t1 <- ggtree(vshv.tree, layout="circular") %<+% meta +

# Add bootstrap support

t1$data$bootstrap <- '0'

t1$data[which(t1$data$SH_aLRT >= 70 & t1$data$UFboot  >= 70),]$bootstrap <- '1'

t1 <- t1 + new_scale_color() +
 geom_tree(aes(color=bootstrap == '1')) +
 scale_color_manual(name='Bootstrap', values=setNames( c('black', 'grey'), c(T,F)), guide = "none")


Notice that we have given a circular layout to the tree. We’ve initiated a bootstrap column with a value of 0 and updated it to 1 if it satisfy our condition. The tree now look like this:

The tree starts to look beautiful, but wait, we can do more.

Show Metadata in Heat-Map

A great way to show other meta data associated with your phylogeny is to add a heat-map. But we need to have separate color layers for separate data-columns. Also, we need to use a good color-palette. For drawing heat map, we need ggtree library. So before starting, we need to add the following libraries:

library(ggtree) #for heat-map
library(ggnewscale) #for new color/fill scale
library(viridis) #using special color scale

Let’s define the meta-data columns individually so that we can call that for vizualization. We are interested in the Water column, which is categorical variable and Year column, which contain continuous variable.

# Lets add heat-map specific dataframe

meta.water <-[,'Water'])

colnames(meta.water) <- 'Water'
rownames(meta.water) <- meta$Strain

meta.year <-[,'Year'])
colnames(meta.year) <- 'Year'
rownames(meta.year) <- meta$Strain

For each variable, we have prepared a data.frame containing single column, add coumn- and row-names to it. Now let’s draw the phylogeny:

# Categorical variable
t2 <- gheatmap(t1, meta.water, width=0.2) + 
  scale_fill_viridis_d(option="A", name="Country") +

# Continuous variable
t2 <- gheatmap(t2, meta.year, width=0.1, offset=0.05) + 
  scale_fill_viridis(option="D", name="Year")

Notice that we are passing t1 into first gheatmap command and define new tree t2, and passing the t2 again for second gheatmap command. Also, we are defining a new fill scale using new_scale_fill() after the first gheatmap done its drawing, for the second gheatmap to draw upon. And finally, we have two different viridis fill command for categorical vs continuous variable.

Before finally plotting the t2, let’s also add the Country from meta data as tip-label (because why not use all meta-data variable).

# Plot tiplab2

t2 + geom_tiplab2(aes(label=Country), color="gray40", offset=0.003, size=3)

So now, this is the final look:

I hope you liked this tutorial! You can get the full code from my GitHub repo here.

Other Useful Tree Manipulation

Coming up next

  • getMRCA
  • get_taxa_name
  • extract.clade


One response to “Creating a Publication Quality Phylogeny Using ggtree”

  1. […] in R that can be used for drawing tanglegram. Particularly I wanted to leverage the beautiful ggtree library. After trying out several methods, I found the following approach works well for me so […]

Leave a Reply

Join as a subscriber

Only the posts on data visualization, bioinformatics how to tutorials, web-development, and general comments on research and science will be sent.

Join 11 other subscribers