Friday, March 29, 2024

Function for plotting discrete and/or continuous characters at the tips of a fan or arc-style tree (plotFanTree.wTraits) now in phytools

Over the past few days I’ve made various posts about graphing discretely & continuous-valued trait data at the tips of an “arc” or “fan” style tree (1, 2, 3).

After some fixes and improvements, my best solution to this problem (here) has now been added to the phytools package on GitHub (which can be installed, as always, using the remotes or devtools packages).

Here’s a quick example using a now classic phylogeny & trait dataset for Anolis lizards from Mahler et al. (2010).

library(phytools)
packageVersion("phytools")
## [1] '2.2.2'
data(anoletree)
anoletree
## 
## Phylogenetic tree with 82 tips and 81 internal nodes.
## 
## Tip labels:
## 	ahli, allogus, rubribarbus, imias, sagrei, bremeri, ...
## 
## The tree includes a mapped, 6-state discrete character
## with states:
## 	CG, GB, TC, TG, Tr, Tw
## 
## Rooted; includes branch lengths.
data(anole.data)
head(anole.data)
anole_resid<-phyl.resid(anoletree,
  x=setNames(anole.data[,"SVL"],rownames(anole.data)),
  Y=anole.data[,2:6])
cols<-plotFanTree.wTraits(ladderize(anoletree,right=FALSE),
  cbind(anole.data[,1,drop=FALSE],anole_resid$resid),
  part=0.5,arc_height=1,spacer=0.1,fsize=0.7)
h<-max(nodeHeights(anoletree))
arc_height<-1
text(x=h+seq(0.05,by=0.07,length.out=ncol(anole.data))*h+
    arc_height*h,y=rep(-0.5,ncol(anole.data)),
  c("SVL",paste("resid(",colnames(anole.data)[2:6],")",sep="")),
  srt=-90,adj=c(0, 0.25),cex=0.5)
text(x=-h-seq(0.05,by=0.07,length.out=ncol(anole.data))*h-
    arc_height*h,y=rep(-0.5,ncol(anole.data)),
  c("SVL",paste("resid(",colnames(anole.data)[2:6],")",sep="")),
  srt=90,adj=c(1, 0.25),cex=0.5)
add.color.bar(1.5*h,cols[[1]],title="",lims=NULL,digits=0,
  subtitle="",x=-0.75*h,y=0,prompt=FALSE,outline=FALSE,
  lwd=8)
text(x=-0.75*h,y=-0.25,"smaller",adj=c(0,1))
text(x=0.75*h,y=-0.25,"larger",adj=c(1,1))

plot of chunk unnamed-chunk-12

Although what’s now in phytools is largely what I previously posted on this blog, the most significant improvement is that the phytools version can handle non-ultrametric trees.

Here’s an example.

tree<-rtree(n=200)
Q<-matrix(c(-1,1,0,1,-2,1,0,1,-1),3,3,
  dimnames=list(letters[1:3],letters[1:3]))
X<-sim.Mk(tree,Q,nsim=10)
plotFanTree.wTraits(tree,X,ftype="off")

plot of chunk unnamed-chunk-13

OK – well, I’m not sure about that default color palette, but, aside from that, pretty cool IMO.

Saludos people.

1 comment:

  1. Hey Liam, I am running into an error that I cannot figure out. Everything to works as anticipated if the taxon names are separated by underscores. However, if I try to change the taxon names in the tree and data files to be e.g., "B. pilosa", instead of" bidens_pilosa" as they were in the data frame used for analyses, I get this error: Error in seq.default(min(X[, i]), max(X[, i]), length.out = 100) : 'from' must be a finite number.

    I can always change the tips in an image editor, but wondering if there was a quick fix for this before exporting the figure. Thanks!

    Cheers,
    Luke Campillo

    ReplyDelete

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