使用 discrete_scale 生成自定义 ggplot2 比例时手动排序调色板

Manual ordering of color palette when using discrete_scale to generate custom ggplot2 scales

我创建了一个自定义的 ggplot2 fill/colour 比例尺作为 discrete_scale 的包装器,ggplot2 比例尺函数中环绕 discrete_scale 的唯一参数是 shade。我实际上根据这个参数选择了 4 个不同的调色板:浅色、中间调、深色、轮廓。

我面临的问题是,当我在比例包装函数(填充和颜色)中调用这些不同的阴影时,这些颜色的顺序会有所不同。如何确保一致的顺序?

palette_light_custom <- c("#FF61c3", "#53c1e8", "#82FF73", "#FFFA5C", "#FFA200", "#FF6D5C", "#C478FF", "#C478FF", "#C478FF")
palette_midtone_custom <- c("#f84eb7", "#f3413a", "#f48c00", "#fffa5c", "#56dc5b", "#219ed4", "#b86ef4", "#b86ef4", "#b86ef4")
palette_dark_custom <- c("#e0008a", "#eb0008", "#f48c00", "#fffa5c", "#15a736", "#0085cf", "#8745c6", "#8745c6", "#8745c6")
palette_outline_custom <- c("#a30059", "#005ba1", "#6729a7", "#FFFA5C", "#FFA200", "#FF6D5C", "#C478FF", "#C478FF", "#C478FF")

pal_light <- function() { scales::manual_pal(palette_light_custom) }
pal_midtone <- function() { scales::manual_pal(palette_midtone_custom) }
pal_dark <- function() { scales::manual_pal(palette_dark_custom) }
pal_outline <- function() { scales::manual_pal(palette_outline_custom) }

scale_colour_custom <- function(shade, ...) { 
  if (shade == "light") {
    discrete_scale("colour", "custom", pal_light_custom(), ...) 
  } else if (shade == "midtone") {
    discrete_scale("colour", "custom", pal_midtone_custom(), ...) 
  } else if (shade == "dark") {
    discrete_scale("colour", "custom", pal_dark_custom(), ...) 
  } else if (shade == "outline") {
    discrete_scale("colour", "custom", pal_outline_custom(), ...) 
  } else {
    stop("Incorrect or missing shade parameter in scale_color_custom()")
  }
}

scale_color_custom <- scale_colour_custom

scale_fill_custom <- function(shade, ...) { 
  if (shade == "light") {
    discrete_scale("fill", "custom", pal_light_custom(), ...) 
  } else if (shade == "midtone") {
    discrete_scale("fill", "custom", pal_midtone_custom(), ...) 
  } else if (shade == "dark") {
    discrete_scale("fill", "custom", pal_dark_custom(), ...) 
  } else if (shade == "outline") {
    discrete_scale("fill", "custom", pal_outline_custom(), ...) 
  } else {
    stop("Incorrect or missing shade parameter in scale_color_custom()")
  }
}

ggplot(data=iris,
       aes(x = Sepal.Length,
           color = Species, 
           fill = Spcies)) + 
  geom_density() + 
  scale_colour_custom("outline") +
  scale_fill_custom("midtone") +
  theme_minimal()

请注意图像中的填充和颜色不对齐。如何强制 discrete_scale() 选择指定调色板的顺序?

编辑

感谢您在评论中提供的额外细节;这能解决您的问题吗?

library(tidyverse)
palette_light_custom <- c("#FF61c3", "#FF6D5C", "#FFA200", "#FFFA5C", "#82FF73", "#53c1e8", "#C478FF", "#C478FF", "#C478FF")
palette_midtone_custom <- c("#f84eb7", "#f3413a", "#f48c00", "#fffa5c", "#56dc5b", "#219ed4", "#b86ef4", "#b86ef4", "#b86ef4")
palette_dark_custom <- c("#e0008a", "#eb0008", "#d97d00", "#fffa5c", "#15a736", "#0085cf", "#8745c6", "#8745c6", "#8745c6")
palette_outline_custom <- c("#ad006b", "#b50006", "#c26f00", "#f7f000", "#006e19", "#005ba1", "#6729a7", "#6729a7", "#6729a7")

scales::show_col(palette_light_custom)
scales::show_col(palette_midtone_custom)
scales::show_col(palette_dark_custom)
scales::show_col(palette_outline_custom)

pal_light <- function() { scales::manual_pal(palette_light_custom) }
pal_midtone <- function() { scales::manual_pal(palette_midtone_custom) }
pal_dark <- function() { scales::manual_pal(palette_dark_custom) }
pal_outline <- function() { scales::manual_pal(palette_outline_custom) }

scale_colour_custom <- function(shade, ...) { 
  if (shade == "light") {
    discrete_scale("colour", "custom", pal_light(), ...) 
  } else if (shade == "midtone") {
    discrete_scale("colour", "custom", pal_midtone(), ...) 
  } else if (shade == "dark") {
    discrete_scale("colour", "custom", pal_dark(), ...) 
  } else if (shade == "outline") {
    discrete_scale("colour", "custom", pal_outline(), ...) 
  } else {
    stop("Incorrect or missing shade parameter in scale_color_custom()")
  }
}

scale_fill_custom <- function(shade, ...) { 
  if (shade == "light") {
    discrete_scale("fill", "custom", pal_light(), ...) 
  } else if (shade == "midtone") {
    discrete_scale("fill", "custom", pal_midtone(), ...) 
  } else if (shade == "dark") {
    discrete_scale("fill", "custom", pal_dark(), ...) 
  } else if (shade == "outline") {
    discrete_scale("fill", "custom", pal_outline(), ...) 
  } else {
    stop("Incorrect or missing shade parameter in scale_color_custom()")
  }
}

ggplot(data=iris,
       aes(x = Sepal.Length,
           color = Species, 
           fill = Species)) + 
  geom_density() + 
  scale_colour_custom("outline") +
  scale_fill_custom("light") +
  theme_minimal()

ggplot(data=iris,
       aes(x = Sepal.Length,
           color = Species, 
           fill = Species)) + 
  geom_density() + 
  scale_colour_custom("outline") +
  scale_fill_custom("midtone") +
  theme_minimal()

ggplot(data=iris,
       aes(x = Sepal.Length,
           color = Species, 
           fill = Species)) + 
  geom_density() + 
  scale_colour_custom("outline") +
  scale_fill_custom("dark") +
  theme_minimal()