Skip to content

Instantly share code, notes, and snippets.

@fdschneider
Created July 25, 2016 06:57
Show Gist options
  • Save fdschneider/b4d9c24bebbf0d15282233a8f66bc760 to your computer and use it in GitHub Desktop.
Save fdschneider/b4d9c24bebbf0d15282233a8f66bc760 to your computer and use it in GitHub Desktop.
transfer numerical vector into color gradient
highlight <- function(x, colrange = c("black", "red2"), steps = NULL, range = "auto"){
if(is.null(steps)) steps = length(unique(x))
if(range[1] == "auto") {
min_val = min(x, na.rm = TRUE)
max_val = max(x, na.rm = TRUE)
} else {
min_val = range[1]
max_val = range[2]
}
colorscale <- colorRampPalette(colrange, space = "rgb")
cols <- colorscale( steps)
cols[as.integer((x-min_val)/(max_val-min_val)*steps+1)]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment