D Using colors in R
Using colors is one of the most powerful ways to structure information. In scientific visualizations, the use of color can reach from ornamental eye-candy to eye-opening revelation of key information. Whereas understanding the concept of color is an ambitous endeavor — involving various arts, humanities, and sciences — using colors effectively requires knowledge, creativity, and experience (e.g., in choosing or creating appropriate color scales).
While this is easily the most colorful chapter of this book, defining and selecting colors in R can seem rather dull and technical. Thanks to its integrated graphics and grDevices packages, any installation of R comes fully-loaded with colors and tools for manipulating them. Consequently, most R users do not need to understand the details of color theory for creating beautiful visualizations. However, to efficiently find, choose, and combine colors, we need some additional insights and tools.
This appendix provides a primer on finding, choosing, and using colors in R. Beyond explaining different ways in which colors are represented in R, it introduces some R packages that allow to find, modify and use beautiful color palettes. A deeper knowledge about the range of color options in R complements the chapter on Visualizing data (see Chapter 2), but is also useful in itself.
As this chapter focuses on the practical aspects of choosing and defining colors in R, we side-step the fundamental theoretical issues surrounding the nature and perception of colors. (Consult the Wikipedia articles on Color and Color vision for an introduction and links, and read Fabio Crameri, Shephard, & Heron, 2020 for an excellent overview of using and misusing color in science communication.)
Disclaimer
This chapter uses the unikn package (Neth & Gradwohl, 2024) for easily displaying and modifying color palettes.
This is less motivated by its functionality (though see Section D.4.3) than by our familiarity with it.
Whereas its color palettes are used throughout this book, it is mostly used for its seecol()
and usecol()
functions here.
As unikn is imported when installing the ds4psy package, there is no need for installing it separately.