library(cb.city)
Divergent color palettes should be used when the range of values is centered on some breakpoint -- zero, for example -- implying a difference or relative value. This would be appropriate for plotting model residuals, for example. The suite of color palettes currently available, by name, is
library(reshape2) all.div <- list_cpt(pal.type=='div') #http://www.edlazorvfx.com/ysu/html/ascii.html shapes <- paste0("&#", c(10003, 9996, 9745, 9786, 9734, 9728), ";") tbl <- dcast(all.div, pal.name ~ pal.n, value.var='pal.n', fun.aggregate = function(x){lx <- length(x); as.character(ifelse(lx==1,sample(shapes,1),lx))}, margins = TRUE) knitr::kable(tbl, "markdown")
where the top number gives the number of colors in the palette, and a happy
character^[A happy character includes any of these: ✓, ✌, ☑, ☺, ☆, or ☀]
indicates the palette (named to the far left)
has that many colors.
The columns on the far-right and bottom show counts of the number of TRUE
variants of that given row or column.
Since there is some cost associated with downloading the palettes, here are just the length-11 palettes:
nt <- 11 cseq <- seq_len(nt) shake <- FALSE PLT <- function(pal.name, pal.n=nt, ...){ if (shake){ letters[1:pal.n] } else { cc. <- city_palette(pal.name, pal.n) plot(cc., ax=axis(1, at=cseq), ...) as.vector(cc.) } } pals <- unique(all.div$pal.name) layout(matrix(seq_len(length(pals)))) par(mar=c(0,4,0,0), oma=c(3,2,1,1), las=1) Cols <- as.data.frame(t(sapply(pals, PLT))) names(Cols) <- cseq
and a table of the hexidecimal codes:
knitr::kable(Cols)
The red-to-blue (through white) is a classic palette that is highly effective.
Use city_palette
to quickly retrieve the
color palette^[Remember: the result will be a cpt.cols
object, so use as.vector
or another method coercion to extract
the color codes!]
cpal <- city_palette("RdBu",11)
or, with RColorBrewer:
cpal.rcb <- RColorBrewer::brewer.pal(11,"RdBu") print(all.equal(as.vector(cpal), cpal.rcb))
From then on you can use the palette however you see fit. Here
are two illustrations using that palette^[The plot.cpt.cols
method is
used to create the color scalebars.]: one is a pattern found by $\sin(x) + \cos(y)$, and
the other a more interesting pattern based on
the $\mathrm{sinc}$ function:
layout(matrix(c(1,2,1,2,1,2,1,2,3,4), nrow=5, byrow=TRUE)) par(mar=c(0,0.1,1,0.1), oma=c(0,0,0,0)) sines <- function(x, y){sin(x*pi/180)/2 + cos(y*pi/180)/2} image(outer(0:180, 0:180, FUN = sines), col=as.vector(cpal), axes=FALSE, zlim=c(-1,1), asp=1) mtext("A sum of sinusoids", side=3, line=0.) sinc <- function(x,y){ r. <- sqrt(x**2 + y**2) + .Machine$double.eps; sin(r.)/r. } x <- y <- seq(-10,10,by=0.1) image(outer(x, y, FUN = sinc), col=as.vector(cpal), axes=FALSE, zlim=c(-1,1), asp=1) mtext("the sinc function", side=3, line=0.) par(mar=c(3,4,1,4)) plot(cpal, ax=axis(1, at=c(1,6,11), labels=c(-1,0,1)), ylab="") plot(cpal, ax=axis(1, at=c(1,6,11), labels=c(-1,0,1)), ylab="")
The diverging color palette makes it easy for the brain to distinguish highs and lows, and to compare their size relative to zero.
In these examples
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.