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
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
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.