What is a “good” palette for divergent colors in R? (or: can viridis and magma be combined together?)

后端 未结 4 1611
既然无缘
既然无缘 2020-12-07 14:11

I am interested in having a \"good\" divergent color pallette. One could obviously use just red, white, and blue:

img <- function(obj, nam) {
  image(1:le         


        
4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-07 14:19

    Library RColorBrewer provides beautiful palettes for =<13 colors. For example, palette BrBG shows diverging colors from brown to green.

    library(RColorBrewer)
    display.brewer.pal(11, "BrBG")
    

    Which can be expanded to a less informative palette by creating palettes to and from a mid-point color.

    brbg <- brewer.pal(11, "BrBG")
    cols <- c(colorRampPalette(c(brbg[1], brbg[6]))(51), 
        colorRampPalette(c(brbg[6], brbg[11]))(51)[-1])
    

    Analogically, using your choice of viridis and magma palettes, you can try finding a similarity between them. This could be a point, where to join the palettes back to back.

    select.col <- function(cols1, cols2){
        x <- col2rgb(cols1)
        y <- col2rgb(cols2)
        sim <- which.min(colSums(abs(x[,ncol(x)] - y)))
        message(paste("Your palette will be", sim, "colors shorter."))
        cols.x <- apply(x, 2, function(temp) rgb(t(temp)/255))
        cols.y <- apply(y[,sim:ncol(y)], 2, function(temp) rgb(t(temp)/255))
        return(c(cols.x,cols.y))
    }
    
    img(select.col(rev(viridis(100,0)),magma(100,0)), "")
    # Your palette will be 16 colors shorter.
    

提交回复
热议问题