Created
September 23, 2016 09:09
-
-
Save timcdlucas/d303b70eb3140c10bfc96cd24e3f7004 to your computer and use it in GitHub Desktop.
bivariate colour palette
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Code by Dave Redding | |
library(raster) | |
zzz=100 | |
colsX<-raster(nrow=zzz,ncol=zzz) | |
values(colsX)<-1:ncell(colsX) | |
extent(colsX)<-c(-100,100,-100,100) | |
cols<-c("#00FF00","#FFFFFF") | |
#cols<-c("#00FF00","#000000") | |
colX1<-colorRampPalette(cols,space="rgb",interpolate ="spline",bias=1.5) | |
c1<-colX1(zzz) | |
cols2<-c("#FC0000","#0000FC") | |
colX2<-colorRampPalette(cols2,space="rgb",interpolate ="spline",bias=1.5) | |
c2<-colX2(zzz) | |
for (i in 1:100){ | |
cols3<-c(c1[i],c2[i]) | |
colX3<-colorRampPalette(cols3,space="rgb",interpolate ="spline") | |
c3<-colX3(zzz) | |
if(i==1){c4<-c3} else{c4<-c(c4,c3)} | |
} | |
plot(colsX,col=c4,legend=F) | |
res1<-matrix(nrow=zzz,ncol=zzz,data=c4, byrow = TRUE) | |
#'@example | |
#' data <- data.frame(x = rnorm(100), y = rnorm(100), z1 = runif(100), z2 = runif(100)) | |
#' data$z1percentile <- as.numeric(cut(data$z1, 100)) | |
#' data$z2percentile <- as.numeric(cut(data$z2, 100)) | |
#' data$col <- sapply(1:nrow(data), function(x) res1[data$z1percentile[x], data$z2percentile[x]]) | |
# layout(matrix(c(1,2), 1, 2, byrow = TRUE), widths = c(0.7, 0.3)) | |
#' plot(y ~ x, data, col = data$col) | |
#' image(as.matrix(colsX),col=c4,legend=F) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment