light_scale: Lightness scale and palette

View source: R/scale_light.R

light_scaleR Documentation

Lightness scale and palette

Description

Lightness-based color scale, in HCL space.

Usage

light_scale(
  l = c(0.1, 0.9),
  c = 0.5,
  h = 0,
  domain = c(0, 1),
  reverse = FALSE,
  na.value = NULL,
  extrapolate = FALSE
)

light_map(x, ...)

light_palette(...)

light_colors(n, ...)

scale_color_light(
  ...,
  l = c(0.1, 0.9),
  c = 0.5,
  h = 0,
  reverse = FALSE,
  na.value = NULL,
  guide = "colorbar"
)

scale_fill_light(
  ...,
  l = c(0.1, 0.9),
  c = 0.5,
  h = 0,
  reverse = FALSE,
  na.value = NULL,
  guide = "colorbar"
)

Arguments

l

lightness, vector of two numbers in [0,1] (0 is darkest, 1 is brightest) giving the minimum and maximum lightness along the scale.

c

chromacity, number in [0,~1]; 0 is grey, ~1 is full color.

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 light_scale from other light_* 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.

luminance for a the computation of perceived luminance and the creation of perception-based luminance palettes.

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

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

Examples

# Define a dark-to-light blue scale
blues <- light_scale(h=220)
# and apply it to some data
blues(x=c(0, 0.2, 0.6, 1))

# Define a palette function
blues_pal <- light_palette(h=220, c=0.3)
# and get 10 colors from it
blues_pal(n=10)
show_col(blues_pal(n=10))
# or use the shortcut
show_col(light_colors(n=50, h=220, c=0.3))

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

# Perceived lightness (computed by luminance()) increases in a similar
# way across the four hues, making the palettes almost comparable.
# (This would not be the case with a HSL or HSV gradient)
plot(  luminance(blues),  col=blues[40])
points(luminance(greens), col=greens[40])
points(luminance(golds),  col=golds[40])
points(luminance(pinks),  col=pinks[40])

# Lightness scales are good for continuous variables
# such as the elevation of the Maunga Whau volcano
image(maunga, col=light_colors(100, h=140))
contour(maunga, col=alpha("white", 0.5), add=TRUE)

filled.contour(maunga, color.palette=light_palette(h=140))

persp(maunga, theta=50, phi=25, scale=FALSE, expand=2,
      border=alpha("black", 0.4),
      col=light_map(persp_facets(maunga$z), h=140))

## Not run: 
# in spining 3D
library("rgl")
persp3d(maunga, aspect=c(1,0.7,0.2), axes=FALSE, box=FALSE,
        col=light_map(maunga$z, h=140))
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_light(h=140)

## 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 <- light_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 lightess matches the rest of the scale
plot(Wind, Temp, col=light_map(Ozone, l=c(0.5, 0.9)), pch=19)
plot(Wind, Temp, col=light_map(Ozone, l=c(0.1, 0.5)), pch=19)


# 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=light_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.