更改ggplot中的默认调色板

时间:2012-05-08 18:57:33

标签: 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)
}

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

4 个答案:

答案 0 :(得分:18)

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

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

同样适用于填充量表。

答案 1 :(得分: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)
}

(它接受一个可选的set参数,只是为了表明你不仅限于一个调色板。)好的,现在我们创建一个“scale”,我称之为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"'))

这里重要的是定义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')

要将其设为默认值,只需使用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))

plot showing the new palette

答案 2 :(得分:4)

只需指定一个具有所需比例名称的变量:

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

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

get('scale_colour_discrete', envir = globalenv())

答案 3 :(得分:4)

17天前(2020-05-19)work by Carson Sievert has been merged进入ggplot2的开发版本,该版本允许使用选项(ggplot2.discrete.fill和{{1} }):

ggplot2.discrete.color

它还可以指定几个不同尺寸的托盘(使用的托盘要最小):

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

不幸的是,它没有采用调色板功能(如您的options(ggplot2.discrete.color = list(c("red", "#af01ef"), custom.colors(99))) ),但确实具有了缩放功能(因此,您可以按照@ernestA's answer所述构建一个调色板功能,以产生所需的警告)。

来自NEWS

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