knitr::opts_chunk$set(comment="#", tidy=FALSE)
chroma
is a R package for parsing and formating colors in various specifications, manipulating colors, and creating nice color scales and palettes. Much of the functionality is based on the excellent chroma.js javascript library by Gregor Aisch.
chroma
is not on CRAN yet. To get it from this page, install the package devtools
and
devtools::install_github("jiho/chroma") library("chroma")
library("ggplot2") devtools::load_all(".", quiet=TRUE)
NB: some functions in the chroma
package mask functions in basic R packages (e.g. grDevices
) or often used ones (e.g. ggplot2
). Most of the time, they are argument-compatible with the function they mask and, if their result is different, I would argue that the one in chroma is more correct. If you want to use chroma but would rather not mask the other functions, just install the package but do not load it with library()
; instead, just call the functions with the chroma::
prefix (e.g. chroma::rgb()
instead of rgb()
, to avoid masking rgb()
in package grDevices
).
Parse colors in various specifications
rgb(r=0.5, g=0.5, b=0.5) ryb(r=0.5, y=0.5, b=0.5) hsv(h=120, s=0.5, v=0.5) hsl(h=120, s=0.5, l=0.5) hsi(h=120, s=0.5, i=0.5) hcl(h=120, c=0.5, l=0.5) lab(l=0.5, a=-0.5, b=0.2) hex("#393939") hex("F39") css("rgb(100,100,100)") temperature(5600) wavelength(500)
Parse directly from a matrix
x <- matrix(c(0.2, 0.5, 0.5, 0.5, 0.6, 0.4), ncol=3) print(x) rgb(x)
Convert back in various formats
as.rgb("coral1") as.ryb("coral1") as.hsv("coral1") as.hsl("coral1") as.hsi("coral1") as.hcl("coral1") as.lab("coral1") as.hex("coral1") as.css("coral1") as.temperature("coral1") as.wavelength("coral1")
Slightly modify a base color
col <- "#7BBBFE" show_col(c(col, brighten(col), darken(col))) show_col(c(col, desaturate(col), saturate(col)))
Make a color semi-transparent
col <- "#7BBBFE" show_col(c(col, alpha(col), alpha(col, 0.2)))
Mix or blend two colors
show_col(c("#7BBBFE", "#FDFF68", mix("#7BBBFE", "#FDFF68"))) show_col(c("#7BBBFE", "#FDFF68", blend("#7BBBFE", "#FDFF68"))) show_col(c("#7BBBFE", "#FDFF68", blend("#7BBBFE", "#FDFF68", mode="screen")))
Compute the contrast between two colors
contrast("darkblue", "darkgreen") contrast("yellow", "darkgreen")
Compute the Euclidean or perceptual distance between two colors (following the CIE Delta E 2000 formula)
# pick three colors; lightgreen is closer to darkgreen that darkblue is show_col(c("darkblue", "darkgreen", "lightgreen")) color_distance("darkblue", "darkgreen") color_distance("lightgreen", "darkgreen") CMClc("darkblue", "darkgreen") CMClc("lightgreen", "darkgreen") deltaE("darkblue", "darkgreen") deltaE("lightgreen", "darkgreen")
which allows to find the closest perceived color in an array of possibilities
target <- "limegreen" choices <- rainbow(10) closest_color <- choices[which.min(deltaE(target, choices))] show_col(target, choices, closest_color)
Extract or set a color channel
col <- "#7BBBFE" channel(col, model="hcl", "h") channel(col, model="hsv", "s") channel(col, model="hsi", "i") channel(col, model="rgb", "r") col1 <- col2 <- col channel(col1, model="hcl", "h") <- 120 channel(col2, model="hcl", "l") <- 1 show_col(c(col, col1, col2))
Compute or set the perceived luminance of a color
luminance(c("red", "yellow", "darkblue")) col1 <- col2 <- col <- "#7BBBFE" luminance(col) luminance(col1) <- 0.6 luminance(col2) <- 0.2 show_col(c(col, col1, col2))
All scales and palettes are organised the same way:
*_scale
return a function that takes a numeric vector x
as argument and returns the corresponding colors in the scale.*_map
are shortcuts that build the scale, map the values, and return the colors.*_palette
return a function that takes an integer n
as argument and returns n
equally spaced colors along the scale.*_colors
are shortcut that create the palette and return the n
colors.x <- 0:10/10 s <- interp_scale() s(x) # or interp_map(x) n <- 11 p <- interp_palette() p(n) # or interp_colors(n)
Palettes can be built by interpolating between colors
show_col(interp_colors(10)) show_col(interp_colors(10, colors=c("#2D2B63", "#F7FF84"))) show_col(interp_colors(10, colors=c("#2D2B63", "#FB3C44", "#F7FF84")))
For the perception of "change" along the scale to be more linear, colors can be interpolated using bezier curves and their lightness can be corrected.
show_col(interp_colors(10, colors=c("#2D2B63", "#FB3C44", "#F7FF84"))) show_col(interp_colors(10, colors=c("#2D2B63", "#FB3C44", "#F7FF84"), interp="bezier")) show_col(interp_colors(10, colors=c("#2D2B63", "#FB3C44", "#F7FF84"), interp="bezier", correct.lightness=TRUE))
Preset palettes are available, from colorbrewer
show_col(lapply(brewer_info$name, function(x) {brewer_colors(n=7, name=x)}))
Or viridis
show_col( viridis_colors(10), magma_colors(10), plasma_colors(10), inferno_colors(10) )
Or cubehelix
show_col( cubehelix_colors(10), cubehelix_colors(10, h=300, rot=-0.75), cubehelix_colors(10, h=120, rot=0.5), cubehelix_colors(10, h=300, rot=0.5) )
Or ETOPO1 and Wikipedia for topographical maps
show_col( etopo_colors(100), wikitopo_colors(100) )
Custom, perceptually appropriate, palettes can be built in HCL space, for either discrete
show_col(hue_colors(10))
or continuous variables
show_col(chroma_colors(10, h=140)) show_col(light_colors(10, h=140))
Additional functions are included to facilitate the use of the color scales in base and ggplot2 plots.
sidemargin()
and sidelegend()
allow to place legends on the side of base plots
attach(iris) # make larger right margin pars <- sidemargin() # plot the data with a nice color scale plot(Petal.Length, Petal.Width, col=hue_map(Species), pch=19) # add a legend for the scale on the side sidelegend(legend=levels(Species), col=hue_colors(nlevels(Species)), pch=19) # set graphical parameters back to default par(pars) detach(iris)
persp_facets()
allows to compute the colors for each facet of a persp()
plot
persp(maunga, theta=50, phi=25, scale=FALSE, expand=2, border=alpha("black", 0.4), col=magma_map(persp_facets(maunga$z)))
Various scale_color_*()
and scale_fill_*()
functions allow to use the color scales defined in chroma directly with ggplots and scale_xy_map()
draws nice x
and y
scales for maps.
ggplot(iris) + geom_point(aes(Petal.Length, Sepal.Length, color=Species)) + scale_color_cubehelix_d(h=0, rot=0.75, c=1, l=c(0.6, 0.6)) ggplot(thaixyz) + coord_quickmap() + geom_raster(aes(x, y, fill=z)) + scale_fill_wikitopo() + scale_xy_map()
Happy coloring!
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.