D Using colors in R
Using colors is one of the most powerful ways to structure 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 or technical. Thanks to its integrated graphics and grDevices packages, any installation of R comes fully-loaded with colors and tools for manipulating them. This means that most users of R do not need to understand the details of color theory to create beautiful visualizations. However, to efficiently find, choose, and combine colors, we need to know where to look and in which ways colors are represented in R.
This appendix provides a primer on finding, choosing, and using colors in R. A deeper knowledge about the range of color options in R directly 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.)
This chapter uses the unikn package (Neth & Gradwohl, 2021) 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
usecol() functions here.
As unikn is imported when installing the ds4psy package, there is no need for installing it separately.