colouring subset of tip labels

1,284 views
Skip to first unread message

Adam Witney

unread,
Nov 22, 2016, 8:22:27 AM11/22/16
to ggtree
Hi,

I am trying to colour a subset of 75 tip labels a specific colour, and colour the rest black. I think i can do it using something like so:

ggtree(tr) + geom_tiplab(aes(subset=node==1))
ggtree(tr) + geom_tiplab(aes(subset=node==2))
ggtree(tr) + geom_tiplab(aes(subset=node==3))
etc

but this doesn't seem like a good approach. How can I put in a vector or list into this syntax

e,g something like this (using my tip labels rather than node number)

mylabels <- c("001-1", "002-2", "010-1)
ggtree(tr) + geom_tiplab(aes(subset=node==mylabels))

thanks

Adam

Ryan Kepler

unread,
Nov 22, 2016, 2:14:05 PM11/22/16
to ggtree
Adam,

Take a look at the groupOTU function

taxa_to_color<-c("taxon1", "taxon2", "taxon3")
tree<-groupOTU(tree, taxa_to_color)
p<-ggtree(tree) +
     geom_text2(aes(label=label, subset=isTip, color=group)) +
     scale_color_manual(values = c("black", "red"))
print(p)

Since you have 75, it could be tedious to type in all those names, but you could import a table and bind it to the tree.  Say you have a column called name_color with "red" or "black" associated with the appropriate tip.  You could do:

p<-ggtree(tree)
p<-p %<&% table + 
     geom_text2(aes(label=label, subset=isTip, color=name_color)) +
     scale_color_manual(values = c("black", "red"))
print(p)

I think one of those should work for you.  Some details on these methods available here.

Ryan
Reply all
Reply to author
Forward
0 new messages