Get startedGet started for free

Color scales

1. Color scales

Whenever color encodes data in your plot, it's important to put some thought into choosing good colors.

2. A perceptual color space: HCL

We say our color perception is trichromatic, in that we perceive color as three dimensional, however the three dimensions that people naturally describe do not correspond to contributions from three primary colors. Instead, we tend to describe color on three perceptual dimensions: hue, chroma and luminance. Hue is describes a color based on its predominant wavelength, think color of the rainbow.

3. A perceptual color space: HCL

Hue is circular, and generally considered unordered, perceptually there is no clear lowest or highest value. Chroma describes the intensity of the color from gray to maximal intensity, intuitively people will refer to low chroma colors as muddy. Luminance describes the lightness of the color from black at one end, and white at the other.

4. A perceptual color space: HCL

Both chroma and luminance are considered ordered, there is clear agreement on the order of colors that only vary in one of these dimensions. Why talk about these perceptual dimensions? Because these dimensions form the basis of selecting palettes of colors that match perceptually the meaning of our variables.

5. A perceptual color space: HCL

For example, when a variable is ordered, there is a meaningful idea of one value being bigger than another, and a bigger value, should correspond to a bigger color.

6. A perceptual color space: HCL

What does a bigger color mean? We don't really have a concept of big in color, the best we can do is to make the color higher on one of the ordered perceptual dimensions: chroma or luminance.

7. Types of scale

When a palette is designed to represent a value from low to high we call it sequential. A sequential palette, will vary either chroma, luminance or both. Here, a scale going from light to dark blue. Occasionally, a sequential scale will additionally vary hue, we call this a redundant coding. Lightness carries most of the meaning of small to big, but the addition of hue from yellow to purple will help people identify specific values. A diverging palette is designed for values that correspond to a deviation from a central value. They are generally constructed by using chroma and/or lightness to distinguish close or far from the center and two hues to distinguish above and below the central value. Finally, when there is naturally no order to the values, for example they are categories, we want colors that don't imply an order. Generally this kind of qualitative palette uses colors of the same chroma and luminance and only varies hue.

8. Generating color scales in R

There are a number or packages that help you generate color palettes in R. A popular one is RColorBrewer, which provides a set of named palettes. You can view them all with the "display brewer all" function, where they are helpfully grouped into sequential, diverging and qualitative palettes. You can get the hexadecimal code of colors in a palette by calling brewer-dot-pal with the number of colors required and the palette name. Another newer package viridisLite offers some alternative sequential palettes and its function viridis works in a very similar way to brewer-dot-pal. Occasionally you'll see an extra two digits in a hex string. These digits describe how opaque a color is, but you can see here all the colors are at maximal opaqueness.

9. Let's practice!

Over the next exercises, you'll learn how to take these vectors of colors and use them for a color scale in both ggplot2 and tmap.