Overview

All HCL-based color palettes in the colorspace package (Zeileis et al. 2019) are also provided as discrete and/or continuous color scales for the use with the ggplot2 package (Wickham 2016; Wickham et al. 2019).

The scales are called via the scheme

scale_<aesthetic>_<datatype>_<colorscale>()

where

  • <aesthetic> is the name of the aesthetic (fill, color, colour).
  • <datatype> is the type of the variable plotted (discrete or continuous).
  • <colorscale> sets the type of the color scale used (qualitative, sequential, diverging, divergingx).

A few examples of these scales are illustrated in the following sections.

Using the scales in ggplot2

A discrete qualitative scale applied to a fill aesthetic corresponds to the function scale_fill_discrete_qualitative():

Similarly, a color aesthetic for a discrete qualitative scale corresponds to the function scale_color_discrete_qualitative():

A continuous sequential scale applied to a color aesthetic corresponds to the function scale_color_continuous_sequential():

A continuous sequential scale applied to a fill aesthetic corresponds to the function scale_fill_continuous_sequential():

A continuous diverging scale applied to a fill aesthetic corresponds to the function scale_fill_continuous_diverging():

Customizing the scales

All scale functions accept a palette argument which allows you to pick a specific color palette out of a selection of different options. All available palettes are listed at the end of this document. For example, we could use the “Harmonic” palette when we need a qualitative color scale:

The color palettes are calculated on the fly depending on the number of different colors needed. But sometimes, it may be desireable to pick specific colors out of a larger set, e.g., when we are making two plots where one contains a subset of the data of the other, or when the default order of colors is not ideal. Therefore, all discrete scales provide parameters nmax to set the total number of colors requested and order (a vector of integers) to reorder the color palette.

Applied to the previous plot, we could for example do the following:

The nmax option is also convenient to remove some colors from a scale that may not be appropriate for the plot. For example, the scale_color_brewer() scale that comes with ggplot2 tends to produce points that are too light:

Similar problems can arise with the HCL palettes, but there we have the option of creating additional colors that we then do not use:

gg + scale_color_discrete_sequential(palette = "Blues", nmax = 6, order = 2:6)

(We use order = 2:6 to pick the five darkest colors and omit the lightest color.)

All HCL-based color palettes are defined via sets of hue (H), chroma (C), and luminance (L) values. For example, the qualitative scales vary hue from a start value to an end value while keeping chroma and luminance fixed. Similarly, single-hue sequential scales vary chroma and luminance while keeping the hue fixed. We can override these settings by specifying the corresponding H, C, or L values in addition to the palette name. As an example, consider the following plot:

Now assume we generally like the color scale but find it a bit too pink at the end. We can fix this issue by specifying an alternative final hue value, e.g., h2 = 60 corresponding to yellow:

The next example uses a diverging scale. First consider the plot with the unmodified “Blue-Yellow 2” palette:

And now the same plot with some palette customizations: The ordering is reversed so that blue is used for positive correlations and yellow for negative ones. Moreover, the power parameter p2 for the luminance is increased so that only correlations close to an absolute value of 1 have dark colors while intermediate correlations have relatively light colors.

gg + scale_fill_continuous_diverging(palette = "Blue-Yellow 2", rev = TRUE, p2 = 2)

See the reference manual for the exact set of customization parameters that are available for each scale.

The continuous scales also provide the option to limit the scale range to which data are mapped, via the parameters begin and end. As an example, assume we are using the approximation of the viridis scale provided by scale_color_continuous_sequential():

If we want to remove some of the darkest blues and some of the brightest yellows from this scale, we can write:

Available palettes

In the following, we are visualizing all scales currently available via pre-defined names. These visualizations are generated by the function hcl_palettes() with option plot = TRUE.

The discrete qualitative scales are all called via scale_*_discrete_qualitative(palette = "name"), where name is the name of the palette, e.g., palette = "Pastel 1". There are no continuous qualitative scales.

The discrete sequential scales are all called via scale_*_discrete_sequential(palette = "name"), where name is the name of the palette, e.g., palette = "Grays". Continuous approximations to the discrete scales exist and can be called via scale_*_continuous_sequential(palette = "name")

The discrete diverging scales are all called via scale_*_discrete_diverging(palette = "name"), where name is the name of the palette, e.g., palette = "Blue-Red". Continuous approximations to the discrete scales exist and can be called via scale_*_continuous_diverging(palette = "name")

References

Wickham, Hadley. 2016. Ggplot2: Elegant Graphics for Data Analysis. 2nd ed. Springer-Verlag. http://ggplot2.org/.

Wickham, Hadley, Winston Chang, Lionel Henry, Thomas Lin Pedersen, Kohske Takahashi, Claus O. Wilke, Kara Woo, and Hiroaki Yutani. 2019. Ggplot2: Create Elegant Data Visualisations Using the Grammar of Graphics. https://CRAN.R-project.org/package=ggplot2.

Zeileis, Achim, Jason C. Fisher, Kurt Hornik, Ross Ihaka, Claire D. McWhite, Paul Murrell, Reto Stauffer, and Claus O. Wilke. 2019. “Colorspace: A Toolbox for Manipulating and Assessing Colors and Palettes.” ArXiv 1903.06490. arXiv.org E-Print Archive. http://arxiv.org/abs/1903.06490.