Friday, June 6, 2014

Plotting a slanted cladogram using phytools

I inadvertently discovered the algorithm for plotting a right-facing slanted cladogram this evening. Basically, to get the vertical position of each internal node we just assign heights 1 through N to the tips; and then each internal node is merely the simple arithmetic mean of its descendants. We get the horizontal positions (the heights above the root) using the method of Grafen with ρ=1, implemented in the ape function compute.brlen.

We can apply this algorithm via the phytools function phenogram as follows:

plotCladogram<-function(tree){
  foo<-function(tree,x){
   n<-1:tree$Nnode+length(tree$tip.label)
   setNames(sapply(n,function(n,x,t) mean(x[Descendants(t,n,
    "tips")[[1]]]),x=x,t=tree),n)
  }
  tree<-reorder(tree,"cladewise")
  x<-setNames(1:length(tree$tip.label),tree$tip.label)
  phenogram(compute.brlen(tree),c(x,foo(tree,x)),ylab="")
}

For example:

> require(phytools)
> require(phangorn)
> tree<-pbtree(n=26,tip.label=LETTERS[26:1])
> plotCladogram(tree)

No comments:

Post a Comment

Note: due to the very large amount of spam, all comments are now automatically submitted for moderation.