Saturday, November 5, 2016

Plotting point-size on a phylomorphospace proportional to the value of a third character

Recently a phytools user asked if it were possible to scale the plotted points at tips & nodes of a plotted phylomorphospace in proportion to the value of a third trait.

“What I would like to do is to plot axes 1 and 2 in two dimensions and then make the points in the graph as circles proportional to axis 3. Do you have an idea how to do that?”

They provided an attached example in the form of Briggs et al. (1992).

This is actually pretty easy, as follows:

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
##         [,1]        [,2]       [,3]
## A  1.4800637  1.09652745 -1.1494221
## B  0.4791366  0.40907306 -0.3548262
## C  1.2351474  0.19028043 -0.5856361
## D  2.0851866  1.57117881 -1.3810955
## E  2.1138250  1.55440915 -1.6382140
## F  2.5633292  1.16354716 -0.3219158
## G  2.4537429  1.23287364 -0.3740234
## H  3.5660959 -1.62714583  1.2958532
## I  1.9329064 -1.87436079  1.1086486
## J  0.5050758 -1.56200859  1.9635014
## K  3.7305217 -2.41850413  2.3232728
## L  1.8526637 -2.30833059  3.4108897
## M  1.7070475 -2.10857785  2.2998548
## N  1.4046043 -2.53014781  2.3675294
## O  1.6555163 -1.99505382  2.2816972
## P  1.5965899 -0.51055114  1.0490624
## Q  1.0374652  0.17976541  1.2657477
## R  1.1659834  0.07728329  1.2683668
## S  0.4475435  0.09072082  1.5856455
## T  1.9675116 -0.41992527  1.4377758
## U  0.7077652 -0.56399506  1.6097363
## V -0.2623193 -0.79534615  0.2402987
## W  0.5563390 -1.20883630 -0.4694405
## X  0.1164100 -0.97543773 -0.3125623
## Y -0.6341650 -1.79165841  2.0198135
## Z -0.8576449 -1.74610813  2.0654261
obj<-phylomorphospace(tree,X[,1:2],node.size=c(0,0),xlab="x",
    ylab="y")
a<-fastAnc(tree,X[,3]) ## reconstruct states
cx<-c(X[,3],a)-min(c(X[,3],a))
cx<-cx/max(cx)*2 ## rescale to [0,2]
points(obj$xx,obj$yy,cex=cx,pch=19)

plot of chunk unnamed-chunk-1

Here I have rescaled the trait values to cex on the interval [0, 2], but this is arbitrary - although we of course have to ensure that no values are negative. We could also easily change colors, etc.

Finally, phytools also has 3D phylomorphospace plotting using 'rgl' or by simulating a third dimension in 2D:

phylomorphospace3d(tree,X,method="static")

plot of chunk unnamed-chunk-2

That's it.

1 comment:

  1. I should have noted that the code above assumes that x is in the order of the tip labels of the tree. - Liam

    ReplyDelete