34

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

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

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

4

4 回答 4

20

要重新定义默认色标,您也可以重新定义ggplot函数:

ggplot <- function(...) ggplot2::ggplot(...) + scale_color_brewer(palette="Spectral")

这同样适用于填充比例。

于 2013-05-08T09:54:13.287 回答
18

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

options(ggplot2.discrete.color = c("red", "#af01ef"))

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

options(ggplot2.discrete.color = list(c("red", "#af01ef"), custom.colors(99)))

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

来自新闻

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

于 2020-06-05T13:28:55.617 回答
8

@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)
}

(它接受一个可选set参数,只是为了表明您不限于单个调色板。)好的,现在我们创建一个“比例”,我称之为magazine. 它基于 ggplot 的 brewer scale 并且代码非常难看:

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"'))

这里重要的是定义output_setggplot 调用哪个函数来获取颜色名称/代码。此外,如果您需要额外的参数,则必须包含这些参数,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')

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

set_default_scale("colour", "discrete", "magazine") 
set_default_scale("fill", "discrete", "magazine") 

就是这样。

> qplot(mpg, wt, data=mtcars, colour=factor(carb), fill=factor(carb))

显示新调色板的图

于 2012-05-09T01:02:04.110 回答
6

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

scale_colour_discrete <- function(...)
  scale_colour_manual(..., values = c('dodgerblue1', *))

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

get('scale_colour_discrete', envir = globalenv())
于 2017-06-01T12:09:57.993 回答