如何在R中为ggplot自定义调色板? [英] How to customize a color palette in r for ggplot?

查看:156
本文介绍了如何在R中为ggplot自定义调色板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码绘制北美温度变化图:

I'm using this code to plot a map of temperature change in North America:

ggplot(maps, aes(y=Latitude, x=Longitude, z=variable)) +
  ggtitle(title)+
  scale_fill_brewer(palette = "Spectral")+
  geom_contour_filled(breaks = c(-Inf,-2., -1.5, -1., -0.5, 0, 0.5, 1, 1.5, 2, 3, 4, 5, 7, 9, 11,Inf))+
  xlim(-146,-44)+
  ylim(35,90)+
  theme(plot.title = element_text(hjust = 0.5))

我想更改调色板以实现更好的可视化.当前,它编写 scale_fill_brewer(palette ="Spectral"),我将其更改为自定义调色板

And I want to change the color palette for better visualization. Currently, it writes scale_fill_brewer(palette = "Spectral"), and I'm going to change it into a custom palette

colors <- c(rgb(10,40,100,max=255),rgb(51,102,217,max=255),rgb(105,138,236,max=255),rgb(151,180,250,max=255),rgb(204,217,255,max=255),rgb(255,245,204,max=255),rgb(255,224,153,max=255),rgb(255,203,102,max=255),rgb(255,180,51,max=255),rgb(255,140,51,max=255),rgb(255,85,0,max=255),rgb(230,40,30,max=255),rgb(191,0,0,max=255),rgb(140,0,0,max=255),rgb(108,0,0,max=255),rgb(110,0,70,max=255))

使用 scale_fill_brewer(palette = colors)将以错误结束,我也尝试了 palette(colors),它也无法正常工作.

Using scale_fill_brewer(palette = colors)will end with error, I also tried palette(colors), and it also doesn't work.

如何自定义参数可以识别的调色板?

How can I custom a palette that the argument can recognize?

推荐答案

如果我正确理解了您的问题,则希望创建一个自定义的 scale _... 函数.简而言之,这正在更深地浸入源代码中.

If I understand your question correctly, you want to create a custom scale_... function. In short, this is dipping somewhat deeper into the source code.

我将展示一种方法,该方法将简单地修改RColorBrewer包,scales和ggplot2包中的现有功能.

I will show one approach which will simply modify the existing functions in the packages RColorBrewer, scales, and of course ggplot2.

  1. 核心是修改 RcolorBrewer :: brewer.pal ,我想这基本上是您想要的.看到代码,它会从颜色列表中选择合适的颜色,具体取决于您的"n".这就是您需要手动创建的内容.我只是复制了调色板"YlOrBr"中的颜色.需要考虑的事情:Brewer调色板不是随机的,它们已经过测试和创建,可以识别出打印,复印和色盲,因此我不确定创建自己的调色板是否非常聪明.看看 https://colorbrewer2.org/可以找到合适的调色板.
  2. 修改基础颜色选择器 scales ::: brewer_pal
  3. 修改 scale_fill/scale_color 函数
  1. The core is to modify RcolorBrewer::brewer.pal, which I guess is bascially what you want. Seeing the code, it selects from a list of colors the right one, depending on your "n". And this is what you need to manually create. I just copied the colors from the palette "YlOrBr". Something to consider: Brewer palettes are not random, they have been tested and created for being recognizable for printing, copying and colorblindness, so I am not sure if it is very clever to create your own palette. Have a look at https://colorbrewer2.org/ to find suitable palettes.
  2. modify the underlying color selector scales:::brewer_pal
  3. modify the scale_fill/scale_color function

我将功能归结为核心,因此它们不会进行常规检查并且灵活性较差.您可以修改原始功能以恢复该功能.

I've boiled down the functions to the cores, so they won't make the usual checks and are less flexible. You can modify the original functions to get this functionality back.

library(ggplot2)

mybrewerpal <- function(n, name) {# modified RcolorBrewer::brewer.pal
## the first call to switch would not be necessary in this example,
## but I leave it in order to make the underlying structure in brewer.pal clearer
  switch(name, mypal = switch(n - 2, rgb(c(255, 254, 217), c(247, 196, 95), c(188, 79, 14), maxColorValue = 255),
    rgb(c(255, 254, 254, 204), c(255, 217, 153, 76), c(212, 142, 41, 2), maxColorValue = 255),
    rgb(c(255, 254, 254, 217, 153), c(255, 217, 153, 95, 52), c(212, 142, 41, 14, 4), maxColorValue = 255),
    rgb(c(255, 254, 254, 254, 217, 153), c(255, 227, 196, 153, 95, 52), c(212, 145, 79, 41, 14, 4), maxColorValue = 255),
    rgb(c(255, 254, 254, 254, 236, 204, 140), c(255, 227, 196, 153, 112, 76, 45), c(212, 145, 79, 41, 20, 2, 4), maxColorValue = 255),
    rgb(c(255, 255, 254, 254, 254, 236, 204, 140), c(255, 247, 227, 196, 153, 112, 76, 45), c(229, 188, 145, 79, 41, 20, 2, 4), maxColorValue = 255),
    rgb(c(255, 255, 254, 254, 254, 236, 204, 153, 102), c(255, 247, 227, 196, 153, 112, 76, 52, 37), c(229, 188, 145, 79, 41, 20, 2, 4, 6), maxColorValue = 255)
  ))
}

brewer_pal2 <- # modified from scales:::brewer_pal
  function() { # stripped down all arguments, just to show the core
    function(n) {
      mybrewerpal(n, "mypal") ##modified, usually this is selected by a function 
      ## with type and name as arguments, selecting a palette from a list called scales:::brewer
    }
  }

scale_fill_custom <- ### modified from scale_fill_brewer, removed some arguments
  function (..., aesthetics = "fill") {
    discrete_scale(aesthetics, "custom", brewer_pal2(), ...) ## give a new name to the
    ## scale, it will create a new Scale object.  
  }

p <- 
  ggplot(mtcars, aes(x = mpg, y = disp)) +
  scale_fill_custom()

p + geom_point(shape = 21, aes(fill = as.factor(cyl))) 

p + geom_point(shape = 21, aes(fill = as.factor(carb))) 

这篇关于如何在R中为ggplot自定义调色板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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