r3dphylogeny

R phytools add color to branches in 3D phylogenetic tree


I am using R "phytools" library for the 3D phylogenetic tree.

And I have made 3D tree successfully with the script below

library(phytools)
library(rgl)

tree<-read.tree(text="((un6:3,(un2:2,(un7:1,un5:1):1):1):1,((un1:1,un4:1):1,un3:1,un8:1):1):1,(un9:2,un10:2):1):1):1;")
class(tree) <- "phylo"
Bae2 <- matrix(c(1,2,2,3,3,0.5,4,4,5,5,1,4,6,7,9,2,1,1,7,8),10,2,byrow=TRUE)
rownames(Bae2) <- c("un1","un2","un3","un4","un5","un6","un7","un8","un9","un10")
fancyTree(tree, type="traitgram3d",X=Bae2,control=list(spin=TRUE)

The output is

enter image description here

What I want to do is, since the tree is in 3D, branches are hard to distinguish from each other.

So is there any way I can add colors to the branches?


Solution

  • Exploring the function fancyTree at github, we can see that the argument type="traitgram3d" internally calls a function traitgram3d, which in turn is a wrapper for function phylomorphospace3d. Typing

    ?phylomorphospace3d
    

    gives explanation for the argument control as

    col.edge a vector of colors of length nrow(tree$edge).

    Let's choose some colors from a Set 3 and use them to color the tree edges.

    cols <- hcl.colors(nrow(tree$edge), palette = "Set 3")
    fancyTree(tree, type = "traitgram3d",X = Bae2, 
        control = list(col.edge = cols))