Friday, June 5, 2026

Generic plot method for the "fastAnc" object class in phytools

Sometimes I forget about all the hidden features of phytools.

One that I’d nearly forgotten about today is an S3 generic plot method that I wrote for the "fastAnc" continuous character ancestral state reconstruction method object class that I wrote just a few months ago.

Here’s a very simple demo.

## load phytools
library(phytools)
## load a tree & some data
data(primate.data)
data(primate.tree)
head(primate.data)
##                                  Group Skull_length Optic_foramen_area Orbit_area
## Allenopithecus_nigroviridis Anthropoid         98.5                7.0      298.7
## Alouatta_palliata           Anthropoid        109.8                5.3      382.3
## Alouatta_seniculus          Anthropoid        108.0                8.0      359.4
## Aotus_trivirgatus           Anthropoid         60.5                3.1      297.4
## Arctocebus_aureus            Prosimian         49.5                1.2      134.8
## Arctocebus_calabarensis      Prosimian         53.8                1.6      156.6
##                             Activity_pattern Activity_pattern_code
## Allenopithecus_nigroviridis          Diurnal                     0
## Alouatta_palliata                    Diurnal                     0
## Alouatta_seniculus                   Diurnal                     0
## Aotus_trivirgatus                  Nocturnal                     2
## Arctocebus_aureus                  Nocturnal                     2
## Arctocebus_calabarensis            Nocturnal                     2

Let’s analyze primate skull length, on a log-scale.

lnSkullLength<-setNames(log(primate.data$Skull_length),
  rownames(primate.data))

Here’s our ancestral state reconstruction under BM.

primate_anc<-fastAnc(primate.tree,lnSkullLength,CI=TRUE)
print(primate_anc,printlen=6)
## Ancestral character estimates using fastAnc:
##        91       92       93       94       95       96     
##  4.175934 4.186044 4.431545 4.724982 4.681729 4.665113 ....
## 
## Lower & upper 95% CIs:
##       lower    upper
## 91 3.814174 4.537694
## 92 3.828175 4.543914
## 93 4.116413 4.746677
## 94 4.477337 4.972627
## 95 4.483873 4.879584
## 96 4.494131 4.836094
##        ....     ....

Now let’s use the plot method, as follows. Note that most of the arguments of both phytools::plotTree and phytools::add.color.bar can be passed internally.

h<-max(nodeHeights(primate.tree))
plot(primate_anc,direction="upwards",ftype="i",
  fsize=0.6,title="log(skull length)",
  offset=1,legend="bottomleft",
  ylim=c(-0.1*h,1.35*h))

plot of chunk unnamed-chunk-6

That’s pretty useful, I think!

No comments:

Post a Comment

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