Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
plotting phylogenetic trees
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  2 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
filip  
View profile  
 More options Nov 3 2010, 5:10 pm
From: filip <fbiele...@gmail.com>
Date: Wed, 3 Nov 2010 22:10:44 +0100
Local: Wed, Nov 3 2010 5:10 pm
Subject: plotting phylogenetic trees
Hello everybody,

I wrote a few methods for plotting phylogenetic trees in ggplot, might
come in handy (though for now only applicable for objects class
"phylo" not "multiphylo"). Based on plot methods from ape
package:

#################
#---LIBRARIES---#
#################
library(ape)
library(ggplot2)

#####################
#---FORTIFY.PHYLO---#
#####################
fortify.phylo <- function(phylo) {

Ntip <- length(phylo$tip.label)
Nnode <- phylo$Nnode
Nedge <- dim(phylo$edge)[1]
z <- reorder(phylo, order = "pruningwise")
yy <- numeric(Ntip + Nnode)
TIPS <- phylo$edge[phylo$edge[, 2] <= Ntip, 2]
yy[TIPS] <- 1:Ntip

ans1 <- .C("node_height_clado", as.integer(Ntip), as.integer(Nnode),
as.integer(z$edge[, 1]), as.integer(z$edge[,2]), as.integer(Nedge),
double(Ntip + Nnode), as.double(yy), DUP = FALSE, PACKAGE = "ape")

        yy <- ans1[[7]]

ans2 <- .C("node_depth_edgelength", as.integer(Ntip),
as.integer(Nnode), as.integer(z$edge[, 1]), as.integer(z$edge[, 2]),
as.integer(Nedge), as.double(z$edge.length), double(Ntip + Nnode), DUP
= FALSE, PACKAGE = "ape")

        xx <- ans2[[7]]

    edge <- phylo$edge
    nodes <- (Ntip + 1):(Ntip + Nnode)
    x0v <- xx[nodes]
    y0v <- y1v <- numeric(Nnode)
    NodeInEdge1 <- vector("list", Nnode)

    for (i in nodes) {
        ii <- i - Ntip
        j <- NodeInEdge1[[ii]] <- which(edge[, 1] == i)
        tmp <- range(yy[edge[j, 2]])
        y0v[ii] <- tmp[1]
        y1v[ii] <- tmp[2] }

    x0h <- xx[edge[, 1]]
    x1h <- xx[edge[, 2]]
    y0h <- yy[edge[, 2]]

lineh <- data.frame(x=x1h, y=y0h, xend=x0h, yend=y0h)
linev <- data.frame(x=x0v, y=y1v, xend=x0v, yend=y0v)
x <- rbind(linev, lineh)

return(x)

}

###################
#---LABEL.PHYLO---#
###################
label.phylo <- function(phylo) {

Ntip <- length(phylo$tip.label)
Nnode <- phylo$Nnode
Nedge <- dim(phylo$edge)[1]
z <- reorder(phylo, order = "pruningwise")
yy <- numeric(Ntip + Nnode)
TIPS <- phylo$edge[phylo$edge[, 2] <= Ntip, 2]
yy[TIPS] <- 1:Ntip

ans1 <- .C("node_height_clado", as.integer(Ntip), as.integer(Nnode),
as.integer(z$edge[, 1]), as.integer(z$edge[,2]), as.integer(Nedge),
double(Ntip + Nnode), as.double(yy), DUP = FALSE, PACKAGE = "ape")

        yy <- ans1[[7]]

ans2 <- .C("node_depth_edgelength", as.integer(Ntip),
as.integer(Nnode), as.integer(z$edge[, 1]), as.integer(z$edge[, 2]),
as.integer(Nedge), as.double(z$edge.length), double(Ntip + Nnode), DUP
= FALSE, PACKAGE = "ape")

        xx <- ans2[[7]]

data.frame(x=xx[1:Ntip], y=yy[1:Ntip], label=phylo$tip.label)

}

###############
#---EXAMPLE---#
###############
data(bird.orders)
x <- bird.orders
#x<-read.nexus(treefile.tree)

xlim=35

p <- ggplot(data=x)
p <- p + geom_segment(aes(x=x,y=y,xend=xend,yend=yend), colour="blue",alpha=1)
p <- p + geom_text(data=label.phylo(x), aes(x=x, y=y, label=label),hjust=0, family=3, vjust=0.5, size=3) + xlim(0, xlim)
theme <- theme_update(
                           axis.text.y = theme_blank(),
                           axis.ticks = theme_blank(),
                           axis.title.x = theme_blank(),
                           axis.title.y = theme_blank(),
                           legend.position = "none"
                      )
p <- p + theme_set(theme)
print(p)

--
while(!succeed) { try(); }


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Hadley Wickham  
View profile  
 More options Nov 4 2010, 9:06 pm
From: Hadley Wickham <had...@rice.edu>
Date: Thu, 4 Nov 2010 20:06:25 -0500
Local: Thurs, Nov 4 2010 9:06 pm
Subject: Re: plotting phylogenetic trees
Thanks Filip!
Hadley

--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »