A simple function for plotting phylogenies in ggplot2
[This article was first published on Recology, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
I wrote a simple function for plotting a phylogeny in ggplot2. However, it only handles a 3 species tree right now, as I haven’t figured out how to generalize the approach to N species.Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Any ideas on how to improve this?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
###### Simple function for plotting phylogenies in ggplot2 | |
# x = a phylo object | |
# form = one of: star or ladder | |
# dependencies: ape, ggplot2, adephylo (loaded within function) | |
ggtree <- function(x, form) { | |
# Load packages | |
require(ape); require(ggplot2); require(adephylo) | |
# Define plotting format | |
phytheme_ <- function() | |
list(theme_bw(), | |
opts(panel.grid.major = theme_blank(), panel.grid.minor = theme_blank(), | |
legend.position="none", axis.text.x = NULL, axis.text.y = NULL, | |
axis.ticks = NULL, panel.border = NULL), | |
labs(x = "", y = "")) | |
# Process newick file | |
tree_ <- as(as(x, "phylo4"), "data.frame") # convert to phylo4 table | |
tree_tips <- tree_[tree_$node.type == "tip", ] # get table with tips only | |
ntips <- nrow(tree_tips) # get number of tips | |
# Plot tree | |
if ( form == "ladder" ) { | |
t_ <- ggplot(tree_) + | |
geom_segment(aes(x = 1, y = 1, xend = 1-tree_[1,4], yend = 1)) + # tip | |
geom_segment(aes(x = 1, y = 2, xend = 1-tree_[2,4], yend = 2)) + # tip | |
geom_segment(aes(x = 1, y = 3, xend = 1-tree_[3,4], yend = 3)) + # tip | |
geom_segment(aes(x = 1-tree_[1,4], y = 1.5, xend = 1-tree_[1,4]-tree_[5,4], yend = 1.5)) + | |
geom_segment(aes(x = 1-tree_[1,4], y = 1, xend = 1-tree_[1,4], yend = 2)) + | |
geom_segment(aes(x = 1-tree_[3,4], y = 1.5, xend = 1-tree_[3,4], yend = 3)) + | |
geom_text(data = tree_tips, aes(x = 1, y = node, label = label, hjust = 0)) + # label tips | |
phytheme_() # add theme options | |
return(t_) } else # return tree | |
if ( form == "star" ) { | |
tree_tips_ <- cbind(tree_tips, star = seq(0, 1, 0.5)) # add column of star end locations | |
star_t_ <- ggplot(tree_tips_) + | |
geom_segment(aes(x = 0, y = 0.5, xend = 1, yend = tree_tips_[1,6])) + # tip | |
geom_segment(aes(x = 0, y = 0.5, xend = 1, yend = tree_tips_[2,6])) + # tip | |
geom_segment(aes(x = 0, y = 0.5, xend = 1, yend = tree_tips_[3,6])) + # tip | |
geom_text(aes(x = 1, y = star, label = label, hjust = 0)) + # label tips | |
phytheme_() # add theme options | |
return(star_t_) } # return tree | |
} | |
###### Example | |
tree <- rcoal(3) | |
ggtree(tree, "star") | |
ggtree(tree, "ladder") | |
To leave a comment for the author, please follow the link and comment on their blog: Recology.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.