Is it possible to color tip points and OTUS with different colors?

1,412 views
Skip to first unread message

jeama...@gmail.com

unread,
Feb 1, 2017, 12:43:13 PM2/1/17
to ggtree
I'm able to color the tips (although the color order is incorrect, so please check.)

library(ggtree)
set.seed(123)
tree
<- rtree(10)

#Colored tips
data
<- data.frame(taxa=c("t1","t2","t3","t4","t5","t6","t7","t8","t9","t10"),place=c(rep("red",5),rep("blue",5)))
p
<- ggtree(tree)
p
<- p %<+% data + geom_tippoint(aes(color=place)) + geom_tiplab()
p
#Note that the colors are inverted. t1 to t5 should be blue, not red, and the contrary for t6 to t10. Is this a bug?

And color OTUS

#Colored OTUS
cls
<- list(c1=c("t1", "t2","t8","t7"),
            c2
=c("t3", "t4", "t10"),
            c3
=c( "t9","t6","t5"))
tree1
<- groupOTU(tree, cls)
p1
<- ggtree(tree1, aes(color=group)) +
        scale_color_manual
(values=c("black", "red","green","blue"))

But I'm unable to combine them?

#Combined?
p2
<- p1 %<+% data + geom_tippoint(aes(color=place))
p2
Error: Insufficient values in manual scale. 6 needed but only 4 provided.

How can I create plots in which the edges have certain color and the tip a different one? Why is the order of the colors in p inverted?

Ryan Kepler

unread,
Feb 1, 2017, 2:29:41 PM2/1/17
to ggtree
In your first tree, the colors are not set by the data, "red" and "blue" are just the two classes of things being sorted.  It just so happens the default colors are kind of the inverse of what you want, but they are not really "blue" and "red". To get the color scheme you want for p try this:

p <- ggtree(tree)
p <- p %<+% data + geom_tippoint(aes(color=place)) + geom_tiplab() + scale_color_manual(values = c("red", "blue"))
p

For the error you are seeing at p2, you need 6 colors but for p2 you only set for with scale_color_manual (again, because the colors in your data are group names, not designations that carry over into the graphic). To get at the result you want try the following:

p2 <- ggtree(tree1, aes(color=group))
p2
p3 <- p2 %<+% data + geom_tippoint(aes(color=place)) + 
  scale_color_manual(values=c("black", "red","red","green", "blue", "blue")) 
p3

I had to work to get the order right, but I think this is right.  Maybe someone with more insight into ggtree can explain the pattern of color associations.

jeama...@gmail.com

unread,
Feb 1, 2017, 5:20:41 PM2/1/17
to ggtree
Dear Ryan,

Thank you for your answer. That solves part of my problem. Unfortunately, my tree is incredibly big and unless I get a clue on how to order the colors in the scale, I won't be able to apply your solution. As of now, I have the colors of the otus and a dataframe with the color of the tips.

Ryan Kepler

unread,
Feb 1, 2017, 8:09:56 PM2/1/17
to ggtree
Maybe this post on stackoverflow will be helpful. From the example above this would be:
p3 <- p2 %<+% data + geom_tippoint(aes(color=place)) + 
  scale_color_manual(na.value = "black", values=c(red ="red", c1 = "red",c2 ="green", c3 ="blue", blue ="blue", "black")) 
p3
 
With this solution you have to add "black" in the values =c() portion.

Good luck,

Ryan
Reply all
Reply to author
Forward
0 new messages