Friday, July 22, 2022

Graphing a contMap style tree in unrooted style using phytools & ape::plot.phylo

I think I've shown this before, but I just wanted to reiterate a quick demo on how to graph a "contMap" style plot of an unrooted tree using ape::plot.phylo.

For this example, I'll use a phylogeny & dataset of cordylid lizards from Broeckhoven et al. (2016), although I'm going to rejig the edge lengths a little bit so they look more like we might expect to see for an unrooted phylogeny. I use the same dataset (but for different analyses) in my upcoming book with Luke Harmon.

library(phytools)
cordylid.tree<-read.tree(file="http://www.phytools.org/Rbook/5/cordylid-tree.phy")
print(cordylid.tree,printlen=5)
## 
## Phylogenetic tree with 28 tips and 27 internal nodes.
## 
## Tip labels:
##   C._aridus, C._minor, C._imkeae, C._mclachlani, C._macropholis, ...
## 
## Rooted; includes branch lengths.
cordylid.data<-read.csv(file="http://www.phytools.org/Rbook/5/cordylid-data.csv",
    row.names=1)
head(cordylid.data)
##                    pPC1     pPC2     pPC3
## C._aridus       0.59441 -0.40209  0.57109
## C._minor        0.65171 -0.32732  0.55692
## C._imkeae       0.19958 -0.08978  0.56671
## C._mclachlani   0.62065  0.03746  0.86721
## C._macropholis  0.44875 -0.75942  0.09737
## C._cordylus    -0.07267  0.48294 -0.54394
pc1<-setNames(cordylid.data$pPC1,rownames(cordylid.data))

As I said, let's rejig the edges of the tree a tiny bit so they more closely typify what we might expect to see in an unrooted phylogeny.

cordylid.unrooted<-unroot(cordylid.tree)
ee<-cordylid.unrooted$edge.length
ee<-ee*rgamma(n=length(ee),shape=3,scale=1/3)
cordylid.unrooted$edge.length<-ee

Next, I'll create the "contMap" object (without plotting it), and then re-color it.

## first, compute the contMap & re-color it
cordylid.cMap<-contMap(cordylid.unrooted,pc1,plot=FALSE)
cordylid.cMap<-setMap(cordylid.cMap,
    c("#e4d96f","red","black"))

Now I'm pretty much ready to make my plot.

To do this, I'm going to first convert my "contMap" object tree to a standard "phylo" object with a whole bunch of unbranching (i.e., “singleton”) nodes using the phytools function map.to.singleton. Next, I'm going to add spaces before & after the species labels. This is just to make sure our taxa names are spaced sufficiently off the tips of the phylogeny when we plot it using thickened edges

## now convert "contMap" object to tree with singleton
## nodes
cordylid.nodeMap<-map.to.singleton(cordylid.cMap$tree)
par(lend=0)
cordylid.nodeMap$tip.label<-paste(" ",cordylid.nodeMap$tip.label," ",sep="")
plot(cordylid.nodeMap,
    edge.col=cordylid.cMap$cols[names(cordylid.nodeMap$edge.length)],
    type="unrooted",lab4ut="axial",cex=0.7,edge.width=4,
    no.margin=TRUE)
dx<-diff(par()$usr[1:2])
dy<-diff(par()$usr[3:4])
add.color.bar(0.4*dx,cordylid.cMap$cols,title="PC 1",
    lims=cordylid.cMap$lims,digit=2,prompt=FALSE,
    x=par()$usr[1]+0.05*dx,y=par()$usr[4]-0.05*dy,
    outline=FALSE,subtitle="",fsize=0.8,lwd=8)

plot of chunk unnamed-chunk-4

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.