Created
May 17, 2011 19:34
-
-
Save sckott/977207 to your computer and use it in GitHub Desktop.
Simple function for plotting phylogenies in ggplot2
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") | |
Thanks, I'll check it out!
joey
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Scott, I've just started investigating the
ggdendro
packagehttps://github.com/andrie/ggdendro
which appears to do many of the things you're discussing here. It doesn't yet support
phylo
class trees, but I'm looking into creating adendro_data.phylo
function that would solve that problem. I'll post again if I figure it out.In the time since you posted this, have you found any other solutions to this? I want to create a specialized version of this in my package. So far ggdendro appears to be the most promising route.
By the way, I just came across this informative tag about
ggplot2
in StackOverflow (mentionsggdendro
):http://stackoverflow.com/tags/ggplot2/info