--- title: "The colorRamp2 package" author: "Zuguang Gu (z.gu@dkfz.de)" date: '`r Sys.Date()`' output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{The colorRamp2 package} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- 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. ```{r, fig.width = 6} 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. ```{r} x1 = runif(10) col = col_fun(x1) x2 = col2value(col, col_fun = col_fun) x1 x2 ``` ```{r} sessionInfo() ```