Separate palettes for facets in ggplot facet_grid

时光毁灭记忆、已成空白 提交于 2019-12-01 05:03:17

问题


Question
How can I use a different color palette for each facet? Ideally I would like to have a generic legend in gray to serve as a reference.

I'm working on a visualization using ggplot's facet_grid. The layout is fine, but I would like to use a distinct color palette for every row in the grid. My goal is to use a similarly-shaded gradient for every palette and then tie them together with a grayscale legend. I'm would like to do this to maintain internal color-coding consistency within a larger set of graphics. It would amazing to be able to still use facet_grid instead of using grobs (with which I am vastly less familiar).

I've included an example to work with using the diamonds data set and an arbitrary grouping to approximate what my data looks like.

data(diamonds) 
diamonds$arbitrary = sample(c("A", "B", "C"), length(diamonds$cut), replace = TRUE)

blues = brewer.pal(name="Blues", n=3)
greens = brewer.pal(name="Greens", n=3)
oranges = brewer.pal(name="Oranges", n=3)
purples = brewer.pal(name="Purples", n=3)

ggplot(diamonds) + 
  geom_bar(aes(x = clarity, stat = "bin", fill = arbitrary, group = arbitrary)) + 
  facet_grid(cut~.) + 
  # Here I assign one palette... is this where I could also
  # designate the other palettes?
  scale_fill_manual(values = blues)

Thank you!


回答1:


faking a colour scale with transparency might be your best option, unless you're willing to combine multiple pieces at the grid/gtable level.

ggplot(diamonds) + 
  geom_bar(aes(x = clarity, stat = "bin", fill = cut, 
               alpha=arbitrary, group = arbitrary)) + 
  facet_grid(cut~.) + 
  scale_fill_manual(values = brewer.pal(name="Set1", n=5), guide="none") +
  scale_alpha_manual(values=c(0.8, 0.6, 0.4))



来源:https://stackoverflow.com/questions/33221794/separate-palettes-for-facets-in-ggplot-facet-grid

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!