Maps colors to a vector of numeric data based on a palette of colors and a
vector of bin ends. Can use either a named palette from the
RColorBrewer package or a list of explicit colors. Data is binned and
labeled with the associated color as per
mapLabels. This is not
intended to produce a smooth gradient, but instead compresses the information
in the data into a discrete set of colors.
The vector of numeric data to map to a color
The sequence of cut-points defining the bins into which the
data will be split. Each bin will correspond to a color in the color vector
or palette and provides for mapping each value to a color. Values exactly
matching bin ends will be in the lower bin.
The name of the RColorBrewer palette to use
for bin labels, by default
Reverse the order of the colors. If explicitly specifying the
A vector of colors to use as bin names. The number of colors
will have to be one less than the number of bin ends. If this is specified,
This is really just a convenience function to allow using color brewer
palettes easily when binning data. Colors are no different than other labels
and can be applied generically using
mapLabels. Note, this
requires the RColorBrewer package if using
The vector of colors the data maps to.
If no colors are specified, a vector of colors will be assigned using the
brewer palette named,
'RdBu' if not specified. Each palette comes
in multiple variants with a different number of colors. The number of
colors will be determined automatically from the
Using color brewer palettes is just a convenient shortcut to specifying a
list of colors and is ignored if a vector of colors is specified. If no
version of the given brewer palette has the number of levels required by
the bin ends, an error will occur.
For diverging values, can have 3 - 11 bins, allowed values are:
BrBG PiYG PRGn PuOr RdBu RdGy RdYlBu RdYlGn Spectral
For sequential values, can have 3 - 8 bins, allowed values are:
Blues BuGn BuPu GnBu Greens Greys Oranges OrRd PuBu PuBuGn PuRd Purples RdPu Reds YlGn YlGnBu YlOrBr YlOrRd
(Note: it is Greys, not Grays)
For qualitative palettes, can have 3 to (n) bins where n varies by palette:
Accent (8), Dark2 (8), Paired (12), Pastel1 (9), Pastel2 (8), Set1 (9), Set2 (8), Set3 (12)
Note: palettes are only specified in one direction, and binEnds are always sorted from lowest to highest before using, so set reverse=TRUE if you want to reverse the color order.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
library(RColorBrewer); ### Review from RColorBrewer # Display available palettes display.brewer.all() # Display colors in a palette paletteName <- "RdBu" levels <- 4 RColorBrewer::brewer.pal(levels, paletteName) ### Using mapColors x <- c(-100, -1, 0, 1, 100) ends <- c(-Inf,-10,10,Inf) # Map data to 3 colors using the default (RdBu) palette mapColors( x, binEnds= ends ) #=>  "#EF8A62" "#F7F7F7" "#F7F7F7" "#F7F7F7" "#67A9CF" # Map data to 3 colors using the reverse of the default (RdBu) palette mapColors( x, binEnds= ends, reverse= TRUE) #=>  "#67A9CF" "#F7F7F7" "#F7F7F7" "#F7F7F7" "#EF8A62" # Map to a specified palette name mapColors( x, binEnds= ends, brewerPaletteName= 'Set1' ) #=>  "#E41A1C" "#377EB8" "#377EB8" "#377EB8" "#4DAF4A" # Map data using an explicit color vector colorMap <- c('red', 'violet', 'blue') mapColors( x, binEnds= ends, colors= colorMap) #=>  "red" "violet" "violet" "violet" "blue" mapColors( x, binEnds= ends, colors= colorMap, reverse= TRUE) #=>  "blue" "violet" "violet" "violet" "red"
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.