Skip to content

Instantly share code, notes, and snippets.

@Mortimerp9
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mortimerp9/aabaf0b0dea2157969d8 to your computer and use it in GitHub Desktop.
Save Mortimerp9/aabaf0b0dea2157969d8 to your computer and use it in GitHub Desktop.
diverging color schemes for negative and positive numbers in R
library(RColorBrewer)
colPos<-colorRampPalette(brewer.pal(9, "YlGn")[3:9])(100)
colNeg<-colorRampPalette(brewer.pal(9, "OrRd")[3:9])(100)
foo <- function(diff, bin) {
if(diff>=0) colPos[bin] else colNeg[bin]
}
mat$diff <- mat$x-mat$y
mat$bin <- cut(log10(abs(mat$diff)+1),100, labels=seq(1,100))
mat$col <- mapply(foo, mat$diff, mat$bin)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment