| 1 |
--- |
| 2 |
title: "colorspace: A Toolbox for Manipulating and Assessing Colors and Palettes" |
| 3 |
author: "Achim Zeileis, Jason C. Fisher, Kurt Hornik, Ross Ihaka, Claire D. McWhite, Paul Murrell, Reto Stauffer, Claus O. Wilke" |
| 4 |
output: |
| 5 |
html_document: |
| 6 |
toc: true |
| 7 |
toc_float: true |
| 8 |
theme: flatly |
| 9 |
bibliography: color.bib |
| 10 |
link-citations: true |
| 11 |
vignette: > |
| 12 |
%\VignetteIndexEntry{colorspace: A Toolbox for Manipulating and Assessing Colors and Palettes} |
| 13 |
%\VignetteEngine{knitr::rmarkdown} |
| 14 |
%\VignetteDepends{colorspace,ggplot2} |
| 15 |
%\VignetteKeywords{RGB, sRGB, XYZ, LUV, LAB, HLS, HSV, HCL, qualitative palette, sequential palette, diverging palette, shiny, visualization, color vision deficiency} |
| 16 |
%\VignettePackage{colorspace} |
| 17 |
--- |
| 18 |
|
| 19 |
```{r preliminaries, echo=FALSE, message=FALSE} |
| 20 |
library("colorspace") |
| 21 |
library("ggplot2") |
| 22 |
theme_set(theme_minimal()) |
| 23 |
prefix <- "https://colorspace.R-Forge.R-project.org/articles/" ## "" |
| 24 |
``` |
| 25 |
|
| 26 |
## Overview |
| 27 |
|
| 28 |
The _colorspace_ package provides a broad toolbox for selecting individual |
| 29 |
colors or color palettes, manipulating these colors, and employing |
| 30 |
them in various kinds of visualizations. |
| 31 |
|
| 32 |
At the core of the package there are various utilities for computing with |
| 33 |
color spaces (as the name of the package conveys). Thus, the package helps to map various three-dimensional |
| 34 |
representations of color to each other. A particularly important |
| 35 |
mapping is the one from the perceptually-based and device-independent color model |
| 36 |
HCL (Hue-Chroma-Luminance) to standard Red-Green-Blue (sRGB) which is the basis for color |
| 37 |
specifications in many systems based on the corresponding hex codes (e.g., in HTML but also |
| 38 |
in R). For completeness further standard color models are included as well in the package: |
| 39 |
`polarLUV()` (= HCL), `LUV()`, `polarLAB()`, `LAB()`, `XYZ()`, `RGB()`, `sRGB()`, `HLS()`, |
| 40 |
`HSV()`. |
| 41 |
|
| 42 |
The HCL space (= polar coordinates in CIELUV) is particularly useful for |
| 43 |
specifying individual colors and color palettes as its three axes match those |
| 44 |
of the human visual system very well: Hue (= type of color, dominant wavelength), |
| 45 |
chroma (= colorfulness), luminance (= brightness). |
| 46 |
|
| 47 |
```{r hcl-properties, echo = FALSE, message = FALSE, warning = FALSE, fig.width = 4, fig.height = 2.2, fig.align = "center", dev = "png"} |
| 48 |
swatchplot( |
| 49 |
"Hue" = sequential_hcl(5, h = c(0, 300), c = c(60, 60), l = 65), |
| 50 |
"Chroma" = sequential_hcl(5, h = 0, c = c(100, 0), l = 65, rev = TRUE, power = 1), |
| 51 |
"Luminance" = sequential_hcl(5, h = 260, c = c(25, 25), l = c(25, 90), rev = TRUE, power = 1), |
| 52 |
off = 0 |
| 53 |
) |
| 54 |
``` |
| 55 |
|
| 56 |
The _colorspace_ package provides three types of palettes based on the HCL model: |
| 57 |
|
| 58 |
* _Qualitative:_ Designed for coding categorical information, i.e., |
| 59 |
where no particular ordering of categories is available and every color |
| 60 |
should receive the same perceptual weight. Function: `qualitative_hcl()`. |
| 61 |
* _Sequential:_ Designed for coding ordered/numeric information, i.e., |
| 62 |
where colors go from high to low (or vice versa). Function: `sequential_hcl()`. |
| 63 |
* _Diverging:_ Designed for coding ordered/numeric information around a central |
| 64 |
neutral value, i.e., where colors diverge from neutral to two extremes. |
| 65 |
Function: `diverging_hcl()`. |
| 66 |
|
| 67 |
To aid choice and application of these palettes there are: scales for use |
| 68 |
with _ggplot2_; _shiny_ (and _tcltk_) apps for interactive exploration; |
| 69 |
visualizations of palette properties; accompanying manipulation utilities |
| 70 |
(like desaturation, lighten/darken, and emulation of color vision deficiencies). |
| 71 |
|
| 72 |
More detailed overviews and examples are provided in the articles: |
| 73 |
|
| 74 |
* [Color Spaces: S4 Classes and Utilities](`r prefix`color_spaces.html) |
| 75 |
* [HCL-Based Color Palettes](`r prefix`hcl_palettes.html) |
| 76 |
* [HCL-Based Color Scales for _ggplot2_](`r prefix`ggplot2_color_scales.html) |
| 77 |
* [Palette Visualization and Assessment](`r prefix`palette_visualization.html) |
| 78 |
* [Apps for Choosing Colors and Palettes Interactively](`r prefix`hclwizard.html) |
| 79 |
* [Color Vision Deficiency Emulation](`r prefix`color_vision_deficiency.html) |
| 80 |
* [Color Manipulation and Utilities](`r prefix`manipulation_utilities.html) |
| 81 |
* [Approximating Palettes from Other Packages](`r prefix`approximations.html) |
| 82 |
* [Somewhere over the Rainbow](`r prefix`endrainbow.html) |
| 83 |
|
| 84 |
|
| 85 |
## Installation |
| 86 |
|
| 87 |
The stable release version of _colorspace_ is hosted on the Comprehensive R Archive Network |
| 88 |
(CRAN) at <https://CRAN.R-project.org/package=colorspace> and can be installed via |
| 89 |
|
| 90 |
```{r installation-cran, eval=FALSE} |
| 91 |
install.packages("colorspace") |
| 92 |
``` |
| 93 |
|
| 94 |
The development version of _colorspace_ is hosted on R-Forge at |
| 95 |
<https://R-Forge.R-project.org/projects/colorspace/> in a Subversion (SVN) repository. |
| 96 |
It can be installed via |
| 97 |
|
| 98 |
```{r installation-rforge, eval=FALSE} |
| 99 |
install.packages("colorspace", repos = "https://R-Forge.R-project.org") |
| 100 |
``` |
| 101 |
|
| 102 |
A Python version is available from the Python Package Index (PyPI) at |
| 103 |
<https://pypi.org/project/colorspace/> with documentation at |
| 104 |
<https://retostauffer.github.io/python-colorspace/>. |
| 105 |
|
| 106 |
|
| 107 |
## Choosing HCL-based color palettes |
| 108 |
|
| 109 |
The _colorspace_ package ships with a wide range of predefined color palettes, |
| 110 |
specified through suitable trajectories in the HCL (hue-chroma-luminance) color space. |
| 111 |
A quick overview can be gained easily with the `hcl_palettes()` function: |
| 112 |
|
| 113 |
```{r hcl-palettes, message = FALSE, warning = FALSE, fig.align = "left", fig.height = 9, fig.width = 16, dpi = 48, out.width = "100%"} |
| 114 |
library("colorspace") |
| 115 |
hcl_palettes(plot = TRUE) |
| 116 |
``` |
| 117 |
|
| 118 |
A suitable vector of colors can be easily computed by specifying the desired number of colors and the palette name (see the plot above), e.g., |
| 119 |
|
| 120 |
```{r qualitative-hcl-4} |
| 121 |
q4 <- qualitative_hcl(4, palette = "Dark 3") |
| 122 |
q4 |
| 123 |
``` |
| 124 |
|
| 125 |
The functions `sequential_hcl()`, and `diverging_hcl()` work analogously. Additionally, |
| 126 |
their hue/chroma/luminance parameters can be modified, thus allowing for easy customization of |
| 127 |
each palette. Moreover, the `choose_palette()`/`hclwizard()` app provide convenient user |
| 128 |
interfaces to perform palette customization interactively. Finally, even more flexible diverging |
| 129 |
HCL palettes are provided by `divergingx_hcl()`. |
| 130 |
|
| 131 |
|
| 132 |
## Usage with base graphics |
| 133 |
|
| 134 |
The color vectors returned by the HCL palette functions can usually be passed directly |
| 135 |
to most base graphics, typically through the `col` argument. Here, the `q4` |
| 136 |
vector created above is used in a time series display: |
| 137 |
|
| 138 |
```{r eustockmarkets, eval = FALSE} |
| 139 |
plot(log(EuStockMarkets), plot.type = "single", col = q4, lwd = 2) |
| 140 |
legend("topleft", colnames(EuStockMarkets), col = q4, lwd = 3, bty = "n") |
| 141 |
``` |
| 142 |
|
| 143 |
```{r eustockmarkets-plot, echo = FALSE, message = FALSE, warning = FALSE, fig.align = "left", fig.height = 4, fig.width = 6, dpi = 48, out.width = "100%"} |
| 144 |
q4 <- qualitative_hcl(4) |
| 145 |
par(mar = c(5, 4, 1, 1)) |
| 146 |
plot(log(EuStockMarkets), plot.type = "single", col = q4, lwd = 2) |
| 147 |
legend("topleft", colnames(EuStockMarkets), col = q4, lwd = 3, bty = "n") |
| 148 |
``` |
| 149 |
|
| 150 |
As another example for a sequential palette, we demonstrate how to create a spine plot |
| 151 |
displaying the proportion of Titanic passengers that survived per class. |
| 152 |
The `Purples 3` palette is used, which is quite similar to the **ColorBrewer.org** |
| 153 |
palette `Purples`. Here, only two colors are employed, yielding a dark purple |
| 154 |
and a light gray. |
| 155 |
|
| 156 |
```{r titanic, eval = FALSE} |
| 157 |
ttnc <- margin.table(Titanic, c(1, 4)) |
| 158 |
spineplot(ttnc, col = sequential_hcl(2, palette = "Purples 3")) |
| 159 |
``` |
| 160 |
|
| 161 |
```{r titanic-plot, echo = FALSE, message = FALSE, warning = FALSE, fig.align = "left", fig.height = 4, fig.width = 6, dpi = 48, out.width = "100%"} |
| 162 |
ttnc <- margin.table(Titanic, c(1, 4)) |
| 163 |
par(mar = c(5, 4, 1, 1)) |
| 164 |
spineplot(ttnc, col = sequential_hcl(2, "Purples 3")) |
| 165 |
``` |
| 166 |
|
| 167 |
|
| 168 |
## Usage with _ggplot2_ |
| 169 |
|
| 170 |
To provide access to the HCL color palettes from within _ggplot2_ graphics suitable discrete and/or |
| 171 |
continuous _ggplot2_ color scales are provided. The scales are named via the scheme |
| 172 |
`scale_<aesthetic>_<datatype>_<colorscale>()`, where `<aesthetic>` is the name |
| 173 |
of the aesthetic (`fill`, `color`, `colour`), `<datatype>` is the type of the |
| 174 |
variable plotted (`discrete` or `continuous`) and `<colorscale>` sets the type |
| 175 |
of the color scale used (`qualitative`, `sequential`, `diverging`, |
| 176 |
`divergingx`). |
| 177 |
|
| 178 |
To illustrate their usage two simple examples are shown using the qualitative `Dark 3` |
| 179 |
and sequential `Purples 3` palettes that were also employed above. For the first example, semi-transparent |
| 180 |
shaded densities of the sepal length from the iris data are shown, grouped by species. |
| 181 |
|
| 182 |
```{r iris-ggplot, message = FALSE, warning = FALSE, fig.align = "left", fig.height = 4, fig.width = 6, dpi = 48, out.width = "100%"} |
| 183 |
library("ggplot2") |
| 184 |
ggplot(iris, aes(x = Sepal.Length, fill = Species)) + geom_density(alpha = 0.6) + |
| 185 |
scale_fill_discrete_qualitative(palette = "Dark 3") |
| 186 |
``` |
| 187 |
|
| 188 |
And for the second example the sequential palette is used to code the cut levels in a scatter of price by carat |
| 189 |
in the diamonds data (or rather a small subsample thereof). The scale function first |
| 190 |
generates six colors but then drops the first color because the light gray is too light |
| 191 |
here. (Alternatively, the chroma and luminance parameters could also be tweaked.) |
| 192 |
|
| 193 |
```{r diamonds-ggplot, message = FALSE, warning = FALSE, fig.align = "left", fig.height = 4, fig.width = 6, dpi = 48, out.width = "100%"} |
| 194 |
dsamp <- diamonds[1 + 1:1000 * 50, ] |
| 195 |
ggplot(dsamp, aes(carat, price, color = cut)) + geom_point() + |
| 196 |
scale_color_discrete_sequential(palette = "Purples 3", nmax = 6, order = 2:6) |
| 197 |
``` |
| 198 |
|
| 199 |
|
| 200 |
## Palette visualization and assessment |
| 201 |
|
| 202 |
The _colorspace_ package also provides a number of functions that aid visualization and |
| 203 |
assessment of its palettes. |
| 204 |
|
| 205 |
* `demoplot()` can display a palette (with arbitrary number of colors) in a range of |
| 206 |
typical and somewhat simplified statistical graphics. |
| 207 |
* `hclplot()` converts the colors of a palette to the corresponding hue/chroma/luminance |
| 208 |
coordinates and displays them in HCL space with one dimension collapsed. The collapsed |
| 209 |
dimension is the luminance for qualitative palettes and the hue for sequential/diverging palettes. |
| 210 |
* `specplot()` also converts the colors to hue/chroma/luminance coordinates but draws |
| 211 |
the resulting spectrum in a line plot. |
| 212 |
|
| 213 |
For the qualitative `Dark 3` palette from above the following plots can be obtained. |
| 214 |
|
| 215 |
```{r visualiation-qualitative, eval = FALSE} |
| 216 |
demoplot(q4, "bar") |
| 217 |
hclplot(q4) |
| 218 |
specplot(q4, type = "o") |
| 219 |
``` |
| 220 |
|
| 221 |
```{r allplots-qualitative, echo = FALSE, fig.height = 4.5, fig.width = 14, fig.align = "center", dev = "png", dpi = 48, out.width = "100%"} |
| 222 |
allplots <- function(palette, ...) { |
| 223 |
layout(cbind(1, 2, 3:4), heights = c(2, 10)) |
| 224 |
par(oma = c(2, 5, 2, 3), mar = rep(0.5, 4)) |
| 225 |
demoplot(palette, ...) |
| 226 |
hclplot(palette) |
| 227 |
par(xaxt = "n", yaxt = "n", mar = c(0.2, 3, 0.2, 0), cex = 1) |
| 228 |
image(matrix(seq_along(palette), ncol = 1L), col = palette) |
| 229 |
par(yaxt = "s") |
| 230 |
specplot(palette, type = "o", palette = FALSE, oma = FALSE, mar = c(0.2, 3, 0.2, 0)) |
| 231 |
} |
| 232 |
allplots(q4, "bar") |
| 233 |
``` |
| 234 |
|
| 235 |
The bar plot is used as a typical application for a qualitative palette (in addition to the |
| 236 |
time series and density plots used above). The other two displays show that luminance |
| 237 |
is (almost) constant in the palette while the hue changes linearly along the color |
| 238 |
"wheel". Ideally, chroma would have also been constant to completely balance the colors. |
| 239 |
However, at this luminance the maximum chroma differs across hues so that the palette |
| 240 |
is fixed up to use less chroma for the yellow and green elements. |
| 241 |
|
| 242 |
Note also that in a bar plot areas are shaded (and not just points or lines) so that |
| 243 |
lighter colors would be preferable. In the density plot above |
| 244 |
this was achieved through semi-transparency. Alternatively, luminance could be increased |
| 245 |
as is done in the `"Pastel 1"` or `"Set 3"` palettes. |
| 246 |
|
| 247 |
Subsequently, the same types of assessment are carried out for the sequential `"Purples 3"` palette |
| 248 |
as employed above. |
| 249 |
|
| 250 |
```{r visualization-sequential, eval = FALSE} |
| 251 |
s9 <- sequential_hcl(9, "Purples 3") |
| 252 |
demoplot(s9, "heatmap") |
| 253 |
hclplot(s9) |
| 254 |
specplot(s9, type = "o") |
| 255 |
``` |
| 256 |
|
| 257 |
```{r allplots-sequential, echo = FALSE, fig.height = 4.5, fig.width = 14, fig.align = "center", dev = "png", dpi = 48, out.width = "100%"} |
| 258 |
s9 <- sequential_hcl(9, "Purples 3") |
| 259 |
allplots(s9, "heatmap") |
| 260 |
``` |
| 261 |
|
| 262 |
Here, a heatmap (based on the well-known Maunga Whau volcano data) is used as a typical |
| 263 |
application for a sequential palette. The elevation of the volcano is brought out clearly, |
| 264 |
using dark colors to give emphasis to higher elevations. |
| 265 |
|
| 266 |
The other two displays show that hue is constant in the palette while luminance and chroma vary. |
| 267 |
Luminance increases monotonically from dark to light (as required for a proper sequential palette). |
| 268 |
Chroma is triangular-shaped which allows the viewer to better distinguish the middle colors in the palette |
| 269 |
when compared to a monotonic chroma trajectory. |