Advertisement
Guest User

Untitled

a guest
May 25th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. ramp <- function(values, cols, n) {
  2. # values: a sorted vector of values
  3. # col: a vector of colours corresponding to the values
  4. # n: total number of colours to create
  5. scale01 <- function(x) (x - min(x, na.rm=TRUE))/diff(range(x))
  6. round2 <- function(x) {
  7. # ensures sum of rounded values is maintained
  8. y <- floor(x)
  9. i <- tail(order(x-y), round(sum(x)) - sum(y))
  10. y[i] <- y[i] + 1
  11. y
  12. }
  13. if(is.unsorted(values)) stop('values should be sorted ascendingly', call.=FALSE)
  14. nv <- length(values)
  15. nc <- length(cols)
  16. stopifnot(nc==nv)
  17.  
  18. pals <- lapply(mapply(c, cols[-nc], cols[-1], SIMPLIFY=FALSE),
  19. colorRampPalette)
  20. nn <- round2(diff(scale01(values)*n))
  21. ramps <- mapply(function(p, n) p(n), pals, nn)
  22. unname(unlist(ramps))
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement