繁体   English   中英

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

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

我创建了一个自定义 ggplot2 填充/色阶作为discrete_scale discrete_scaleshade 我实际上有 4 种基于此参数选择的不同调色板:浅色、中间调、深色、轮廓。

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

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

示例_light.png

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

example_midtone.png

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

示例_dark.png

暂无
暂无

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

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