【问题标题】:How is this custom color palette function being passed the 'n' argument?这个自定义调色板函数是如何传递“n”参数的?
【发布时间】:2019-11-05 14:09:08
【问题描述】:

我正在使用附加的 R 代码在 R 中创建自定义调色板,它根据级别数返回十六进制值。附加的代码完美运行,但我无法理解代码如何理解输入向量中的级别数。 'N' 是函数内函数中的一个参数(create_pal 中的 check_pal_n),但 'n' 不是您可以传递给 'create_pal' 的参数之一。

显然代码可以工作,但我想更好地了解它是如何工作的。任何帮助表示赞赏!

library(tidyverse)

# create a labeled list of colors
custom_color_hexcodes <- c(
  `blue`  = "#002F6C",
  `red`  = "#BA0C2F",
  `light blue`  = "#A7C6ED",
  `medium blue`  = "#006789",
  `dark red`  = "#631032",
  `web blue` = "#205493",
  `rich black`  = "#212721",
  `dark grey` = "#6C6463",
  `medium grey`= "#8C8983",
  `light grey`= "#CFCDC9")

# wrap that list in a callable function
custom_cols <- function(...) {
  cols <- c(...)
  if (is.null(cols))
    return (custom_color_hexcodes)
  custom_color_hexcodes[cols]
}


# There are 10 colors, so our max number of colors will be 10.
check_pal_n <- function(n, max_n) {
  if (n > max_n) {
    warning("This palette can handle a maximum of ", max_n, " values.",
            "You have supplied ", n, ".")
  } else if (n < 0) {
    stop("`n` must be a non-negative integer.")
  }
}


custom_pal <- function(fill=TRUE){
  colors <- custom_color_hexcodes
  if (fill){
    max_n <- 9
    f <- function(n) {
      check_pal_n(n, max_n)
      if (n == 1L) {
        i <- "blue"
      } else if (n == 2L) {
        i <- c("blue", "red")
      } else if (n == 3L) {
        i <- c("blue", "red", "light blue")
    } else if (n == 4L) {
      i <- c("blue", "red", "light blue", "dark red")
    } else if (n %in% 5:6) {
      ## 20120901_woc904
      i <- c("blue", "red", "light blue", "dark red",
             "dark grey", "light grey")
    } else if (n == 7L) {
      # 20120818_AMC820
      i <- c("blue", "red", "light blue", "dark red",
             "dark grey", "light grey", "medium blue")
    } else if (n >= 8L) {
      # 20120915_EUC094
      i <- c("blue", "red", "light blue", "dark red",
             "dark grey", "light grey", "medium blue", "rich black")
    }
    unname(colors[i][seq_len(n)])
  }
} else {
  max_n <- 9
  f <- function(n) {
    check_pal_n(n, max_n)
    if (n <= 3) {
      i <- c("blue", "red", "light blue")
    } else if (n %in% 4:5) {
      # i <- c("blue", "red", "light blue", "dark red")
      i <- c("blue", "red", "light blue", "dark red", "dark grey")
    } else if (n == 6) {
      # 20120825_IRC829
      i <- c("blue", "red", "light blue", "dark red",
             "dark grey", "light grey")
    } else if (n > 6) {
      # 20120825_IRC829
      i <- c("blue", "red", "light blue", "dark red",
             "dark grey", "light grey", "medium blue", "rich black",
             "web blue", "medium grey")
      }
      unname(colors[i][seq_len(n)])
    }
  }
  attr(f, "max_n") <- max_n
  f
}


scale_colour <- function(...) {
  discrete_scale("colour", "custom_cols", custom_pal(), ...)
}

scale_fill <- function(...) {
  discrete_scale("fill", "custom_cols", custom_pal(), ...)
}

【问题讨论】:

  • custom_pal 创建 n 的函数n 将作为它返回的任何函数的参数。
  • 你可能想从library(RColorBrewer)检查colorRampPalette()

标签: r ggplot2 color-palette


【解决方案1】:

假设create_pal 是指custom_pal

custom_pal 函数返回一个函数(在 custom_pal 函数中内部命名为 f),它将接受一个参数 n

https://ggplot2.tidyverse.org/reference/discrete_scale.html 解释了discrete_scale 函数:您传递给它“一个调色板函数,当使用单个整数参数(刻度中的级别数)调用时,它会返回它们应该采用的值。”在这种情况下,它被传递给custom_pal 返回的函数。

ggplot 将使用适当的参数为您调用此函数。

【讨论】:

  • 非常感谢 - 这真的很有帮助。
猜你喜欢
  • 2011-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-10
  • 2018-02-27
  • 1970-01-01
  • 2013-01-07
  • 2021-09-04
相关资源
最近更新 更多