Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ramp <- function(values, cols, n) {
- # values: a sorted vector of values
- # col: a vector of colours corresponding to the values
- # n: total number of colours to create
- scale01 <- function(x) (x - min(x, na.rm=TRUE))/diff(range(x))
- round2 <- function(x) {
- # ensures sum of rounded values is maintained
- y <- floor(x)
- i <- tail(order(x-y), round(sum(x)) - sum(y))
- y[i] <- y[i] + 1
- y
- }
- if(is.unsorted(values)) stop('values should be sorted ascendingly', call.=FALSE)
- nv <- length(values)
- nc <- length(cols)
- stopifnot(nc==nv)
- pals <- lapply(mapply(c, cols[-nc], cols[-1], SIMPLIFY=FALSE),
- colorRampPalette)
- nn <- round2(diff(scale01(values)*n))
- ramps <- mapply(function(p, n) p(n), pals, nn)
- unname(unlist(ramps))
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement