-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.Rmd
318 lines (226 loc) · 10.5 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
---
title: "datacolor"
author: Éric Marty
date: August 1, 2018
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "README-"
)
```
Color for Data Visualization
<!-- https://ecotrust-canada.github.io/markdown-toc/ -->
# Table of Contents
- [Introduction](#introduction)
* [Installation](#installation)
- [Analysis and display of palettes](#analysis-and-display-of-palettes)
- [Converters](#converters)
* [Hex RGB to HCL](#hex-rgb-to-hcl)
* [Hex RGB palette to simulated colorblind hex RGB](#hex-rgb-palette-to-simulated-colorblind-hex-rgb)
- [Precalculated palettes](#precalculated-palettes)
* [7 category qualitative palette for nominal data](#7-category-qualitative-palette-for-nominal-data)
* [Sequential palette for heatmaps](#sequential-palette-for-heatmaps)
+ [Comparison of Heat Palettes](#comparison-of-heat-palettes)
* [Divergent colorscale for AUC heatmaps](#divergent-colorscale-for-auc-heatmaps)
- [HCL Palettes with power curves, stepped and cyclic elements](#hcl-palettes-with-power-curves-stepped-and-cyclic-elements)
- [Multihue HCL Palettes with bezier interpolation and lightness correction](#multihue-hcl-palettes-with-bezier-interpolation-and-lightness-correction)
* [Comparison with non-bezier multi-hue palettes](#comparison-with-non-bezier-multi-hue-palettes)
# Introduction
This package provides
* prebuilt color palettes appropriate for data representation,
* functions for defining color palettes for data representation,
* utilities for analysis of palettes as perceived by normal and colorblind viewers.
Color palettes for data should ideally be perceptually linear in all perceptual color dimensions
(hue, chroma or saturation, lightness or luminance). Ideally, palettes should aslo be color-blind
safe (i.e. they should preserve an appropriate mapping of data to perceived color for colorblind
viewers). Palettes may also need to be black and white printer-friendly, if there is a chance plots
will be printed in greyscale. Additional requirements for color scales can include the presence or
absence of discontinuities and corners, maximizing discrimination between levels, and respecting
semantic relationships between colors.
This package relies on the perceptually flat colorspace HCL, which represents color as preceived
by people with normal vision. HCL functions from the `grDevices` and `colorspace` packages. This
package also relies on the `dichromat` R package to simulate color perception under the three most
common forms of colorblindness.
## Installation
Install the `datacolor` package from github:
```{r gh-installation, eval = FALSE}
# install.packages("devtools")
# devtools::install_github("allopole/datacolor")
```
# Analysis and display of palettes
See `?colorbar` for description of `colorbar` and `colorplot` functions
```{r colorbar}
library(datacolor)
# colorbar
pal <- grDevices::rainbow(36)
colorbar(pal)
colorbar(pal,dots=TRUE)
colorbar(pal,colorblind=TRUE)
colorbar(pal,dots=TRUE,colorblind=TRUE)
```
```{r colorplot}
# colorplot
colorplot(pal)
colorplot(pal,dots=TRUE)
colorplot(pal,colorblind=TRUE)
colorplot(pal,dots=TRUE,colorblind=TRUE)
```
# Converters
## Hex RGB to HCL
See `?hex2hcl`
```{r hex2hcl}
pal <- grDevices::rainbow(7)
hex2hcl(pal)
```
## Hex RGB palette to simulated colorblind hex RGB
See `?colorblind`
```{r colorblind}
pal <- grDevices::rainbow(7)
colorblind(pal)
colorplot(colorblind(pal)$deuteranopia)
```
# Precalculated palettes
## 7 category qualitative palette for nominal data
See `?unipalette` for details
* designed for high contrast against a white background, with good distinction among colors
* colors distinguishable under two most common forms of colorblindness
* colors distinguishable in greyscale for b/w printing
* Good for bar charts, box plots, pies, etc.
* Line should be relatively thick
* Points should be relatively large
```{r unipalette}
colorplot(unipalette(),colorblind = TRUE)
## Sample plot
randomseries <- data.frame(x=1:4, y=runif(4*7), grp=rep(letters[1:7],each=4))
pal <- unipalette()
lattice::xyplot(y~x, group=grp, data=randomseries, type="b", pch=16, lwd=3, col = pal,
key = list(space = "right", lines = list(col=pal, lwd=3), text=list(letters[1:7]))
)
```
## Sequential palette for heatmaps
A replacement for `grDevices::heat.colors()` or `colorspace::heat_hcl()`
See `?heat` for details
Palette characteristics:
* perceptually linear
* color-blind-safe (under three most common forms of colorblindness) and print friendly
* under normal vision: linear lightness, chroma, and hue gradients
* under colorblind viewing, linear lightness, smooth chroma, flat hue gradients
* appropriate for display of temperature, intensity, etc.
```{r heat}
colorplot(datacolor::heat(128),colorblind = T)
mtext("datacolor::heat()", side=1, line = 2.5)
```
### Comparison of Heat Palettes
```{r heat_comparison}
n <- 128
colorplot(grDevices::heat.colors(n), colorblind = TRUE)
mtext("grDevices::heat.colors(128)", side=1, line = 2.5)
colorplot(viridis::inferno(128), colorblind = TRUE)
mtext("viridis::inferno(128)", side=1, line = 2.5)
colorplot(colorspace::heat_hcl(n), colorblind = TRUE)
mtext("colorspace::heat_hcl()", side=1, line = 2.5)
colorplot(datacolor::heat(128),colorblind = T)
mtext("datacolor::heat()", side=1, line = 2.5)
## Maroon to yellow through salmon and pink
stops <- c('#800000', '#CD4949', '#FF9696','#FFFF00')
palette <- multiHue(128,colors=stops)
colorplot(palette,colorblind = TRUE) # display palette
mtext("datacolor::multiHue(128,c('#800000', '#CD4949', '#FF9696','#FFFF00'))", side=1, line = 2.5)
```
## Divergent colorscale for AUC heatmaps
See `?AUColors` for details
Palette characteristics:
* color-blind-safe (under three most common forms of colorblindness)
* perceptually linear
* equal brightness gradients on both sides of central value
* constant hue and chroma on either side of central value
* Dark value for no correlation (near 0.5); bright values for high correlation (near 1 and 0)
* Blue for positive correlation (1); orange for negative correlation (0)
* NOT suitable for b/w printing, as low and high values are not distinguishable in greyscale (however, greyscale printing still accurately shows the absolute distance from 0.5)
```{r AUC}
## default palette (n = 10):
p <- AUColors()
colorplot(p,colorblind = TRUE)
## inverted and reversed, with grey center bin:
p <- AUColors(n=21,invert=TRUE,reverse=TRUE)
colorplot(p,colorblind = TRUE)
# Sample AUC Heat Map
randommatrix <- matrix(runif(12*6),ncol=6)
nlevels <- 10
lattice::levelplot(randommatrix,
at = seq(0,1,1/nlevels), # number of breaks
col.regions = AUColors(nlevels), # color map
aspect = "iso")
```
# HCL Palettes with power curves, stepped and cyclic elements
Use `rampx()`, `stepx()`, and `cyclx()` to construct numeric vectors of `H`, `C`, and `L`.
See `?cyclx` for description of `rampx()`, `stepx()`, and `cyclx()`.
Use `hcl2hex()` to convert HCL palette to a hex RGB palette.
Depricated: `cyclic_hcl`
```{r cyclx}
library(datacolor)
n <- 36
pal <- hcl2hex(
H=rampx(from=260,to=-100,n=n),
C=stepx(from=40,to=60,n=n,step.n=n/3),
L=100*cyclx(from=.75,to=.45,n=n,
step.n=n/3,exponent=1,
cyc.from=.8,cyc.to=1,cyc.n=n/3,cyc.exponent=2.5)
)
colorplot(pal)
```
# Multihue HCL Palettes with bezier interpolation and lightness correction
`multihue()`, `multihue.diverge()` and `multihue.constantL()` create multi-hue (multi "stop")
color scales with perceptually linear lightness gradients and smooth hue gradients (with no
first or second order discontinuities).
The palettes follow bezier curves through HCL space. The bezier curves are defined by two endpoint
colors and one or more optional intermediate color "stops." The intermediate stops are treated as
control points for the bezier curve, and are therefore not necessarily present in the final palette.
After interpolation, the palettes are stretched to counter any non-linearity in lightness gradient
arising from the presence of intermediate color stops. The resulting palette has a perceptually
linear lightness gradient.
`multihue.diverge()` builds a divergent palette by concatenating two bezier curves (defined by a
"left" and "right" set of color stops).
See `?multiHue` for description of `multiHue()`, `multiHue.diverge()` and `multiHue.constantL()`
For a discussion of bezier interpolation in HCL with lightness correction, see:
Gregor Aisch. *Mastering Multi-hued Color Scales with Chroma.js.* Blog post. Sep 9, 2013 <https://www.vis4.net/blog/2013/09/mastering-multi-hued-color-scales/>
Palette characteristics:
* perceptually linear lightness gradients (or constant lightness for `multihue.constantL()` )
* smooth hue and chroma gradient throughout (no "corners")
* multiple color stops allow increased color discrimination between levels
* hue and chroma gradients not necessarily linear. Only brightness is adjusted for linearity.
* `multihue()`, `multihue.diverge()` produce print-friendly palettes.
* `multihue.diverge()` may produce a palette with a central discontinuity (first or second order)
```{r multiHue}
## default = blue to yellow through green, 11 colors
p <- multiHue()
colorplot(p,colorblind=TRUE)
colorplot(p)
## Maroon to yellow through salmon and pink
stops <- c('#800000', '#CD4949', '#FF9696','#FFFF00')
palette <- multiHue(128,colors=stops)
colorplot(palette, colorblind=TRUE) # display palette
## Divergent (default)
p <- multiHue.diverge(128)
colorplot(p,colorblind=TRUE)
## Divergent with discontinuity: left = Blue to Cyan, right = Yellow to Red
leftstops <- c("#3136AA","#3980B0","#00C5C0","#8EFDFD")
rightstops <- c("#F3F300","#FF8CB4","#CD4C4C","#8F0000")
palette <- multiHue.diverge(128,leftstops,rightstops,continuous=FALSE)
colorplot(palette,colorblind=TRUE)
## Constant Lightness (and chroma) color palette:
## 4 color stops define the bezier curve through HCL space; n=6 colors are output.
palette <- multiHue.constantL(n=64, h=c(-180, -105, -45, 0),c=50,l=60)
colorplot(palette,colorblind=TRUE)
```
## Comparison with non-bezier multi-hue palettes
By comparison, here is the "rainbow" palette showing discontinuities typical of multi-hule palettes.
This palette includes corners (second order discontinuities) in lightness. From a perceptual point of view, this palette is not a continuous colors scale, but a multiply-diverging scale with three distinct articulation points (at 1/6, 1/2 and 5/6 of the palette length).
```{r rainbow}
pal <- grDevices::rainbow(128)
colorplot(pal,colorblind=TRUE)
```