Friday, December 18, 2020

Co-phylogenetic plotting with different color linking lines

I recently received the following inquiry:

“I am attempting to use phytools and cophyloplot to compare two trees. I would like to color the tip labels and connectors between certain tip labels. For example, in the tree below I might like to have 't1' and 't3' red in both trees and the connectors between them also be red. I have not been able to find an example for this in your blog. Could you tell me how this can be done or direct me to a blog post that explains this.”

In fact, this is pretty straightforward to do in phytools using the plot.cophylo argument link.col.

Here's a quick demo.

Load phytools:

library(phytools)

Load some co-phylogenetic data. This comes from Lopez-Vaamonde et al. (2001):

data(wasp.trees)
print(wasp.trees,details=TRUE)
## 2 phylogenetic trees
## tree 1 : 19 tips
## tree 2 : 15 tips
data(wasp.data)
wasp.data
##        Pleistodontes                Sycoscapter
## 1      P._greenwoodi             S._4_{obliqua}
## 2  P._xanthocephalus        S._2_{aff._obliqua}
## 3        P._plebejus   S._14_{hesperidiiformis}
## 4           P._rieki          S._12_{xylosycia}
## 5         P._blandus         S._11_{glandifera}
## 6         P._regalis         S._8_{pleurocarpa}
## 7          P._nitens          S._10_{crassipes}
## 8    P._schizodontes          S._9_{triradiata}
## 9      P._imperialis         S._15_{rubiginosa}
## 10      P._athysanus          S._1_{brachypoda}
## 11 P._astrabocheilus         S._7_{subpuberula}
## 12       P._proximus        S._6_{lilliputiana}
## 13    P._macrocainus         S._5_{cerasicarpa}
## 14       P._cuneatus            S._3_{playpoda}
## 15      P._froggatti S._australis_{macrophylla}

Now let's create & plot a standard "cophylo" object:

wasp.cophylo<-cophylo(wasp.trees[[1]],wasp.trees[[2]],
    assoc=wasp.data)
## Rotating nodes to optimize matching...
## Done.
wasp.cophylo
## Object of class "cophylo" containing:
## 
## (1) 2 (possibly rotated) phylogenetic trees in an object of class "multiPhylo".
## 
## (2) A table of associations between the tips of both trees.
plot(wasp.cophylo)

plot of chunk unnamed-chunk-3

That's pretty ugly. To get some more interesting colors for our linking lines, we can use the package randomcoloR as follows:

library(randomcoloR)
palette<-distinctColorPalette(nrow(wasp.data))

Let's graph these colors:

pie(rep(1,nrow(wasp.data)),col=palette)

plot of chunk unnamed-chunk-5

Now, our lines cross (obviously) so a useful feature of our colors could be to make them semi-transparent so that linking lines from one taxon to the other are easier to follow. Let's do that:

palette<-make.transparent(palette,0.5)

Finally, let's create our new "cophylo" plot:

par(lend=3)
plot(wasp.cophylo,link.col=palette,link.lwd=4,link.type="curved",
    link.lty="solid",fsize=c(0.8,0.8))

plot of chunk unnamed-chunk-7

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.