Site icon R-bloggers

R Color Reference Sheet

[This article was first published on Brokering Closure » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

R has a built-in collection of 657 colors that you can use in plotting functions by using color names. There are also various facilities to select color sequences more systematically:

When producing data visualizations, the choice of proper colors is often a compromise between the requirements dictated by the data visualisation itself and the overall style and color of the article/book/report that the visualization is going to be an element of. Choosing an optimal color palette is not so easy and its handy to have some reference. Inspired by a this sheet by Przemek Biecek I created a variant of an R color reference sheet showing different ways in which you can use and call colors in R when creating visualizations. The sheet fits A4 paper (two pages). On the first page it shows a matrix of all the 657 colors with their names. On the second page, on the left, all palettes from RColorBrewer package are displayed. On the right, selected color ramps available in base R (base package grDevices) and in the contributed package colorRamps. Miniatures below:


You can download the sheet as PDF from here.

Below is a gist with the code creating the sheet as a PDF “rcolorsheet.pdf”. Instead of directly reusing the Przemek’s code I have rewritten the parts that produce the first page (built-in color names) and the part with the ramps using the image function. I think it is much simpler, less low-level for-looping and a bit more extensible. For example, it is easy to extend the collection of color ramps by providing just additional function name in the form packagename::functionname to the funnames vector (any extra package would have to be loaded at the top of the script).

library(RColorBrewer)
library(colorRamps)


pdf("rcolorsheet.pdf", paper="a4r", width=11.6, height=8.2, onefile=TRUE)

### page 1

# grDevices::colors
m <- matrix(1:660, 60, 11)
kol <- colors()[m]
op <- par(mar=c(.1, .1, 2, .1))
image(1:11, 1:60, t(m), col=kol, axes=FALSE, ann=FALSE)
txtcol <- ifelse( apply(col2rgb(kol), 2, mean) < 70, "white", "black")
text( as.numeric(col(m)), as.numeric(row(m)), kol, cex=.8, col=txtcol)
mtext("grDevices::colors", 3, cex=2)
par(op)

### page 2
layout(matrix(1:2, 1, 2))

# RColorBrewer
op <- par(mar=c(.1, 5, 2, .1))
display.brewer.all()
mtext("RColorBrewer", 3, cex=2)
par(op)

# ramps
N <- 100 # ramp length
funnames <- c("grDevices::rainbow", "grDevices::heat.colors",
          "grDevices::terrain.colors", "grDevices::topo.colors",
          "grDevices::cm.colors", "colorRamps::blue2red",
          "colorRamps::blue2green", "colorRamps::green2red",
          "colorRamps::blue2yellow", "colorRamps::cyan2yellow",
          "colorRamps::magenta2green", "colorRamps::matlab.like",
          "colorRamps::matlab.like2", "colorRamps::primary.colors",
          "colorRamps::ygobb")
spl <- strsplit(funnames, "::")
pkgs <- sapply(spl, "[", 1)
funs <- sapply(spl, "[", 2)
kolmat <- sapply(funs, do.call, list(N))
mat <- matrix( seq(1, length(kolmat)), nrow(kolmat), ncol(kolmat))
op <- par(mar=c(.1, .1, 2, .1))
image(seq(1, nrow(mat)), seq(1, ncol(mat)), mat, col=kolmat,
      axes=FALSE, ann=FALSE)
text( nrow(mat)/2, seq(1, ncol(mat)), funnames)
mtext("Color ramps", 3, cex=2)
par(op)


dev.off()

To leave a comment for the author, please follow the link and comment on their blog: Brokering Closure » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.