更改ggplot中的默认调色板 [英] change the default colour palette in ggplot

查看:566
本文介绍了更改ggplot中的默认调色板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  custom.colors<  -  function(n) {
palette< -c(dodgerblue1,skyblue4,chocolate1,seagreen4,
bisque3,red4,purple4,mediumpurple3,
褐色,dodgerblue4,skyblue2,darkcyan,
darkslategray3,lightgreen,素描,
palevioletred1,黑色,gray79 (调色板))
警告('调色板有重复的颜色')
rep(调色板,length.out = n)
}

我希望ggplot使用上述函数来生成调色板默认。也许只适用于离散的尺度。每次使用 scale_manual()都会造成太大的阻力。是否有可能?

解决方案

@baptiste指向我的留言板帖子,其中提到了 set_default_scale 可用于设置默认调色板。不过,以下解决方案仅适用于旧版本的ggplot2。



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

  magazine.colours<  - 函数(n,set = NULL){
set < - match.arg(set,c('1','2'))
调色板<-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 colors')
rep(palette,length .out = n)
}

(它接受一个可选的设置参数只是为了表明你不限于一个调色板。)好吧,现在我们创建一个缩放,我称之为 magazine 。它基于ggplot的brewer规模,代码非常难看:

  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 (。)颜色)
}
output_set< - 函数(。){
缺少< - is.na(。$ input_set())
n< - sum(!missing )
palette< - magazine.colours(n,。$ set)
missing_colour(palette,missing,。$ na.colour)
}
max_levels< - function( 。)Inf
})
sc ale_colour_magazine< - ScaleMagazine $ build_accessor(list(variable =''color''))
scale_fill_magazine< - ScaleMagazine $ build_accessor(list(variable =''fill''))
$ b

这里最重要的是定义 output_set 这是ggplot调用的函数获取颜色名称/代码。另外,如果你需要额外的参数,那些参数必须包含在 new 中,随后可以被访问为。$ argument_name 。在上面的例子中, output_set 只需调用 magazine.colours



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

  qplot(mpg,wt,data = mtcars,shape = 21 ,
color = factor(carb),fill = factor(carb))+
scale_colour_magazine(set ='1')+
scale_fill_magazine(set ='1')

要使其成为默认值,只需使用 set_default_scale

  set_default_scale(color,discrete,magazine)
set_default_scale(fill,discrete,杂志)

就是这样。

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


I have written a function that returns a vector of color names:

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

I would like ggplot to use the above function to generate the palette by default. Maybe only for discrete scales. Using scale_manual() every time is too much of a drag. Is it possible?

解决方案

@baptiste pointed me to a message board post that mentions the function set_default_scale which can be used to set a default palette. The following solution only works with old versions of ggplot2, though.

First we need a function that produces colour names or codes. I called mine 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)
}

(It accepts an optional set argument just to show that you are not restricted to a single palette.) Ok, now we create a "scale", which I called magazine. It's based on ggplot's brewer scale and the code is pretty ugly:

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

The important thing here is to define output_set which is the function that ggplot calls to get the colour names/codes. Also, if you need extra arguments, those must be included in new and later can be accessed as .$argument_name. In the example above, output_set simply calls magazine.colours.

Now, check the new scale does actually work:

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

To make it the default, simply use set_default_scale.

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

And that'll be it.

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

这篇关于更改ggplot中的默认调色板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆