is it possible to apply scale_color/fill_manual to a single facet

1,334 views
Skip to first unread message

Ricardo Ramiro

unread,
Jun 13, 2017, 1:17:49 PM6/13/17
to ggtree
Dear all,

I am wanting to have a plot in which i have a tree on the left with red and blue clades and a stacked barplot on the right with different fill colors. My problem is that when I add the scale_color_manual, it is applied to both the tree and the barplot, as you can see in the attached file. Is there a way of applying the scale color only to the tree?

my code is:

tree_2nd_n26 <- groupClade(tree_2nd, node=26)


p
<-ggtree(tree_2nd_n26,branch.length = "none",aes(color=group))


p2<-facet_plot(p, panel = 'Taxa Relative Abundance', 
                 data = L6_freq_long_new, geom = geom_barh,
                 mapping = aes(x = freq, fill = as.factor(OTU.ID)),
                 stat='identity')

p2+scale_fill_manual(values= taxonPalette)+scale_color_manual(values=c("blue","red"))



thanks for any help,

Ramiro


Yu, Guangchuang

unread,
Jun 18, 2017, 11:17:14 PM6/18/17
to Ricardo Ramiro, ggtree
try:


p2<-facet_plot(p, panel = 'Taxa Relative Abundance', 
                 data = L6_freq_long_new, geom = geom_barh,
                 mapping = aes(x = freq, fill = as.factor(OTU.ID)),
                 stat='identity',
color='black')





--
G Yu, DK Smith, H Zhu, Y Guan, TTY Lam*. ggtree: an R package for visualization and annotation of phylogenetic trees with their covariates and other associated data, Methods in Ecology and Evolution, 2017, 8(1):28-36. doi:10.1111/2041-210X.12628
 
Homepage: https://guangchuangyu.github.io/ggtree
---
You received this message because you are subscribed to the Google Groups "ggtree" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bioc-ggtree+unsubscribe@googlegroups.com.
To post to this group, send email to bioc-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bioc-ggtree/6a174015-402e-48be-b06e-29078dd6ebb9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
--~--~---------~--~----~------------~-------~--~----~
Guangchuang Yu, PhD Candidate
State Key Laboratory of Emerging Infectious Diseases
School of Public Health
The University of Hong Kong
Hong Kong SAR, China
-~----------~----~----~----~------~----~------~--~---

Brigida Gallone

unread,
Aug 17, 2017, 5:20:30 AM8/17/17
to ggtree
Hello,

I am also trying to assign different sets of colors to different panels. Currently, if you apply a set of colors to the first panel and a second set of colors to the second panel the new scale will replace the existing one if the number of levels is the same between the two dataframes or give and error if the the number of levels is  different.

E.g.

p<-ggtree(tr)

#adding a panel with a bar chart using a 3 levels variable to fill colors

p1<-facet_plot(p, panel="Panel1", data=data1, geom=geom_barh, mapping=aes(x=value, fill= as.factor(groups)), stat='identity') + scale_fill_manual(values=c("#359946", "#EA2227", "#C8D92D"))

#adding a second panel with a different bar chart using a 2 levels variable to fill colors

p2<-facet_plot(p1, panel="Panel2", data=data2, geom=geom_barh, mapping=aes(x=value, fill= as.factor(groups2)), stat='identity') + scale_fill_manual(values=c("blue", "red"))

In this case you will receive as error:

"Insufficient values in manual scale, 5 needed but only 2 provided". This is because is summing up the levels from the two different panels. If you use a color scale for the first panel and a fill scale for the second you can partially fix it but the result is not 100% the desired result. Is there a way to fix this behavior?

Thanks!

Brigida
To unsubscribe from this group and stop receiving emails from it, send an email to bioc-ggtree...@googlegroups.com.

To post to this group, send email to bioc-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bioc-ggtree/6a174015-402e-48be-b06e-29078dd6ebb9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Yu, Guangchuang

unread,
Aug 17, 2017, 5:45:54 AM8/17/17
to Brigida Gallone, ggtree

To unsubscribe from this group and stop receiving emails from it, send an email to bioc-ggtree+unsubscribe@googlegroups.com.

To post to this group, send email to bioc-...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Brigida Gallone

unread,
Aug 17, 2017, 6:11:57 AM8/17/17
to ggtree
I see! Thank you very much for your time.

Brigida

Michael Tobler

unread,
Mar 8, 2019, 3:32:51 PM3/8/19
to ggtree
Hi everybody
I struggle with a similar problem. I want the fill of my box plots to match the edge color of my tree, and the color of the box plots be black across all groups. The first part was not a problem:

tr <- rtree(20)

cls <- list(c1=c(3, 5, 6, 1, 9, 11, 15, 17, 12, 19))
tr <- groupOTU(tr, cls)

p <- ggtree(tr, size=1.5, branch.length = "none") + aes(color=group) + scale_color_manual(values=c('#DECF3F', '#5DA5DA'))

d4 = data.frame(id=rep(tr$tip.label, each=20), 
                val=as.vector(sapply(1:20, function(i) 
                  rnorm(20, mean=i)))

p2 <- facet_plot(p, panel="Boxplot", data=d4, geom_boxploth, mapping = aes(x=val, group=label, fill = group)) 
p2 <- p2 +  scale_fill_manual(values=c('#DECF3F', '#5DA5DA'))
p2  

However, if I add color = 'black' to aes() as suggested above, I receive the following error:  "Insufficient values in manual scale. 3 needed but only 2 provided."

Does anyone have a workaround for this problem?

Thank you and best wishes, Michi
To unsubscribe from this group and stop receiving emails from it, send an email to bioc-ggtree...@googlegroups.com.

To post to this group, send email to bioc-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bioc-ggtree/6a174015-402e-48be-b06e-29078dd6ebb9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages