1. Two errors in example plotSlanted function on page 407

Chapter 13 covers algorithms for plotting phylogenies and comparative data, and includes some tips on basic R programming.

On page 407 we demonstrate a simple R function for graphing a phylogenetic tree in slanted style.

It works perfectly for the demo example, but actually includes two small bugs which will cause it to fail for more general use.

Here is the function as published:

plotSlanted<-function(phy){
    if(!inherits(phy,"phylo")){
        cat(paste("\nInput",nm,
            "is not an object of class \"phylo\".\n"))
        return(NULL)
    } else {
        cw<-reorder(vert.tree,"cladewise")
        yy<-vector(mode="numeric",length=Ntip(cw)+cw$Nnode)
        yy[1:Ntip(cw)]<-1:Ntip(cw)
        pw<-reorder(cw,"postorder")
        nn<-unique(pw$edge[,1])
        for(i in 1:length(nn)){
            ii<-pw$edge[which(nn[i]==pw$edge[,1]),2]
            yy[nn[i]]<-mean(yy[ii])
        }
        xx<-vector(mode="numeric",length=Ntip(cw)+cw$Nnode)
        for(i in 1:nrow(cw$edge))
            xx[cw$edge[i,2]]<-xx[cw$edge[i,1]]+cw$edge.length[i]
        plot(NA,bty="n",axes=FALSE,xlab="",ylab="",
        xlim=c(0,1.4*max(xx)),
        ylim=c(1,Ntip(cw)))
        for(i in 1:nrow(cw$edge))
            lines(xx[cw$edge[i,]],yy[cw$edge[i,]])
        points(xx,yy,pch=21,bg="white",cex=1.1)
        for(i in 1:Ntip(cw))
            text(xx[i],yy[i],sub("_"," ",cw$tip.label[i]),
                pos=4,font=3)
    }
}

The two errors that I found are that after if(!inherits(phy,"phylo")){ I should have included the line nm<-deparse(substitute(phy)). More critically, the line cw<-reorder(vert.tree,"cladewise") should actually have read cw<-reorder(phy,"cladewise").

Here’s the corrected function.

plotSlanted<-function(phy){
    if(!inherits(phy,"phylo")){
        nm<-deparse(substitute(phy))
        cat(paste("\nInput",nm,
            "is not an object of class \"phylo\".\n"))
        return(NULL)
    } else {
        cw<-reorder(phy,"cladewise")
        yy<-vector(mode="numeric",length=Ntip(cw)+cw$Nnode)
        yy[1:Ntip(cw)]<-1:Ntip(cw)
        pw<-reorder(cw,"postorder")
        nn<-unique(pw$edge[,1])
        for(i in 1:length(nn)){
            ii<-pw$edge[which(nn[i]==pw$edge[,1]),2]
            yy[nn[i]]<-mean(yy[ii])
        }
        xx<-vector(mode="numeric",length=Ntip(cw)+cw$Nnode)
        for(i in 1:nrow(cw$edge))
            xx[cw$edge[i,2]]<-xx[cw$edge[i,1]]+cw$edge.length[i]
        plot(NA,bty="n",axes=FALSE,xlab="",ylab="",
        xlim=c(0,1.4*max(xx)),
        ylim=c(1,Ntip(cw)))
        for(i in 1:nrow(cw$edge))
            lines(xx[cw$edge[i,]],yy[cw$edge[i,]])
        points(xx,yy,pch=21,bg="white",cex=1.1)
        for(i in 1:Ntip(cw))
            text(xx[i],yy[i],sub("_"," ",cw$tip.label[i]),
                pos=4,font=3)
    }
}

Let’s try to use it. Here I’ll use a phylogeny from Benitez-Alvarez et al. (2020) that comes loaded with phytools.

library(phytools)
data(flatworm.tree)
par(mar=rep(0.1,4),cex=0.7)
plotSlanted(flatworm.tree)
Tree of flatworm (planaria) species from Benitez-Alvarez et al. (2020).

Tree of flatworm (planaria) species from Benitez-Alvarez et al. (2020).

Cool.