Saturday, February 6, 2016

Coloring tip labels of a traitgram using phenogram

A phytools user requested instructions on how to paint the labels of a plotted traitgram (from phytools function phenogram) using different colors. Naively, this might seem straightforward now that phenogram assigns the environmental variable, "last_plot.phylo". Unfortunately, it is not that easy because the default is to use a numerical algorithm to optimize the spacing of the tip labels, and these new, spaced positions are not exported.

Consequently, I have now updated phenogram to invisibly return a matrix containing the x & y coordinates of each plotted tip label. Overlaying new tip labels is still not entirely trivial, because we want to overlay them precisely on our existing plotted labels (and we're going to do it with shadowtext in the TeachingDemos package to ensure that we don't see any part of the old label peeking out).

Here's a demo in which I color the tip labels by a discrete character:

library(phytools)
tree
## 
## Phylogenetic tree with 26 tips and 25 internal nodes.
## 
## Tip labels:
##  A, B, C, D, E, F, ...
## 
## Rooted; includes branch lengths.
x
##       A       B       C       D       E       F       G       H       I 
##  2.8600  2.1058  1.7397  2.8509 -1.8374  1.6606  5.6038  4.1693 -2.5189 
##       J       K       L       M       N       O       P       Q       R 
## -1.1314  4.9547  4.3946  5.8012  4.9661  6.0724  4.6994  3.5680  3.9083 
##       S       T       U       V       W       X       Y       Z 
##  1.8861  0.8216 -0.3356 -1.0667 -3.4524 -2.8598 -3.0773 -0.6128
y
## A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 
## a b b b a b a a a b a a a a b c c c c b a b a b b b 
## Levels: a b c
cols<-setNames(c("blue","magenta","red"),
    sort(unique(y)))
label.colors<-setNames(cols[y],names(y))
obj<-phenogram(tree,x)
obj ## coordinates of the labels
##    x          y
## A 11  2.9583965
## B 11  2.2101406
## C 11  1.6551344
## D 11  2.6603615
## E 11 -1.8427531
## F 11  1.3776016
## G 11  5.5009397
## H 11  4.0677803
## I 11 -2.5065820
## J 11 -1.2872559
## K 11  4.9024095
## L 11  4.3454942
## M 11  5.7884745
## N 11  5.1807178
## O 11  6.0724000
## P 11  4.6233649
## Q 11  3.5061471
## R 11  3.7901399
## S 11  1.9331497
## T 11  0.7719063
## U 11 -0.3396495
## V 11 -0.9411745
## W 11 -3.4524000
## X 11 -2.8206221
## Y 11 -3.1061709
## Z 11 -0.6229529
library(TeachingDemos)
lastPP<-get("last_plot.phylo",envir=.PlotPhyloEnv) ## get offset
shadowtext(x=obj,labels=rownames(obj),
    col=label.colors[rownames(obj)],bg="white",pos=4,
    offset=lastPP$label.offset)
## overlay tip states to show they match
tiplabels(pie=to.matrix(y[tree$tip.label],seq=sort(unique(y))),
    piecol=cols,cex=0.4)

plot of chunk unnamed-chunk-1

This latest version of phytools can be installed as in my previous post today.

No comments:

Post a Comment

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