brewer_scale: ColorBrewer scales and palettes

View source: R/scale_brewer.R

brewer_scaleR Documentation

ColorBrewer scales and palettes

Description

ColorBrewer scales and palettes

Usage

brewer_scale(
  name = "Blues",
  model = "lab",
  interp = "linear",
  domain = c(0, 1),
  reverse = FALSE
)

brewer_map(x, ...)

brewer_palette(name = "Blues", ...)

brewer_colors(n, name = "Blues", ...)

Arguments

name

name of a ColorBrewer palette. See brewer_info for a list of palettes and their characteristics.

model

string defining the color model in which to perform the interpolation; valid models are lab (the default and usually most suitable), hcl, lch, hsi, hsl, hsv, rgb, lrgb. Beware that all but lab and (l)rgb can give surprising results.

interp

string defining the type of interpolation to perform; either linear (the default) or bezier, which results in a smoother transition between colors. bezier interpolation is only available with model="lab" however.

domain

vector of two values between which the scale is computed.

reverse

whether to reverse the order of colors along the scale.

x

a vector whose values will be coerced to numbers and mapped to colors.

...

passed to brewer_scale. Note that argument domain is meaningless in functions other than brewer_scale and passing it through ... is an error.

n

number of colors to extract from the color palette.

Value

*_scale returns a function. This function takes a single argument (x: a numeric vector), maps its values to colors, and returns thee colors as hex codes.

*_map is a shortcut for *_scale(domain=range(x))(x): it creates a scale that spans the range of values in argument x, maps the content of x on that scale, and returns the colors.

*_palette returns a function. This function takes an integer (n) as argument, picks n colors evenly spaced along the scale, and returns them as hex codes.

*_colors is a shortcut for *_palette()(n) and directly returns n evenly spaced colors. It is equivalent to built-in functions such as heat.colors, topo.colors, etc.

scale_* return a ggplot2 scale, either discrete (similar to scale_color_discrete) or continuous (similar to scale_color_continuous).

References

http://colorbrewer2.org

See Also

brewer for the colors in the palettes and brewer_info for a list of palettes and their characteristics.

Other color scales and palettes: chroma_scale(), cubehelix_scale(), etopo_scale(), hue_scale(), inferno_scale(), interp_scale(), light_scale(), magma_scale(), plasma_scale(), turbo_scale(), viridis_scale(), wikitopo_scale()

Examples

# Define a scale function
ygb <- brewer_scale(name="YlGnBu")
ygb(c(0, 0.2, 0.6, 1))

# Define a palette function
bgy_pal <- brewer_palette(name="YlGnBu", reverse=TRUE)
bgy_pal(10)
show_col(bgy_pal(100))

# Show 7 colors from each palette
show_col(lapply(brewer_info$name, function(x) {brewer_colors(n=7, name=x)}))

# Warn about the potentially inappropriate use of many colors
brewer_colors(n=15, name="Blues")
brewer_colors(n=15, name="Pastel1")
brewer_palette(name="Pastel1")(15)
# Some warnings can be avoided by explicitly requiring a palette
# which, by definition, is taken from a *continuous* scale
brewer_palette(name="Blues")(15)
brewer_palette(name="Pastel1")(15)

# Sequential ColorBrewer palettes are good for continuous variables
# such as the elevation of the Maunga Whau volcano
image(maunga, col=brewer_colors(100, name="YlOrBr", reverse=TRUE))
contour(maunga, col=alpha("white", 0.5), add=TRUE)

persp(maunga, theta=50, phi=25, scale=FALSE, expand=2,
      border=alpha("black", 0.4),
      col=brewer_map(persp_facets(maunga$z), "YlOrBr", reverse=TRUE))

## Not run: 
library("rgl")
persp3d(maunga, aspect=c(1,0.7,0.2), axes=FALSE, box=FALSE,
        col=brewer_map(maunga$z, "YlOrBr", reverse=TRUE))


## End(Not run)
# Qualitative palettes are appropriate for discrete variables
attach(iris)
plot(Petal.Length, Sepal.Length, pch=19, col=brewer_map(Species, "Set2"))
legend(1, 8, legend=levels(Species), pch=19,
       col=brewer_colors(n=nlevels(Species), name="Set2"))


jiho/chroma documentation built on Nov. 26, 2022, 2:39 a.m.