facet_plot: a general solution to associate data with phylogenetic tree

October 26, 2016
By

(This article was first published on R on Guangchuang YU, and kindly contributed to R-bloggers)

ggtree provides gheatmap for visualizing heatmap with phylogenetic tree and msaplot for visualizing multiple sequence alignment with phylogenetic tree.

We may have different data types and want to visualize and align them with the tree. For example, dotplot of SNP site (e.g. using geom_point(shape='|')), barplot of trait values (e.g. using geom_barh(stat='identity')) et al.

To make it easy to associate different types of data with phylogenetic tree, I implemented the facet_plot function which accepts a geom function to draw the input data.frame and display it in an additional panel.

tr <- rtree(30)
p <- ggtree(tr)
d1 <- data.frame(id=tr$tip.label, location=sample(c("GZ", "HK", "CZ"), 30, replace=TRUE))
p1 <- p %<+% d1 + geom_tippoint(aes(color=location))
d2 <- data.frame(id=tr$tip.label, val=rnorm(30, sd=3))
p2 <- facet_plot(p1, panel="dot", data=d2, geom=geom_point, 
                aes(x=val), color='firebrick') + theme_tree2()

Most of the geom in ggplot2 draw vertical graph object, while for associating graph object with phylogenetic tree, we need horizontal versions. Luckily, we have ggstance which provides horizontal versions of geoms, including:

  • geom_barh()
  • geom_histogramh()
  • geom_linerangeh()
  • geom_pointrangeh()
  • geom_errorbarh()
  • geom_crossbarh()
  • geom_boxploth()
  • geom_violinh()

With ggstance, we can associate barplot, boxplot or other graphs to phylogenetic trees.

d3 <- data.frame(id = rep(tr$tip.label, each=2),
                    value = abs(rnorm(60, mean=100, sd=50)),
                    category = rep(LETTERS[1:2], 30))
p3 <- facet_plot(p2, panel = 'Stacked Barplot', data = d3, 
                geom = geom_barh, 
                mapping = aes(x = value, fill = as.factor(category)), 
                stat='identity' ) 

d4 = data.frame(id=rep(tr$tip.label, each=20), 
                val=as.vector(sapply(1:30, function(i) 
                                rnorm(20, mean=i)))
                )               
p4 <- facet_plot(p3, panel="Boxplot", data=d4, geom_boxploth, 
            mapping = aes(x=val, group=label, color=location))                                  

Citation

G Yu, DK Smith, H Zhu, Y Guan, TTY Lam*. ggtree: an R package for visualization and annotation of phylogenetic trees with their covariates and other associated data. Methods in Ecology and Evolution. doi:10.1111/2041-210X.12628.

To leave a comment for the author, please follow the link and comment on their blog: R on Guangchuang YU.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)