Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何将此自定义调色板函数传递给';n';论点_R_Ggplot2_Color Palette - Fatal编程技术网

R 如何将此自定义调色板函数传递给';n';论点

R 如何将此自定义调色板函数传递给';n';论点,r,ggplot2,color-palette,R,Ggplot2,Color Palette,我使用附加的R代码在R中创建一个自定义调色板,该调色板根据级别数返回十六进制值。附加的代码工作得很好,但我很难理解代码如何理解输入向量中的级别数N'是函数中的一个参数(在create\u pal中检查\u pal\N),但N'不是可以传递给“create\u pal”的参数之一 显然代码是有效的,但我想更好地理解它是如何工作的。感谢您的帮助 library(tidyverse) # create a labeled list of colors custom_color_hexcodes &l

我使用附加的R代码在R中创建一个自定义调色板,该调色板根据级别数返回十六进制值。附加的代码工作得很好,但我很难理解代码如何理解输入向量中的级别数N'是函数中的一个参数(在create\u pal中检查\u pal\N),但N'不是可以传递给“create\u 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(), ...)
}
库(tidyverse)
#创建带标签的颜色列表

自定义颜色六进制代码假设通过
create\u pal
您的意思是
custom\u pal

custom\u-pal
函数返回一个函数(在
custom\u-pal
函数中内部命名为
f
),该函数将接受一个参数
n

解释
discrete\u scale
函数:传递“一个调色板函数,当使用单个整数参数(标尺中的级别数)调用该函数时,该函数返回它们应采用的值。”在这种情况下,传递的是
custom\u pal
返回的函数


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

custom\u pal
创建n的函数
n
将是它返回的任何函数的参数。您可能需要从
library(RColorBrewer)
中检查
colorrmpette()
。非常感谢-这非常有用。