Friday, April 13, 2018

Some new features to modernize plotTree.wBars

After posting a recent hack demonstrating how to plot stacked bars on a fan tree using plotTree.wBars in phytools I decided it might be useful to modernize the function a bit.

One feature I though I could add was dotted linking lines leading from the tip of the tree to either the tip label or the bar for non-ulrametric trees. The reason for this is because it can be very difficult to line-up the bar with its corresponding tip if the some tips terminate before the present.

Another feature that I thought I'd get rid of at the same time is the fact that for all x > 0 the behavior of the function is to attach the bars directly to the tip - regardless of whether or not it terminates in the present.

Here's what I mean:

library(phytools)
tree<-pbtree(n=100,b=1,d=0.5)
x<-fastBM(tree,bounds=c(0,Inf))
plotTree.wBars(tree,x,lwd=1)

plot of chunk unnamed-chunk-1

or:

plotTree.wBars(tree,x,lwd=1,type="fan")

plot of chunk unnamed-chunk-2

Obviously this could be undesirable under some circumstances.

Note that the function does not behave this way when some or all values of x are negative:

y<-fastBM(tree)
plotTree.wBars(tree,y,lwd=1,type="fan")

plot of chunk unnamed-chunk-3

Let's load the update version from GitHub & see how it compares. Note that we would normally just update phytools from GitHub using devtools.

source("https://raw.githubusercontent.com/liamrevell/phytools/master/R/plotTree.wBars.R")
plotTree.wBars(tree,x,lwd=1)

plot of chunk unnamed-chunk-4

## or
plotTree.wBars(tree,x,type="fan")

plot of chunk unnamed-chunk-4

## and
plotTree.wBars(tree,y,type="fan")

plot of chunk unnamed-chunk-4

Note that the default scale is different. The old version used an ignorant scale which could result in some pretty ludicrous plots by default. This should be better.

Some small update to control tip label linking lines in plot.cophylo

On user request I today pushed a very small update to allow the lengths of the linking lines connecting the tips of the tree to the tip labels in a plotted "cophylo" to be controlled by the user.

First, a quick reminder of how a plotted object would normally look:

library(phytools)
t1
## 
## Phylogenetic tree with 20 tips and 19 internal nodes.
## 
## Tip labels:
##  t7, t8, t5, t6, t2, t3, ...
## 
## Rooted; includes branch lengths.
t2
## 
## Phylogenetic tree with 25 tips and 24 internal nodes.
## 
## Tip labels:
##  t1, t12, t13, t5, t6, t20, ...
## 
## Rooted; includes branch lengths.
obj<-cophylo(t1,t2)
## Rotating nodes to optimize matching...
## Done.
plot(obj,link.type="curved")

plot of chunk unnamed-chunk-1

The argument to shorten the linking lines is tip.len, but we can also combine it with tip.lty to turn of the tip lines entirely:

plot(obj,tip.lty=0,tip.len=0.01,pts=FALSE,part=0.35)

plot of chunk unnamed-chunk-2

Finally, let's say we want to turn off the tip labels and draw our linking lines directly from the tips (the tiplabels.cophylo I added just for fun):

plot(obj,tip.len=0,ftype="off",part=0.35,lwd=2)
tiplabels.cophylo(pie=to.matrix(rep(1,Ntip(obj$trees[[1]])),"1"),
    cex=0.3,piecol="grey")
tiplabels.cophylo(pie=to.matrix(rep(1,Ntip(obj$trees[[2]])),"1"),
    cex=0.3,piecol="grey",which="right")

plot of chunk unnamed-chunk-3

That's it.