简体   繁体   English

ggplot facet_grid 中的单独调色板

[英]Separate palettes for facets in ggplot facet_grid

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.我正在使用 ggplot 的 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).能够仍然使用 facet_grid 而不是使用 grobs(我不太熟悉),这将是令人惊奇的。

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.用透明度伪造色标可能是您的最佳选择,除非您愿意在网格/gtable 级别组合多个部分。

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))

在此处输入图像描述

Another option would be to change the color brightness based on the factor level.另一种选择是根据因子级别更改颜色亮度。 You can use the colorspace package or the shades package for this.您可以为此使用colorspace 包shades 包 Here, I prefer the colorspace package because of its slightly simpler syntax and because you only need to modify specific colors.在这里,我更喜欢 colorspace 包,因为它的语法稍微简单一些,而且你只需要修改特定的颜色。

Disadvantage - Adding a legend of grey values could become very hacky here.缺点 - 在此处添加灰度值的图例可能会变得非常麻烦。 Another option would be to just overlay gray values in different shades.另一种选择是仅以不同的阴影覆盖灰度值。 This will require two fill scales (easiest with the ggnewscale package ).这将需要两个填充比例(使用ggnewscale 包最简单)。

Advantage of the latter - it's much shorter and seems less hacky.后者的优势 - 它更短并且看起来不那么hacky。 Disadvantage possibly that it can be tricky to figure out the alpha and the exact grey values used so to get the colors saturated enough to your liking.缺点可能是很难确定 alpha 和使用的确切灰度值,以便使颜色饱和到您喜欢的程度。

Option 1 - change color brightness选项 1 - 更改颜色亮度

library(tidyverse)
library(colorspace)

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

## get colors of your choice
my_cols <- RColorBrewer::brewer.pal(length(unique(diamonds$cut)), "Set1")
## for easier assignment, name the colors
names(my_cols) <- unique(diamonds$cut)

## assign the color to the category
df_grad <-
  diamonds %>%
  ## make your colored aesthetic an ordered factor for better control, 
  ## and change the brightness according to the factor level
  mutate(
    arbitrary_ind = as.integer(ordered(arbitrary)),
    new_cols = my_cols[cut],
    ## now darken or lighten according to the rank
    arbitrary_dark = darken(new_cols, amount = arbitrary_ind / 10)
  )

## use this new color for your fill with scale_identity
ggplot(df_grad) +
  geom_bar(aes(clarity, fill = arbitrary_dark)) +
  facet_grid(cut~.) +
  scale_fill_identity()

Option 2 - add layer of grey scales选项 2 - 添加灰度层

ggplot(diamonds) +
  geom_bar(aes(clarity, fill = cut), show.legend = F) +
  ## add new layer of grey values with an alpha
  ggnewscale::new_scale_fill() +
  geom_bar(aes(clarity, fill = arbitrary), alpha = .5) +
  scale_fill_brewer(palette = "Greys") +
  facet_grid(cut~.) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM