Skip to content

Instantly share code, notes, and snippets.

@derrickturk
Last active August 29, 2015 13:56
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 derrickturk/8831459 to your computer and use it in GitHub Desktop.
Save derrickturk/8831459 to your computer and use it in GitHub Desktop.
Handy function for HSV-scale-izing real-valued data.
hsv.scale <- function (var, hmin, hmax=hmin, smin=1, smax=smin, vmin=1, vmax=vmin,
na.col=rgb(0.75, 0.75, 0.75))
{
var.min <- min(var, na.rm=TRUE)
var.max <- max(var, na.rm=TRUE)
h.f <- approxfun(c(var.min, var.max), c(hmin, hmax))
s.f <- approxfun(c(var.min, var.max), c(smin, smax))
v.f <- approxfun(c(var.min, var.max), c(vmin, vmax))
tmp <- var
tmp[is.na(var)] <- var.min
col <- hsv(h=h.f(tmp), s=s.f(tmp), v=v.f(tmp))
col[is.na(var)] <- na.col
col
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment