Thursday, April 30, 2020

Can you show clades in different colors on a phylomorphospace plot?

Today a phytools user emailed me to ask:

“I've been using the function phylomorphospace and I was wondering if it's possible to colour-code specific clades of interest on the phylomorphospace. I just think this would be cool to visualize how phylogenetically similar species are also grouped together in the plot. Any help would be hugely appreciated.”

In fact, I posted about this some time ago - but since that post is nearly 7 years old (!!) I thought it might be worth quickly revisiting.

So - how do we do this? Well, we're going to make use of the handy function paintSubTree to map the clades we want to paint on the tree. paintSubTree creates an object of class "simmap". When we pass this object to phylomorphospace it will automatically show the edges of our different clades with different colors.

Our first step, of course, is to load phytools. You'll see that I'm using a version of phytools that (as of the day of writing) is newer than the one on CRAN. For the purposes of this exercise, the CRAN version should work just fine - though I do recommend updating phytools to the GitHub version using devtools when it is convenient.

library(phytools)
packageVersion("phytools")
## [1] '0.7.20'

Now, I'm going to use simulated data for the purposes of our exercise here. I imagine the reader will use their own data & tree. The code I used to obtain these data is at the end of the exercise.

tree
## 
## Phylogenetic tree with 26 tips and 25 internal nodes.
## 
## Tip labels:
##  A, B, C, D, E, F, ...
## 
## Rooted; includes branch lengths.
data
##          [,1]        [,2]
## A -1.75195901 -0.02811985
## B -1.72256116 -0.10842583
## C -2.03542188 -0.29208511
## D -1.30386978  1.56135123
## E -1.55427734  0.84457261
## F -1.56124806  0.88057901
## G -1.70357159  0.96768809
## H -1.87085418 -1.04889283
## I -2.20172399 -1.03983458
## J -2.37372561 -1.03157961
## K -0.85082773  0.67359035
## L -3.34822930 -0.01463976
## M -3.65813713 -0.40802685
## N  0.21553734 -1.08821504
## O  0.32124222 -0.86924776
## P  0.87040084  0.16334856
## Q  0.58708585 -0.07989876
## R -0.01174367 -0.46368394
## S -1.02001392 -0.08479048
## T -1.85928590 -0.19700070
## U  0.88282813  0.01883294
## V  1.41692854  0.52650730
## W  0.06936227  0.22382364
## X  0.10850951  0.01089770
## Y  0.24485634  0.19624406
## Z -0.19761858  0.26333832

Let's first create a regular old phylomorphospace without mapping clades:

par(bty="l")
phylomorphospace(tree,data,bty="l",xlab="character 1",
    ylab="character 2",node.size=c(0,1))
title(main="phylomorphospace plot",font.main=3)

plot of chunk unnamed-chunk-4

Great. So far, so good.

Now, let's imagine that we want to color the clades of our tree as follows:

plotTree(tree,ftype="i")
nodelabels(frame="circ",bg="white",cex=0.8)
cladelabels(tree,c("clade 1","clade 2","clade 3"),
    c(47,41,29))

plot of chunk unnamed-chunk-5

The way we'll go about doing this is by 'painting' regimes on the subtrees descended from each of the three MRCAs of these different clades. Let's do it:

painted<-paintSubTree(tree,47,"clade 1","0")
painted<-paintSubTree(painted,41,"clade 2")
painted<-paintSubTree(painted,29,"clade 3")

Let's check to make sure that we got it right…

plot(painted,ftype="off",lwd=3)
## no colors provided. using the following legend:
##         0   clade 1   clade 2   clade 3 
##   "black" "#DF536B" "#61D04F" "#2297E6"
cladelabels(tree,c("clade 1","clade 2","clade 3"),
    c(47,41,29),wing.length=0,offset=0.5)

plot of chunk unnamed-chunk-7

OK, that's right on. Now let's plot our phylomorphospace:

par(bty="l")
phylomorphospace(painted,data,bty="l",xlab="character 1",
    ylab="character 2",node.size=c(0,1.2),node.by.map=TRUE)
title(main="phylomorphospace plot",font.main=3)
legend(x="topleft",legend=c("clade 1","clade 2","clade 3"),
    pch=21,pt.cex=1.5,pt.bg=palette()[2:4],bty="n")

plot of chunk unnamed-chunk-8

That's OK.

As promised, the data were simulated using the following code:

set.seed(1)
tree<-pbtree(n=26,tip.label=LETTERS,scale=1)
data<-fastBM(tree,nsim=2)