Sunday, February 21, 2016

dotTree for discrete character data

I just added the feature to dotTree (previously described previously on this blog here: 1, 2) to permit discrete characters. In this case the dots will simply be colored by state.

For a single trait a similar effect can be produced using tiplabels in the ape package; however the advantage of dotTree is that it an easily simultaneously represent the tip states for a large number of traits.

Here's a quick demo with 6 binary traits:

library(phytools)
packageVersion("phytools")
## [1] '0.5.18'
tree
## 
## Phylogenetic tree with 50 tips and 49 internal nodes.
## 
## Tip labels:
##  t12, t16, t29, t30, t14, t15, ...
## 
## Rooted; includes branch lengths.
X
##     [,1] [,2] [,3] [,4] [,5] [,6]
## t12 "a"  "b"  "b"  "a"  "b"  "b" 
## t16 "a"  "b"  "b"  "a"  "a"  "b" 
## t29 "a"  "b"  "a"  "a"  "a"  "a" 
## t30 "b"  "a"  "a"  "b"  "a"  "a" 
## t14 "b"  "b"  "a"  "a"  "a"  "a" 
## t15 "a"  "a"  "a"  "b"  "a"  "a" 
## t2  "b"  "b"  "a"  "a"  "a"  "a" 
## t32 "b"  "b"  "a"  "a"  "b"  "a" 
## t33 "b"  "b"  "a"  "a"  "b"  "a" 
## t21 "b"  "a"  "a"  "b"  "a"  "a" 
## t47 "b"  "b"  "b"  "a"  "a"  "a" 
## t48 "b"  "b"  "b"  "a"  "a"  "a" 
## t34 "b"  "b"  "b"  "a"  "b"  "a" 
## t23 "b"  "a"  "b"  "b"  "a"  "a" 
## t25 "b"  "b"  "b"  "a"  "a"  "a" 
## t26 "b"  "b"  "a"  "a"  "b"  "a" 
## t9  "a"  "a"  "b"  "a"  "a"  "a" 
## t10 "a"  "a"  "b"  "b"  "a"  "a" 
## t3  "b"  "b"  "b"  "b"  "a"  "a" 
## t8  "b"  "a"  "a"  "b"  "b"  "a" 
## t43 "a"  "a"  "a"  "a"  "a"  "a" 
## t44 "a"  "a"  "a"  "a"  "a"  "a" 
## t39 "a"  "a"  "b"  "b"  "b"  "b" 
## t40 "a"  "a"  "a"  "b"  "b"  "b" 
## t1  "b"  "a"  "a"  "b"  "a"  "b" 
## t4  "b"  "b"  "b"  "b"  "a"  "a" 
## t5  "b"  "b"  "a"  "b"  "a"  "a" 
## t13 "b"  "b"  "a"  "b"  "b"  "b" 
## t41 "b"  "a"  "a"  "a"  "b"  "a" 
## t42 "b"  "a"  "b"  "a"  "b"  "a" 
## t17 "b"  "a"  "b"  "a"  "b"  "b" 
## t18 "b"  "b"  "b"  "a"  "b"  "a" 
## t27 "a"  "b"  "b"  "a"  "b"  "b" 
## t28 "b"  "b"  "a"  "a"  "b"  "b" 
## t24 "b"  "b"  "b"  "a"  "b"  "b" 
## t49 "b"  "a"  "a"  "a"  "b"  "a" 
## t50 "b"  "a"  "a"  "a"  "b"  "a" 
## t22 "b"  "b"  "a"  "b"  "a"  "a" 
## t45 "b"  "b"  "a"  "a"  "a"  "a" 
## t46 "b"  "b"  "a"  "a"  "a"  "a" 
## t38 "b"  "b"  "a"  "a"  "b"  "a" 
## t11 "b"  "b"  "b"  "a"  "b"  "a" 
## t19 "a"  "a"  "a"  "a"  "b"  "b" 
## t31 "a"  "b"  "a"  "a"  "a"  "b" 
## t35 "a"  "a"  "a"  "a"  "b"  "a" 
## t36 "b"  "b"  "a"  "a"  "b"  "b" 
## t37 "a"  "a"  "a"  "a"  "b"  "a" 
## t20 "a"  "a"  "a"  "b"  "a"  "b" 
## t7  "a"  "a"  "a"  "b"  "a"  "b" 
## t6  "a"  "b"  "a"  "a"  "b"  "b"
colors<-setNames(c("blue","red"),c("a","b"))
colors
##      a      b 
## "blue"  "red"
dotTree(tree,X,colors=colors,data.type="discrete",fsize=0.7)

plot of chunk unnamed-chunk-1

Like dotTree for data.type="continuous", it also has a method using plotTree internally which works for one character. For instance:

X[,1]
## t12 t16 t29 t30 t14 t15  t2 t32 t33 t21 t47 t48 t34 t23 t25 t26  t9 t10 
## "a" "a" "a" "b" "b" "a" "b" "b" "b" "b" "b" "b" "b" "b" "b" "b" "a" "a" 
##  t3  t8 t43 t44 t39 t40  t1  t4  t5 t13 t41 t42 t17 t18 t27 t28 t24 t49 
## "b" "b" "a" "a" "a" "a" "b" "b" "b" "b" "b" "b" "b" "b" "a" "b" "b" "b" 
## t50 t22 t45 t46 t38 t11 t19 t31 t35 t36 t37 t20  t7  t6 
## "b" "b" "b" "b" "b" "b" "a" "a" "a" "b" "a" "a" "a" "a"
dotTree(tree,X[,1],fsize=0.7,ftype="i") ## default colors

plot of chunk unnamed-chunk-2

The code of this update can be seen here.

The data above were simulated as follows:

tree<-pbtree(n=50)
Q<-matrix(c(-1,1,1,-1),2,2,dimnames=list(letters[1:2],letters[1:2]))
X<-replicate(6,sim.history(tree,Q)$states)

Finally, phytools can be installed from GitHub using devtools, e.g.:

library(devtools)
install_github("liamrevell/phytools")

4 comments:

  1. Thank you very much for this great addition. May I ask if there's any way to add bootstrap value by each node on the tree plotted by dotTree?

    ReplyDelete
    Replies
    1. Good question. I posted a response here. I hope it's helpful. Liam

      Delete
  2. Hello Liam,

    I would like to know how do you change the size of the red/blue circles please ?

    Thank you !
    Ombeline

    ReplyDelete
  3. Hello,
    If you are trying to plot multiple columns of discrete data, do the data frames need to have matching factor levels?

    I am having issues trying to plot a dotTree with two rows of discrete data with
    x <- five discrete variables
    y <- two discrete variables

    Any advice would be greatly appreciated!

    ReplyDelete

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