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

风流意气都作罢 提交于 2019-12-02 01:14:12

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.

Anthony Damico

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!