Wednesday, January 28, 2026

Simple new S3 plot method for the "fastAnc" object class

I just committed a new update to phytools that involved adding a new generic plot method for the "fastAnc" object class. This was pretty simple, with the main trick to it being that I modified the "fastAnc" object just by adding the tree and input data from fastAnc as object attributes.

Here’s a simple demo.

## load package
library(phytools)
## check version number
packageVersion("phytools")
## [1] '2.5.4'
## load tree and data
data(mammal.tree)
data(mammal.data)
## extract log body mass
lnBodyMass<-setNames(log(mammal.data$bodyMass),
  rownames(mammal.data))
## run ASR using phytools::fastAnc
mammal.anc<-fastAnc(mammal.tree,lnBodyMass)
mammal.anc
## Ancestral character estimates using fastAnc:
##       50       51       52       53       54       55       56       57       58       59 
## 4.616864 3.928453 3.570720 3.372081 5.008271 5.416959 2.503895 1.783068 2.074242 2.092048 
##       60       61       62       63       64       65       66       67       68       69 
## 2.102201 2.427742 2.879777 2.935790 4.000602 3.660331 4.314389 4.607878 4.760248 4.846334 
##       70       71       72       73       74       75       76       77       78       79 
## 5.299692 5.545168 7.076476 5.516430 5.552013 5.123064 5.352999 5.190895 5.148764 5.146091 
##       80       81       82       83       84       85       86       87       88       89 
## 5.221873 6.105385 4.596915 3.642387 3.618018 3.586540 4.613914 4.591181 4.847749 4.777965 
##       90       91       92       93       94       95       96       97 
## 4.873585 4.882390 5.109305 4.960400 4.878856 4.928119 4.832228 4.246744
plot(mammal.anc,ftype="i",fsize=0.6,offset=0.5,
  title="log(body mass)")

plot of chunk unnamed-chunk-6

Cool.

Let’s simulate a large tree and then try it in a different style.

## simulate tree & data
sim_tree<-pbtree(n=500,scale=100)
sim_data<-fastBM(sim_tree)
## do ASR
sim_asr<-fastAnc(sim_tree,sim_data,CI=TRUE)
## print ASR
print(sim_asr,printlen=4)
## Ancestral character estimates using fastAnc:
##        501       502       503       504     
##  -1.968242 -2.989587 -4.870655 -9.828655 ....
## 
## Lower & upper 95% CIs:
##          lower     upper
## 501   -8.59862  4.662136
## 502  -8.817602  2.838427
## 503 -10.380942  0.639632
## 504 -15.847845 -3.809464
##           ....      ....
## plot results
plot(sim_asr,type="fan",ftype="off",lwd=1,color="grey",
  node.cex=0.8,tip.cex=0.5,ylim=c(-120,100),legend="bottomleft")

plot of chunk unnamed-chunk-8

That’s pretty much the idea.

No comments:

Post a Comment

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