【问题标题】:Manual ordering of color palette when using discrete_scale to generate custom ggplot2 scales使用discrete_scale生成自定义ggplot2比例时手动排序调色板
【发布时间】:2021-05-08 06:07:01
【问题描述】:

我创建了一个自定义 ggplot2 填充/颜色比例作为 discrete_scale 的包装,ggplot2 比例函数中唯一的参数是 discrete_scaleshade。我实际上有 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() 选择指定调色板的顺序?

【问题讨论】:

    标签: r ggplot2 color-palette


    【解决方案1】:

    编辑

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

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

    【讨论】:

    • 谢谢,但这仍然不正确,即使在您发布的示例中,颜色也不对齐。
    • 如果您希望它们相同,为什么不能使用相同的调色板:scale_colour_custom("midtone") + scale_fill_custom("midtone")(即完全摆脱“轮廓”)?
    • 轮廓是一组不同的颜色 - 相同基色的较深色调。从上面的代码中可以看出这一点......
    猜你喜欢
    • 2014-07-05
    • 2018-04-03
    • 1970-01-01
    • 2021-08-06
    • 2015-08-31
    • 2014-06-10
    • 2017-08-15
    • 1970-01-01
    • 2014-11-29
    相关资源
    最近更新 更多