chroma_scale: Chromacity scale and palette

View source: R/scale_chroma.R

chroma_scaleR Documentation

Chromacity scale and palette

Description

Chromacity-based color scale, in HCL space.

Usage

chroma_scale(
  chroma = c(0, 1),
  l = 0.5,
  h = 0,
  domain = c(0, 1),
  reverse = FALSE,
  na.value = NULL,
  extrapolate = FALSE
)

chroma_map(x, ...)

chroma_palette(...)

chroma_colors(n, ...)

scale_color_chroma(
  ...,
  chroma = c(0, 1),
  l = 0.5,
  h = 0,
  reverse = FALSE,
  na.value = NULL,
  guide = "colorbar"
)

scale_fill_chroma(
  ...,
  chroma = c(0, 1),
  l = 0.5,
  h = 0,
  reverse = FALSE,
  na.value = NULL,
  guide = "colorbar"
)

Arguments

chroma

chromacity, vector of two numbers in [0,~1] (0 is grey, ~1 is full color) giving the minimum and maximum chromacities along the scale.

l

lightness, number in [0,1]; 0 is darkest, 1 is brightest.

h

hue, either an angle around the color wheel, in [0,360] (angles outside of the range are rotated back to within [0, 360]: 380 = 20, -15 = 345, etc.), or a color (hex or named) from which the hue is extracted (by function hue).

domain

vector of two values between which the scale is computed.

reverse

whether to reverse the order of colors along the scale.

na.value

value to return for missing values in the input. Can be either a color, NULL in which case a tentitatively appropriate color will be chosen automatically, or NA.

extrapolate

when FALSE, the default, return NA for input values that are out of the domain; when TRUE return the color corresponding to the extreme of the domain instead.

x

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

...

passed to chroma_scale from other chroma_* functions; passed to ggplot2::continuous_scale from the scale_* functions. NB: in all situations, passing domain is meaningless and yields an error.

n

number of colors to extract from the color palette.

guide

type of guide for the legend ("colorbar" for a continuous colorbar, "legend" for a categorical guide) or guide object itself.

Details

In HCL space, the perceived color (hue) is completely separated from the perceived intensity (chromacity) and lightness of the color. This means that colors of various hues but same chromacity and lightness appear as the exact same grey when converted to greyscale. This makes the HCL space particularly suitable to create good color palettes:

  • For qualitative palettes (discrete variables): varying h at constant c and l avoids drawing attention to certain hues, as would happen if the same was done in HSV or HSL space. Indeed, some hues are perceived as brighter (yellow, light green, etc.), others as duller/darker (blues, etc.).

  • For sequential palettes (continuous variables): varying l (or possibly c) for a constant h gives a sense of direction and avoid the many perceptual pitfalls typical of 'rainbow'-like scales.

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).

See Also

The hcl function, on which this one is based.

Other HCL-based scales: hue_scale(), light_scale()

Other color scales and palettes: brewer_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 towards a more intense red
reds <- chroma_scale(h=30)
# and apply it to some data
reds(x=c(0, 0.2, 0.6, 1))
show_col(reds(x=c(0, 0.2, 0.6, 1)))

# Define a palette function
reds_pal <- chroma_palette(h=30)
# and get 10 colors from it
reds_pal(n=10)
show_col(reds_pal(n=10))
# or use the shortcut and get 50 colors
show_col(chroma_colors(n=50, h=30))

# Determine hue from a color and then define a chroma scale
blues <- chroma_colors(n=50, h="dodgerblue")
greens <- chroma_colors(n=50, h="green")
golds <- chroma_colors(n=50, h="gold")
pinks <- chroma_colors(n=50, h="deeppink")
show_col(blues, greens, golds, pinks)

# Chroma scales can be used for continuous variables
# such as the elevation of the Maunga Whau volcano
image(maunga, col=chroma_colors(100, h="orange"))
contour(maunga, col=alpha("white", 0.5), add=TRUE)

filled.contour(maunga, color.palette=chroma_palette(h="orange"))

persp(maunga, theta=50, phi=25, scale=FALSE, expand=2,
      border=alpha("black", 0.4),
      col=chroma_map(persp_facets(maunga$z), h="orange"))
# but a lightness-based scale would probably be even better
# (see ?light_scale)

## Not run: 
# in spinning 3D
library("rgl")
persp3d(maunga, aspect=c(1,0.7,0.2), axes=FALSE, box=FALSE,
        col=chroma_map(maunga$z, h="orange"))
play3d(spin3d(axis=c(0, 0, 1), rpm=10), duration=6)

# and with ggplot2
library("ggplot2")
ggplot(maungaxyz) + coord_fixed() +
  geom_raster(aes(x=x, y=y, fill=z)) +
  geom_contour(aes(x=x, y=y, z=z), color="white", alpha=0.5) +
  scale_fill_chroma(h="orange")

## End(Not run)

# Or they could be used to map a third variable on a scatterplot
attach(airquality)
# define a scale encompassing the whole data
blue_scale <- chroma_scale(h="cornflowerblue", domain=c(0,200))
# use it on a plot and in the legend
pars <- sidemargin()
plot(Wind, Temp, col=blue_scale(Ozone), pch=19)
sidelegend(legend=c(pretty(Ozone), "NA"),
           col=blue_scale(c(pretty(Ozone), NA)), pch=19)
par(pars)
# note that the missing value color contrasts with the rest of the scale

# They are not really appropriate for categorical variables though
attach(iris)
plot(Petal.Length, Petal.Width, col=chroma_map(Species), pch=19)
legend(1, 2, legend=levels(Species),
             col=chroma_colors(n=nlevels(Species)), pch=19)
# a hue-based scale would be much better (see ?hue_scale)

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