Skip to content

reproduce OutbreakTools example using ggtree

Guangchuang Yu edited this page Sep 2, 2015 · 9 revisions

OutbreakTools example

This example is presented in vignette of OutbreakTools.

library(OutbreakTools)
data(FluH1N1pdm2009)
attach(FluH1N1pdm2009)

x <- new("obkData", individuals = individuals, dna = FluH1N1pdm2009$dna,
      dna.individualID = samples$individualID, dna.date = samples$date,
      trees = FluH1N1pdm2009$trees)

p <- plotggphy(x, ladderize = TRUE, branch.unit = "year",
               tip.color = "location", tip.size = 3, tip.alpha = 0.75)

In this figure, it use Date as x-axis and annotate the tree with Location. In epidemic time and location that isolate the virus is important information and plotggphy is designed for viewing such information.

reproduce using ggtree

In ggtree, it's very easy to reproduce such figure by adding a layer of location information.

library(ggtree)
tree <- x@trees[[1]]
g <- ggtree(tree, right=T, mrsd="2009-09-30") + theme_tree2()
g <- g + theme(panel.grid.major = element_line(color="grey"), 
               panel.grid.major.y=element_blank())

## extract location info
meta.df <- x@dna@meta
meta.df <- data.frame(taxa=rownames(meta.df), meta.df)
loc <- x@individuals
loc <- data.frame(individualID=rownames(loc), loc)
meta_loc <- merge(meta.df, loc, by="individualID")
meta_loc <- meta_loc[,-1]

## attach additional information to tree view via %<+% operator 
## that was created in ggtree
g <- g %<+% meta_loc

g + geom_tippoint(aes(color=location), size=3, alpha=.75) + 
    theme(legend.position="right") + 
      scale_color_brewer("location", palette="Spectral")

Clone this wiki locally