问题描述
问题
如何为每个方面使用不同的调色板?理想情况下,我希望将灰色的通用图例作为参考。
我正在使用ggplot的facet_grid进行可视化。布局很好,但我想为网格中的每一行使用不同的调色板。我的目标是对每个调色板使用类似阴影的渐变,然后用灰度图例将它们连接在一起。我想这样做是为了在一组更大的图形中保持内部颜色编码的一致性。能够仍然使用facet_grid而不是使用grobs(我并不太熟悉),这将会令人惊讶。
我已经包含了一个使用钻石数据集和任意分组来近似我的数据的例子。
数据(钻石)
钻石$任意=样品(c(A,B,C ),长度(钻石$ cut),替换= 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 =任意,组=任意))+
facet_grid(cut〜。)+
#在这里,我分配一个调色板...这是我也可以
#指定其他调色板?
scale_fill_manual(values = blues)
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!
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))
这篇关于在ggplot facet_grid中为facet分隔调色板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!