9 min read

Using Color in R Plots


swirled primary colors
Photo by Daniel Olah on Unsplash.

View raw source for this post

Summary

Using color effectively is key for informative and accurate plots. R packages can provide tried-and-true palettes to ease the process.

Table of Contents

Overview

Colors are hard. Beginners have two choices. They can rely on their intuition or a preselected palette from one of R’s many packages.[1] The overwhelming consensus is that those lacking design experience should “instead draw from preconstructed ramps.” [1] The good news is that “automatically generated color ramps” can perform at least as well as designer ramps in many circumstances. [1] Fortunately, R has many options available.

Some basic design principles should be kept at the forefront. An appropriate color selection should accomplish three things. First, the colors should not be unappealing. [2] Large areas of highly saturated colors should be avoided as viewers quickly tire and it can cause after-image effects.[2] Second, the colors should “cooperate” with each other, meaning that any one color should both be appealing by itself and when viewed with the others. [2] Third, “the colors should work everywhere”. [2] The colors should be distinguishable regardless of how they are displayed. The plot should convey the same meaning if it’s shown on a projector, a computer screen or a facsimile.

You can find help if you know where to look. ?grDevices::hcl.colors or ?grDevices::palette are great places to start. For brevity, the post will only address a palette of five categorical/qualitative color choices from the grDevices, RColorBrewer and colorspace packages. The post concludes by recommending the colorspace package as an excellent starting point for most analysts.

Terms

In reviewing discussions about color, the following terms are helpful to know:

General

  • Color palette is a collection of colors.

  • Color ramp is a synonym for a collection or palette of colors.

  • Color space is a specific organization of colors.

Color Blindness

  • Deuteranomaly is a type of color blindness from the defectiveness of green cone cells.

  • Protanomaly is a type of color blindness from the defectiveness of red cone cells.

  • Tritanomaly is a type of colorblindness from the defectiveness of blue cone cells.

Color Spaces

  • HSV is an acronym for “hue-saturation-value” and is a simple transformation of the RGB palette. HSV palletes are disfavored.

  • HCL is an acronym for “hue-chroma-luminance”. HCL palettes are preferred because they account for human color perception.

  • RGB is an acronym for “red-green-blue”.

Palette Types

  • Qualitative palettes are used for categorical data “where no particular ordering of catgories is available and every color should receive the same perceptual weight.”

  • Sequential palettes are used for “coding ordered/numeric information where colors go from high to low or vice versa.”

  • Diverging palettes are “designed for coding numeric information around a central neutral value.” Their use shows contrasts between two extremes.

Hexadecimal Notation

Colors are often described in their hexadecimal equivalent, often with a leading “#”. A hex triplet is a six-digit, three-byte hexadecimal number ranging from 00 to FF or 0 to 255 in decimal notation. The color intensity ranges from 0 (least intense) to 255 (most intense). A hexadecimal color is specified with #RRGGBB. RR (red), GG (green) and BB (blue) are hexadecimal integers between 00 and FF. The number of potential colors are 16,777,216. R uses hexadecimal notation for color. White, for example, is “#FFFFFF”. R also uses names for 657 colors like “olivedrab2.”

Conversion

R translates color naming strategies into hexadecimal notation. Hexadecimal notation can contain an additional two digits for specifying transparency.

RGB

rgb (red-blue-green) converts colors in the red, green and blue primaries to hexadecimal. A transparency argument can be set as well with the “alpha” argument. The specification refers to the “sRGB” where “s” represents “standard RGB” and is based on IEC standard 61966. (Disfavored).

# usage rgb(r, g, b, maxColorValue=255, alpha=255)
rgb(red = 0, green = 1, blue = 0, maxColorValue = 200, alpha = 50)
[1] "#00010040"

HSV

hsv (hue-saturation-value) creates a vector of hexadecimal values from hue, saturation, and value. It is a simple transformation of the RGB space. (Disfavored).

# usage hsv(h = 1, s = 1, v = 1, alpha)
hsv(0.5, 0.5, 0.5)
[1] "#408080"

HCL

hcl (hue-chroma-luminance) creates hexadecimal values from the hue, chroma, and luminance and is derived from human color perception. (Preferred).

# usage hcl(h = 0, c = 35, l = 85, alpha, fixup = TRUE)
hcl(h = 0, c = 35, l = 85)
[1] "#FFC5D0"

Avoid the rainbow palette. Instead, use a palette from hcl.colors().

Color Packages

There are many packages in R that can provide a color palette. A partial list includes: RColorBrewer, viridis, rcartocolor, wesanderson, and scico. Other packages like pals and paletteer combine the palettes from the different packages into a “unified interface.”

Here, three packages will be used to generate palettes: grDevices, RColorBrewer and colorspace.

grDevices

The grDevices package contains several palettes.

The hcl.colors palette was omitted because grDevices incorporates it from the colorspace package.

Figure 1: The hcl.colors palette was omitted because grDevices incorporates it from the colorspace package.

RColorBrewer

RColorBrewer was my “go-to” color package for many years. The package is easy to use and the many palettes can be seen with the command display.brewer.all().

colorspace

The “[C]olorspace package can derive general and adaptable strategies for color palettes; manipulate individual colors and color palettes; and assess and visualize the properties of color palettes (beyond simple color swatches).”[3] Like other packages, colorspace provides qualitative, sequential and diverging palettes.

The authors of colorspace have provided a suite of tools to assist in palette selection. To start, you can run the command hcl_palettes(plot = TRUE) and see the different color selections.

Remember that the “HCL” space is “particularly useful for specifying individual colors and color palettes as its three axes match those of the human visual system very well”.[3] A vector of HCL colors can be generated by specifying the (1) kind of palette desired, (2) the number of colors and (3) the name of the palette:

colorspace::qualitative_hcl(3, "Set3")
colorspace::sequential_hcl(4, "Blues3")
colorspace::diverge_hcl(5, "Tropic")

For additional palettes, run hcl_palettes().

Accessibility

Color is helpful if the viewer can see it. However, many people have some form of color blindness with men at greater risk. Red–green color blindness affects up to 1 in 12 males (8%) and 1 in 200 females (0.5%). Wikipedia, Color Blindness.

Generating palettes can accomodate color blind viewers. A graphical user interface at https://hclwizard.org shows what colorblind viewers see.

Conclusion

Do not handpick your palette. Instead, rely on a tried-and-true package. Avoid fully saturated or “loud” colors. The colorspace package can offer a wide variety of palettes that create attractive visualizations.

Acknowledgements

In addition to the cited packages and journals, this post was made imminently better thanks to:

  • Melanie Frazier at the National Center for Ecological Analysis and Synthesis. Dr. Frazier prepared a color “cheat sheet” that was extremely helpful.

References

[1]
S. Smart, K. Wu, and D. A. Szafir, “Color Crafting: Automating the Construction of Designer Quality Color Ramps,” IEEE Transactions on Visualization and Computer Graphics, vol. 26, no. 1, pp. 1215–1225, Jan. 2020, doi: 10.1109/TVCG.2019.2934284.
[2]
A. Zeileis, K. Hornik, and P. Murrell, “Escaping RGBland: Selecting colors for statistical graphics,” Computational Statistics & Data Analysis, vol. 53, no. 9, pp. 3259–3270, 2009, doi: 10.1016/j.csda.2008.11.033.
[3]
A. Zeileis et al., “Colorspace: A Toolbox for Manipulating and Assessing Colors and Palettes.” arXiv, 14-Mar-2019 [Online]. Available: http://arxiv.org/abs/1903.06490. [Accessed: 16-Aug-2022]
[4]
R Core Team, R: A language and environment for statistical computing. Vienna, Austria: R Foundation for Statistical Computing, 2022 [Online]. Available: https://www.R-project.org/
[5]
Y. Xie, C. Dervieux, and A. Presmanes Hill, Blogdown: Create blogs and websites with r markdown. 2022 [Online]. Available: https://CRAN.R-project.org/package=blogdown
[6]
R. Ihaka et al., Colorspace: A toolbox for manipulating and assessing colors and palettes. 2022 [Online]. Available: https://CRAN.R-project.org/package=colorspace
[7]
E. Neuwirth, RColorBrewer: ColorBrewer palettes. 2022 [Online]. Available: https://CRAN.R-project.org/package=RColorBrewer

Disclaimer

The views, analysis and conclusions presented within this paper represent the author’s alone and not of any other person, organization or government entity. While I have made every reasonable effort to ensure that the information in this article was correct, it will nonetheless contain errors, inaccuracies and inconsistencies. It is a working paper subject to revision without notice as additional information becomes available. Any liability is disclaimed as to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from negligence, accident, or any other cause. The author(s) received no financial support for the research, authorship, and/or publication of this article.

Reproducibility

─ Session info ───────────────────────────────────────────────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.1.3 (2022-03-10)
 os       macOS Big Sur/Monterey 10.16
 system   x86_64, darwin17.0
 ui       X11
 language (EN)
 collate  en_US.UTF-8
 ctype    en_US.UTF-8
 tz       America/New_York
 date     2022-07-31
 pandoc   2.18 @ /Applications/RStudio.app/Contents/MacOS/quarto/bin/tools/ (via rmarkdown)

─ Packages ───────────────────────────────────────────────────────────────────────────────────────────────────────────
 package     * version    date (UTC) lib source
 assertthat    0.2.1      2019-03-21 [1] CRAN (R 4.1.0)
 blogdown    * 1.9        2022-03-28 [1] CRAN (R 4.1.2)
 bookdown      0.25       2022-03-16 [1] CRAN (R 4.1.2)
 bslib         0.4.0.9000 2022-07-27 [1] Github (rstudio/bslib@f33429b)
 cachem        1.0.6      2021-08-19 [1] CRAN (R 4.1.0)
 callr         3.7.0      2021-04-20 [1] CRAN (R 4.1.0)
 cli           3.3.0      2022-04-25 [1] CRAN (R 4.1.2)
 codetools     0.2-18     2020-11-04 [1] CRAN (R 4.1.3)
 colorspace    2.0-3      2022-02-21 [1] CRAN (R 4.1.2)
 crayon        1.5.1      2022-03-26 [1] CRAN (R 4.1.0)
 DBI           1.1.2      2021-12-20 [1] CRAN (R 4.1.0)
 devtools    * 2.4.3      2021-11-30 [1] CRAN (R 4.1.0)
 digest        0.6.29     2021-12-01 [1] CRAN (R 4.1.0)
 dplyr         1.0.9      2022-04-28 [1] CRAN (R 4.1.2)
 ellipsis      0.3.2      2021-04-29 [1] CRAN (R 4.1.0)
 evaluate      0.15       2022-02-18 [1] CRAN (R 4.1.2)
 fansi         1.0.3      2022-03-24 [1] CRAN (R 4.1.2)
 farver        2.1.0      2021-02-28 [1] CRAN (R 4.1.0)
 fastmap       1.1.0      2021-01-25 [1] CRAN (R 4.1.0)
 fs            1.5.2      2021-12-08 [1] CRAN (R 4.1.0)
 generics      0.1.2      2022-01-31 [1] CRAN (R 4.1.2)
 ggplot2     * 3.3.6      2022-05-03 [1] CRAN (R 4.1.2)
 ggthemes    * 4.2.4      2021-01-20 [1] CRAN (R 4.1.0)
 glue          1.6.2      2022-02-24 [1] CRAN (R 4.1.2)
 gtable        0.3.0      2019-03-25 [1] CRAN (R 4.1.0)
 highr         0.9        2021-04-16 [1] CRAN (R 4.1.0)
 htmltools     0.5.3      2022-07-18 [1] CRAN (R 4.1.2)
 jquerylib     0.1.4      2021-04-26 [1] CRAN (R 4.1.0)
 jsonlite      1.8.0      2022-02-22 [1] CRAN (R 4.1.2)
 knitr         1.39       2022-04-26 [1] CRAN (R 4.1.2)
 labeling      0.4.2      2020-10-20 [1] CRAN (R 4.1.0)
 lifecycle     1.0.1      2021-09-24 [1] CRAN (R 4.1.0)
 magrittr      2.0.3      2022-03-30 [1] CRAN (R 4.1.2)
 memoise       2.0.1      2021-11-26 [1] CRAN (R 4.1.0)
 munsell       0.5.0      2018-06-12 [1] CRAN (R 4.1.0)
 pillar        1.7.0      2022-02-01 [1] CRAN (R 4.1.2)
 pkgbuild      1.3.1      2021-12-20 [1] CRAN (R 4.1.0)
 pkgconfig     2.0.3      2019-09-22 [1] CRAN (R 4.1.0)
 pkgload       1.3.0      2022-06-27 [1] CRAN (R 4.1.2)
 prettyunits   1.1.1      2020-01-24 [1] CRAN (R 4.1.0)
 processx      3.5.3      2022-03-25 [1] CRAN (R 4.1.0)
 ps            1.7.0      2022-04-23 [1] CRAN (R 4.1.2)
 purrr         0.3.4      2020-04-17 [1] CRAN (R 4.1.0)
 R6            2.5.1      2021-08-19 [1] CRAN (R 4.1.0)
 remotes       2.4.2      2021-11-30 [1] CRAN (R 4.1.0)
 rlang         1.0.4      2022-07-12 [1] CRAN (R 4.1.2)
 rmarkdown     2.14       2022-04-25 [1] CRAN (R 4.1.2)
 rstudioapi    0.13       2020-11-12 [1] CRAN (R 4.1.3)
 sass          0.4.2      2022-07-16 [1] CRAN (R 4.1.2)
 scales        1.2.0      2022-04-13 [1] CRAN (R 4.1.2)
 sessioninfo   1.2.2      2021-12-06 [1] CRAN (R 4.1.0)
 stringi       1.7.8      2022-07-11 [1] CRAN (R 4.1.2)
 stringr       1.4.0      2019-02-10 [1] CRAN (R 4.1.0)
 tibble        3.1.7      2022-05-03 [1] CRAN (R 4.1.2)
 tidyselect    1.1.2      2022-02-21 [1] CRAN (R 4.1.2)
 usethis     * 2.1.6      2022-05-25 [1] CRAN (R 4.1.2)
 utf8          1.2.2      2021-07-24 [1] CRAN (R 4.1.0)
 vctrs         0.4.1      2022-04-13 [1] CRAN (R 4.1.2)
 withr         2.5.0      2022-03-03 [1] CRAN (R 4.1.0)
 xfun          0.31       2022-05-10 [1] CRAN (R 4.1.2)
 yaml          2.3.5      2022-02-21 [1] CRAN (R 4.1.2)

 [1] /Library/Frameworks/R.framework/Versions/4.1/Resources/library

──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────