Tuesday, June 14, 2022

phytools plotTree.wBars coloring the bars by our continuous trait

Today, a phytools user writes:

“I am trying to use the command plotTree.wBars in phytools to add bars as tips of a circular tree. The bars represent values of a continuous trait assigned to the tips. I was wondering if it is possible to also color these bars according to their values?”

The answer is, of course, yes. Let's see a couple of different examples.

For this example, I'll use a phylogeny and dataset for liolaemid lizards from Esquerre et al. (2019).

Luke Harmon & I use these data in our upcoming book, so the easiest way to get them is from the book website.

## load phytools
library(phytools)
## read tree
liol.tree<-read.nexus(file="http://www.phytools.org/Rbook/7/Liolaemidae.MCC.nex")
liol.tree
## 
## Phylogenetic tree with 258 tips and 257 internal nodes.
## 
## Tip labels:
##   Ctenoblepharys_adspersa, Liolaemus_abaucan, Liolaemus_koslowskyi, Liolaemus_albiceps, Liolaemus_irregularis, Liolaemus_ornatus, ...
## 
## Rooted; includes branch lengths.
## read data
liol.data<-read.csv(file="http://www.phytools.org/Rbook/7/Liolaemidae.data.csv",
    row.names=1)
head(liol.data)
##                         parity_mode max_altitude temperature
## Ctenoblepharys_adspersa           O          750       23.05
## Liolaemus_abaucan                 O         2600       20.20
## Liolaemus_albiceps                V         4020       12.38
## Liolaemus_andinus                 V         4900       11.40
## Liolaemus_annectens               V         4688        5.10
## Liolaemus_anomalus                O         1400       23.78

I'm going to first plot the tree with max_altitude using plotTree.wBars. The only trick I'm adding is that I'm going to steal some internal calculations (for scale=NULL) to add a scale bar to the tree using polygon. If we had set a specific value for scale, we'd use that instead of the variable ss.

max_altitude<-setNames(liol.data$max_altitude,rownames(liol.data))
plotTree.wBars(liol.tree,max_altitude,type="fan",lwd=1,
    tip.label=TRUE,fsize=0.3)
ss<-0.3*max(nodeHeights(liol.tree))/diff(range(max_altitude))
h<-0.95*par()$usr[2]
bb<-3000
polygon(x=-h+c(0,0,ss*bb,ss*bb),y=h+c(1,0,0,1),col="grey")
text(x=-h+ss*bb,y=h+0.5,paste(bb,"m",sep=""),pos=4)