rggplot2ggtree

how to set specific tip label color in ggtree


I knew that I can set tip color by grouping the tip labels like this:

library(ggtree)
tree<-read.tree(text="(A,(B,C));"
dd <-data.frame(taxa=c("A","B","C"),place=c("s1","s1","s2"))
p<-ggtree(tree)
p<-p %<+% dd +geom_tiplab(aes(color=place))
p

So that random sets of color are applied according to the place group
But how do I specify my own color? (for example I want s1 in black and s2 in red)


Solution

  • You can set the tip color by using groupOTU on the tree and the branches.

    tree<-read.tree(text="(A,(B,C));")
    branches <- list(A=1,B=2,C=3)
    tree <- groupOTU(tree,branches)
    
    p <- ggtree(tree) + geom_tiplab(aes(color=group))+
      scale_color_manual(values=c(A = "#E7B800",B= "#FC4E07", C = "darkgreen"))
    p
    

    enter image description here