Last active
August 29, 2015 14:05
-
-
Save jonesor/55326409f5087a65f048 to your computer and use it in GitHub Desktop.
Manipulate a color palette for "heat map" type colour schemes.
This file contains 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
library(RColorBrewer) | |
#Assume your data are in a data frame. | |
df1 <- data.frame(x = 1:100) | |
#Define the basic palette (this case goes from red - yellow - green, but you could make it any 3 colours.) | |
colCode <- colorRampPalette(c("red", "yellow", "green"))(n = 999) | |
#Make a vector (of length 999), but break apart the parts of the sequence that should be red yellow and green. | |
#This allows you to alter where the yellow "pivot point" is. | |
numValue = c(seq(1,30,length=333), # for red | |
seq(30,50,length=333), # for yellow | |
seq(50,100,length=333)) # for green | |
#put these in a data frame | |
my_palette<-data.frame(colCode,numValue) | |
#A function to pick the closest matching numeric value from the palette. | |
findcol = function(x){my_palette$colCode[which.min(abs(my_palette$numValue - x))]} | |
#Apply this function to the data vector. | |
df1$colVal = as.character(unlist(lapply(df1$x,findcol))) | |
#Plot it. | |
plot(df1$x,pch=16,col=df1$colVal,cex=2) | |
#Now try altering lines 11-13 to see how it alters the sequence of colours. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment