gpt4 book ai didi

r - 如何在 r 中为 ggplot 自定义调色板?

转载 作者:行者123 更新时间:2023-12-04 13:35:22 28 4
gpt4 key购买 nike

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

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") ,我要把它改成自定义调色板
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) ,它也不起作用。

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

最佳答案

如果我正确理解您的问题,您想创建一个自定义 scale_...功能。简而言之,这是对源代码的深入研究。

我将展示一种方法,该方法将简单地修改包 RColorBrewer、scales 和 ggplot2 中的现有函数。

  • 核心是修改RcolorBrewer::brewer.pal ,我想这基本上是你想要的。查看代码,它会从颜色列表中选择正确的颜色,具体取决于您的“n”。这就是您需要手动创建的内容。我只是从调色板“YlOrBr”中复制了颜色。需要考虑的事情:Brewer 调色板不是随机的,它们已经过测试和创建,可用于打印、复印和色盲,因此我不确定创建自己的调色板是否非常聪明。看看https://colorbrewer2.org/找到合适的调色板。
  • 修改底层颜色选择器 scales:::brewer_pal
  • 修改scale_fill/scale_color功能

  • 我已经将功能归结为核心,因此它们不会进行通常的检查并且不太灵活。您可以修改原始函数以恢复此功能。

    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 - 如何在 r 中为 ggplot 自定义调色板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62462030/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com