更改ggplot中的默认调色板

Ern*_*t A 22 r ggplot2

我编写了一个返回颜色名称向量的函数:

custom.colors <- function(n) {
  palette <- c("dodgerblue1", "skyblue4", "chocolate1", "seagreen4",
               "bisque3", "red4", "purple4", "mediumpurple3",
               "maroon", "dodgerblue4", "skyblue2", "darkcyan",
               "darkslategray3", "lightgreen", "bisque",
               "palevioletred1", "black", "gray79", "lightsalmon4",
               "darkgoldenrod1")
  if (n > length(palette))
    warning('palette has duplicated colours')
  rep(palette, length.out=n)
}
Run Code Online (Sandbox Code Playgroud)

我希望ggplot默认使用上面的函数来生成调色板.也许只适用于离散尺度.scale_manual()每次使用都太过拖累了.可能吗?

Hol*_*ndl 20

要重新定义默认色标,您还可以重新定义该ggplot功能:

ggplot <- function(...) ggplot2::ggplot(...) + scale_color_brewer(palette="Spectral")
Run Code Online (Sandbox Code Playgroud)

同样适用于填充比例.

  • 将连续变量映射到颜色时,这可能会造成麻烦 (11认同)

jan*_*glx 14

17 天前 (2020-05-19) Carson Sievert 的工作已合并到 ggplot2 的开发版本中,该版本允许使用选项 ( ggplot2.discrete.fill& ggplot2.discrete.color)指定默认的离散调色板:

options(ggplot2.discrete.color = c("red", "#af01ef"))
Run Code Online (Sandbox Code Playgroud)

它还允许指定几个不同尺寸的托盘(使用最小的足够的调色板):

options(ggplot2.discrete.color = list(c("red", "#af01ef"), custom.colors(99)))
Run Code Online (Sandbox Code Playgroud)

不幸的是,它不采用调色板功能(如您的custom.colors),但确实采用了缩放功能(因此您可以按照@ernestA 的回答中的概述构建一个,以产生您想要的警告)。

来自新闻

现在可以通过options()of ggplot2.discrete.colour和配置默认的离散色标ggplot2.discrete.fill。当设置为具有足够长度的颜色代码字符向量(或字符向量列表)时,这些颜色用于默认比例。有关help(scale_colour_discrete) 更多详细信息和示例,请参阅(@cpsievert,#3833)。


Ern*_*t A 7

@baptiste向我指了一个留言板帖子,提到了set_default_scale可以用来设置默认调色板的功能.但是,以下解决方案仅适用于旧版本的ggplot2.

首先,我们需要一个生成颜色名称或代码的函数.我打电话给我magazine.colours:

magazine.colours <- function(n, set=NULL) {
  set <- match.arg(set, c('1', '2'))
  palette <- c("red4", "darkslategray3", "dodgerblue1", "darkcyan",
               "gray79", "black", "skyblue2", "dodgerblue4",
               "purple4", "maroon", "chocolate1", "bisque3", "bisque",
               "seagreen4", "lightgreen", "skyblue4", "mediumpurple3",
               "palevioletred1", "lightsalmon4", "darkgoldenrod1")
  if (set == 2)
    palette <- rev(palette)
  if (n > length(palette))
    warning('generated palette has duplicated colours')
  rep(palette, length.out=n)
}
Run Code Online (Sandbox Code Playgroud)

(它接受一个可选set参数,只是为了表明你不仅限于一个调色板.)好了,现在我们创建一个"比例",我打电话给他magazine.它基于ggplot的啤酒规模而且代码非常难看:

ScaleMagazine <- proto(ScaleColour, expr={
  objname <- 'magazine'
  new <- function(., name=NULL, set=NULL, na.colour='yellowgreen',
                  limits=NULL, breaks = NULL, labels=NULL,
                  formatter = identity, variable, legend = TRUE) {
    b_and_l <- check_breaks_and_labels(breaks, labels)
    .$proto(name=name, set=set, .input=variable, .output=variable,
            .labels = b_and_l$labels, breaks = b_and_l$breaks,
            limits= limits, formatter = formatter, legend = legend,
            na.colour = na.colour)
  }
  output_set <- function(.) {
    missing <- is.na(.$input_set())
    n <- sum(!missing)
    palette <- magazine.colours(n, .$set)
    missing_colour(palette, missing, .$na.colour)
  }
  max_levels <- function(.) Inf
})
scale_colour_magazine <- ScaleMagazine$build_accessor(list(variable = '"colour"'))
scale_fill_magazine <- ScaleMagazine$build_accessor(list(variable = '"fill"'))
Run Code Online (Sandbox Code Playgroud)

这里重要的是定义output_set哪个是ggplot调用以获取颜色名称/代码的函数.此外,如果您需要额外的参数,那些必须包括在内new,以后可以作为访问.$argument_name.在上面的示例中,output_set只需调用magazine.colours.

现在,检查新的比例确实有效:

qplot(mpg, wt, data=mtcars, shape=21,
      colour=factor(carb), fill=factor(carb)) +
  scale_colour_magazine(set='1') +
  scale_fill_magazine(set='1')
Run Code Online (Sandbox Code Playgroud)

要将其设为默认值,只需使用即可set_default_scale.

set_default_scale("colour", "discrete", "magazine") 
set_default_scale("fill", "discrete", "magazine") 
Run Code Online (Sandbox Code Playgroud)

那就是它.

> qplot(mpg, wt, data=mtcars, colour=factor(carb), fill=factor(carb))
Run Code Online (Sandbox Code Playgroud)

情节显示新的调色板


fly*_*eep 6

只需使用所需比例的名称分配一个变量:

scale_colour_discrete <- function(...)
  scale_colour_manual(..., values = c('dodgerblue1', *))
Run Code Online (Sandbox Code Playgroud)

这是有效的,因为 ggplot 将尽可能从全局环境中获取其默认比例,类似于:

get('scale_colour_discrete', envir = globalenv())
Run Code Online (Sandbox Code Playgroud)

  • 注意:这仅在代码在全局环境中执行时才有效。如果 ggplot2 代码在函数环境中使用,则这将不起作用。 (2认同)