r - colorRamp returns 0 -


i'm trying plot lines , color lines based on probability of connection. given vector of probabilities, use:

colfunc <- colorramp(c("white", "red")) colors <- colfunc(probs) 

colors nx3 matrix of rgb values. however, colfunc quite returns 0 value, when attempt plot using these colors, r complains

error in col2rgb(colors) : numerical color values must positive 

is there error in way defining color function?

your function works fine, think, doesn't return colors can use plot, because plot wants color, not rgb values in matrix.

there's better way, can covert matrix:

probs <- runif(10) colors <- colfunc(probs) my_col = apply(colors, margin = 1,  function(x) rgb(x[1]/255, x[2]/255, x[3]/255))  plot(1:10, 1:10, col = my_col) # should work fine 

or wrap function

better_colfunc <- function(x, ramp = colorramp(c("white", "red"))) {    colors <- ramp(x)    colors = apply(colors, margin = 1,  function(x) rgb(x[1]/255, x[2]/255, x[3]/255))    return(colors) }  plot(1:10, 1:10, col = better_colfunc(probs, ramp = colfunc)) 

as "colfunc quite returns 0 value", , other issues, you'll need share both data (what probs like?) perhaps actual plotting code. see here tips on making reproducible questions.


Comments

Popular posts from this blog

javascript - ScrollTo Effect (href to div) -

javascript - ng-class not responding to change in model in Angular? -

javascript - document.registerElement extending HTMLInputElement prototype while using custom tag name to avoid implicit browser style -