The colorRamp2 package

The colorRamp2() function can generate a color mapping function from a vector of break values and a vector of corresponding colors. Other colors are linearly interpolated in a certain color space.

library(colorRamp2)

col_fun = colorRamp2(c(0, 0.5, 1), c("blue", "white", "red"))
col_fun(seq(0, 1, length = 20))


plot(NULL, xlim = c(0, 1), ylim = c(0, 1))
x = seq(0, 1, length = 20)
y = rep(0.5, 20)
points(x, y, pch = 16, col = col_fun(x), cex = 2)

With the color mapping function col_fun, colors can be mapped back to the original numeric values. Note since colors are discrete, the values that are mapped back are not exactly identical to their original values.

x1 = runif(10)
col = col_fun(x1)
x2 = col2value(col, col_fun = col_fun)
x1
x2
sessionInfo()


Try the colorRamp2 package in your browser

Any scripts or data that you put into this service are public.

colorRamp2 documentation built on Dec. 21, 2022, 5:22 p.m.