Sunday, July 13, 2025

How to create a traitgram with the horizontal axis reversed.... (It's pretty easy.)

A phytools user recently asked about flipping the horizontal (“time”) axis on a traitgram: projection of the tree into phenotype space using phytools::phenogram.

This is quite easy to do, so I thought I’d through up a quick demo, as follows:

library(phytools)
## load data from Garland et al. (1992)
data(mammal.tree)
data(mammal.data)

## extract character of interest
ln.bodyMass<-log(setNames(mammal.data$bodyMass,
  rownames(mammal.data)))
## get total tree height: we'll use this later
max(nodeHeights(mammal.tree))
## [1] 70
## plot traitgram, no axes
par(mar=c(5.1,4.1,1.1,1.1)) ## update margins
phenogram(mammal.tree,ln.bodyMass,ftype="i",
  spread.cost=c(1,0),fsize=0.7,color=palette()[4],
  axes=FALSE,quiet=TRUE)

## add horizontal axis running backwards
axis(1,at=seq(0,70,by=10),labels=seq(70,0,by=-10))
mtext("mybp",1,line=3,at=35)

## add vertical axis running forwards
axis(2,las=1)
title(ylab="log(body mass, kg)")

plot of chunk unnamed-chunk-13

That’s all there is to it.

No comments:

Post a Comment

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