how to color a qualitative gradient with R (using independent categories / multiple dimensions)

前端 未结 2 773
栀梦
栀梦 2021-01-21 10:24

sorry if i\'m missing something fundamental about how colorRampPalette and brewer.pal work, but how can you create a qualitative color gradient based o

相关标签:
2条回答
  • 2021-01-21 11:15

    i believe that something like this is the solution, although i can't believe there isn't a smarter way to do it.

    https://stackoverflow.com/a/26573256/1759499

    0 讨论(0)
  • 2021-01-21 11:22

    If I understand the question, you want to map different colour scales to different groups. Here's a possible strategy,

    x <- seq(0,6*pi-0.01, length=100)
    y <- sin(x)
    i <- x%/%pi + 1 # groups in the data
    d <- data.frame(x=x,y=y,i=i)
    
    cols <- RColorBrewer::brewer.pal(length(unique(i)),"Set1")
    
    library(plyr)
    
    # for each group, map to a specific colorRamp
    d2 <- ddply(d, "i", function(.d){
      id <- as.numeric(as.character(unique(.d$i)))
      pal <- colorRamp(c(cols[id], "white"), )
      cols <- pal(scales::rescale(.d$y))
      mutate(.d, col=rgb(cols[,1],cols[,2],cols[,3], maxColorValue = 255))
    })
    
    
    ggplot(d2, aes(x,y,colour=col,group=i))+
      geom_line(lwd=5) + scale_colour_identity() +
      theme_minimal()
    

    same idea would apply for a map.

    enter image description here

    0 讨论(0)
提交回复
热议问题