Description Usage Arguments Details Value Color Brewer Palettes See Also Examples
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.
1 2 |
x |
The vector of numeric data to map to a color |
binEnds |
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. |
brewerPaletteName |
The name of the RColorBrewer palette to use
for bin labels, by default |
reverse |
Reverse the order of the colors. If explicitly specifying the
colors though |
colors |
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,
any |
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 brewerPaletteName
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 binEnds
provided.
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:
1 | BrBG PiYG PRGn PuOr RdBu RdGy RdYlBu RdYlGn Spectral
|
For sequential values, can have 3 - 8 bins, allowed values are:
1 2 | 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:
1 2 | 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 )
#=> [1] "#EF8A62" "#F7F7F7" "#F7F7F7" "#F7F7F7" "#67A9CF"
# Map data to 3 colors using the reverse of the default (RdBu) palette
mapColors( x, binEnds= ends, reverse= TRUE)
#=> [1] "#67A9CF" "#F7F7F7" "#F7F7F7" "#F7F7F7" "#EF8A62"
# Map to a specified palette name
mapColors( x, binEnds= ends, brewerPaletteName= 'Set1' )
#=> [1] "#E41A1C" "#377EB8" "#377EB8" "#377EB8" "#4DAF4A"
# Map data using an explicit color vector
colorMap <- c('red', 'violet', 'blue')
mapColors( x, binEnds= ends, colors= colorMap)
#=> [1] "red" "violet" "violet" "violet" "blue"
mapColors( x, binEnds= ends, colors= colorMap, reverse= TRUE)
#=> [1] "blue" "violet" "violet" "violet" "red"
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.