Debian Codegraph @

man/hcl_palettes.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/hcl_palettes.R
3 \name{hcl_palettes}
4 \alias{hcl_palettes}
5 \alias{print.hcl_palettes}
6 \alias{summary.hcl_palettes}
7 \alias{plot.hcl_palettes}
8 \alias{qualitative_hcl}
9 \alias{sequential_hcl}
10 \alias{diverging_hcl}
11 \alias{diverge_hcl}
12 \title{HCL Color Palettes}
13 \usage{
14 hcl_palettes(type = NULL, palette = NULL, plot = FALSE, n = 5L, ...)
15
16 \method{print}{hcl_palettes}(x, ...)
17
18 \method{summary}{hcl_palettes}(object, ...)
19
20 \method{plot}{hcl_palettes}(x, n = 5L, fixup = TRUE, off = NULL, border = NULL, ...)
21
22 qualitative_hcl(
23 n,
24 h = c(0, 360 * (n - 1)/n),
25 c = 80,
26 l = 60,
27 fixup = TRUE,
28 alpha = 1,
29 palette = NULL,
30 rev = FALSE,
31 register = "",
32 ...,
33 h1,
34 h2,
35 c1,
36 l1
37 )
38
39 sequential_hcl(
40 n,
41 h = 260,
42 c = 80,
43 l = c(30, 90),
44 power = 1.5,
45 gamma = NULL,
46 fixup = TRUE,
47 alpha = 1,
48 palette = NULL,
49 rev = FALSE,
50 register = "",
51 ...,
52 h1,
53 h2,
54 c1,
55 c2,
56 l1,
57 l2,
58 p1,
59 p2,
60 cmax,
61 c.
62 )
63
64 diverging_hcl(
65 n,
66 h = c(260, 0),
67 c = 80,
68 l = c(30, 90),
69 power = 1.5,
70 gamma = NULL,
71 fixup = TRUE,
72 alpha = 1,
73 palette = NULL,
74 rev = FALSE,
75 register = "",
76 ...,
77 h1,
78 h2,
79 c1,
80 l1,
81 l2,
82 p1,
83 p2,
84 cmax
85 )
86 }
87 \arguments{
88 \item{type}{character indicating type of HCL palette.}
89
90 \item{palette}{character. Name of HCL color palette.}
91
92 \item{plot}{logical. Should the selected HCL color palettes be visualized?}
93
94 \item{n}{the number of colors (\eqn{\ge 1}{>= 1}) to be in the palette.}
95
96 \item{\dots}{Other arguments passed to \code{\link{hex}}.}
97
98 \item{x, object}{A \code{hcl_palettes} object.}
99
100 \item{fixup}{logical. Should the color be corrected to a valid RGB value?}
101
102 \item{off}{numeric. Vector of length 2 indicating horizontal and vertical
103 offsets between the color rectangles displayed.}
104
105 \item{border}{character. Color of rectangle borders.}
106
107 \item{h, h1, h2}{hue value in the HCL color description, has to be in [0, 360].}
108
109 \item{c, c., c1, c2}{chroma value in the HCL color description.}
110
111 \item{l, l1, l2}{luminance value in the HCL color description.}
112
113 \item{alpha}{numeric vector of values in the range \code{[0, 1]} for alpha
114 transparency channel (0 means transparent and 1 means opaque).}
115
116 \item{rev}{logical. Should the color palette vector be returned in reverse order?}
117
118 \item{register}{character. If set to a non-empty character string, the corresponding
119 palette is registered with that name for subsequent use (within the same session).}
120
121 \item{power, p1, p2}{control parameter determining how chroma and luminance should
122 be increased (1 = linear, 2 = quadratic, etc.).}
123
124 \item{gamma}{Deprecated.}
125
126 \item{cmax}{Maximum chroma value in the HCL color description.}
127 }
128 \value{
129 \code{qualitative_hcl}, \code{sequential_hcl}, \code{diverging_hcl} return
130 a vector of \code{n} color strings (hex codes).
131
132 The function \code{hcl_palettes} returns a data frame of class \code{"hcl_palettes"}
133 where each row contains information about one of the requested palettes (name, type,
134 HCL trajectory coordinates). Suitable \code{print}, \code{summary}, and \code{plot}
135 methods are available.
136 }
137 \description{
138 Qualitative, sequential (single-hue and multi-hue), and diverging
139 color palettes based on the HCL (hue-chroma-luminance) color model.
140 }
141 \details{
142 The HCL (hue-chroma-luminance) color model is a perceptual color model
143 obtained by using polar coordinates in CIE \code{\link{LUV}} space
144 (i.e., \code{\link{polarLUV}}), where steps of equal size correspond to
145 approximately equal perceptual changes in color. By taking polar coordinates
146 the resulting three dimensions capture the three perceptual axes very well:
147 hue is the type of color, chroma the colorfulness compared
148 to the corresponding gray, and luminance the brightness. This makes it relatively
149 easy to create balanced palettes through trajectories in this HCL space.
150 In contrast, in the more commonly-used \code{\link{HSV}} (hue-saturation-value)
151 model (a simple transformation of \code{\link{RGB}}), the three axes are
152 confounded so that luminance changes along with the hue leading to very
153 unbalanced palettes (see \code{\link{rainbow_hcl}} for further illustrations).
154
155 Three types of palettes are derived based on the HCL model: \itemize{
156 \item Qualitative: Designed for coding categorical information, i.e.,
157 where no particular ordering of categories is available and every color
158 should receive the same perceptual weight.
159 \item Sequential: Designed for coding ordered/numeric information, i.e.,
160 where colors go from high to low (or vice versa).
161 \item Diverging: Designed for coding numeric information around a central
162 neutral value, i.e., where colors diverge from neutral to two extremes.
163 }
164 The corresponding functions are \code{qualitative_hcl}, \code{sequential_hcl},
165 and \code{diverging_hcl}. Their construction principles are explained in more detail below.
166 At the core is the luminance axis (i.e., light-dark contrasts):
167 These are easily decoded by humans and matched to high-low differences in the underlying
168 data. Therefore, \code{sequential_hcl} palettes are always based on a \emph{monotonic}
169 luminance sequence whereas the colors in a \code{qualitative_hcl} palette all have the
170 \emph{same} luminance. Finally, \code{diverging_hcl} palettes use the same monotonic
171 luminance sequence in both \dQuote{arms} of the palette, i.e., correspond to
172 two balanced sequential palettes diverging from the same neutral value.
173 The other two axes, hue and chroma, are used to enhance the luminance information and/or
174 to further discriminate the color.
175
176 All three palette functions specify trajectories in HCL space and hence need either
177 single values or intervals of the coordinates \code{h}, \code{c}, \code{l}. Their
178 interfaces are always designed such that \code{h}, \code{c}, \code{l} can take vector
179 arguments (as needed) but alternatively or additionally \code{h1}/\code{h2},
180 \code{c1}/\code{cmax}/\code{c2}, and \code{l1}/\code{l2} can be specified. If so,
181 the latter coordinates overwrite the former.
182
183 \code{qualitative_hcl} distinguishes the underlying categories by a sequence of
184 hues while keeping both chroma and luminance constant to give each color in the
185 resulting palette the same perceptual weight. Thus, \code{h} should be a pair of
186 hues (or equivalently \code{h1} and \code{h2} can be used) with the starting and
187 ending hue of the palette. Then, an equidistant sequence between these hues is
188 employed, by default spanning the full color wheel (i.e, the full 360 degrees).
189 Chroma \code{c} (or equivalently \code{c1}) and luminance \code{l} (or equivalently
190 \code{l1}) are constants.
191
192 \code{sequential_hcl} codes the underlying numeric values by a monotonic sequence
193 of increasing (or decreasing) luminance. Thus, the \code{l} argument should provide
194 a vector of length 2 with starting and ending luminance (equivalently, \code{l1} and
195 \code{l2} can be used). Without chroma (i.e., \code{c = 0}), this simply corresponds
196 to a grayscale palette like \code{\link[grDevices]{gray.colors}}. For adding chroma, a simple
197 strategy would be to pick a single hue (via \code{h} or \code{h1}) and then decrease
198 chroma from some value (\code{c} or \code{c1}) to zero (i.e., gray) along with
199 increasing luminance. For bringing out the extremes (a dark high-chroma color vs.
200 a light gray) this is already very effective. For distinguishing also colors in the
201 middle two strategies can be employed: (a) Hue can be varied as well by specifying an
202 interval of hues in \code{h} (or beginning hue \code{h1} and ending hue \code{h2}).
203 (b) Instead of a decreasing chroma a triangular chroma trajectory can be employed
204 from \code{c1} over \code{cmax} to \code{c2} (or equivalently a vector \code{c} of
205 length 3). This yields high-chroma colors in the middle of the palette that are
206 more easily distinguished from the dark and light extremes. Finally, instead of
207 employing linear trajectories, power transformations are supported in chroma and
208 luminance via a vector \code{power} (or separate \code{p1} and \code{p2}). If
209 \code{power[2]} (or \code{p2}) for the luminance trajectory is missing, it defaults
210 to \code{power[1]}/\code{p1} from the chroma trajectory.
211
212 \code{diverging_hcl} codes the underlying numeric values by a triangular luminance
213 sequence with different hues in the left and in the right arm of the palette. Thus,
214 it can be seen as a combination of two sequential palettes with some restrictions:
215 (a) a single hue is used for each arm of the palette, (b) chroma and luminance trajectory
216 are balanced between the two arms, (c) the neutral central value has zero chroma.
217 To specify such a palette a vector of two hues \code{h} (or equivalently \code{h1}
218 and \code{h2}), either a single chroma value \code{c} (or \code{c1}) or a vector
219 of two chroma values \code{c} (or \code{c1} and \code{cmax}), a vector of two
220 luminances \code{l} (or \code{l1} and \code{l2}), and power parameter(s) \code{power}
221 (or \code{p1} and \code{p2}) are used. For more flexible diverging palettes without
222 the restrictrictions above (and consequently more parameters)
223 \code{\link{divergingx_hcl}} is available. For backward compatibility, \code{diverge_hcl}
224 is a copy of \code{diverging_hcl}.
225
226 To facilitate using HCL-based palettes a wide range of example palettes are
227 provided in the package and can be specified by a name instead of a set of
228 parameters/coordinates. The examples have been taken from the literature and many
229 approximate color palettes from other software packages such as ColorBrewer.org
230 (\pkg{RColorBrewer}), CARTO colors (\pkg{rcartocolor}), \pkg{scico}, or \pkg{virids}. The
231 function \code{hcl_palettes} can be used to query the available pre-specified palettes. It
232 comes with a \code{print} method (listing names and types), a \code{summary} method
233 (additionally listing the underlying parameters/coordinates), and a \code{plot}
234 method that creates a \code{\link{swatchplot}} with suitable labels.
235
236 The named HCL-based palettes have also been included in the \code{\link[grDevices]{hcl.colors}}
237 function in base R in order to facilitate adoption in practice and in R packages.
238 However, in \code{hcl.colors} it is only possible to obtain \code{n} colors from the
239 given palette without the possibility for further modifications.
240 }
241 \examples{
242 ## overview of all _named_ HCL palettes
243 hcl_palettes()
244
245 ## visualize
246 hcl_palettes("qualitative", plot = TRUE)
247 hcl_palettes("sequential (single-hue)", n = 7, plot = TRUE)
248 hcl_palettes("sequential (multi-hue)", n = 7, plot = TRUE)
249 hcl_palettes("diverging", n = 7, plot = TRUE)
250
251 ## inspect a specific palette
252 ## (upper-case, spaces, etc. are ignored for matching)
253 hcl_palettes(palette = "Dark 2")
254 hcl_palettes(palette = "dark2")
255
256 ## set up actual colors
257 qualitative_hcl(4, h = c(0, 288), c = 50, l = 60) ## by hand
258 qualitative_hcl(4, palette = "dark2") ## by name
259 qualitative_hcl(4, palette = "dark2", c = 80) ## by name plus modification
260
261 ## how HCL palettes are constructed:
262 ## by varying the perceptual properties via hue/chroma/luminance
263 swatchplot(
264 "Hue" = sequential_hcl(5, h = c(0, 300), c = c(60, 60), l = 65),
265 "Chroma" = sequential_hcl(5, h = 0, c = c(100, 0), l = 65, rev = TRUE, power = 1),
266 "Luminance" = sequential_hcl(5, h = 260, c = c(25, 25), l = c(25, 90), rev = TRUE, power = 1),
267 off = 0
268 )
269
270 ## for qualitative palettes luminance and chroma are fixed, varying only hue
271 hclplot(qualitative_hcl(9, c = 50, l = 70))
272
273 ## single-hue sequential palette (h = 260) with linear vs. power-transformed trajectory
274 hclplot(sequential_hcl(7, h = 260, c = 80, l = c(35, 95), power = 1))
275 hclplot(sequential_hcl(7, h = 260, c = 80, l = c(35, 95), power = 1.5))
276
277 ## advanced single-hue sequential palette with triangular chroma trajectory
278 ## (piecewise linear vs. power-transformed)
279 hclplot(sequential_hcl(7, h = 245, c = c(40, 75, 0), l = c(30, 95), power = 1))
280 hclplot(sequential_hcl(7, h = 245, c = c(40, 75, 0), l = c(30, 95), power = c(0.8, 1.4)))
281
282 ## multi-hue sequential palette with small hue range and triangular chroma vs.
283 ## large hue range and linear chroma trajectory
284 hclplot(sequential_hcl(7, h = c(260, 220), c = c(50, 75, 0), l = c(30, 95), power = 1))
285 hclplot(sequential_hcl(7, h = c(260, 60), c = 60, l = c(40, 95), power = 1))
286
287 ## balanced diverging palette constructed from two simple single-hue sequential
288 ## palettes (for hues 260/blue and 0/red)
289 hclplot(diverging_hcl(7, h = c(260, 0), c = 80, l = c(35, 95), power = 1))
290
291 ## to register a particular adapted palette for re-use in the same session
292 ## with a new name the register=... argument can be used once, e.g.,
293 diverging_hcl(7, palette = "Tropic", h2 = 0, register = "mytropic")
294
295 ## subsequently palete="mytropic" is available in diverging_hcl() and the diverging
296 ## ggplot2 scales such as scale_color_continuous_diverging() etc.
297 demoplot(diverging_hcl(11, "mytropic"), type = "map")
298
299 ## to register this palette in all R sessions you could place the following
300 ## code in a startup script (e.g., .Rprofile):
301 ## colorspace::diverging_hcl(7, palette = "Tropic", h2 = 0, register = "mytropic")
302
303 }
304 \references{
305 Zeileis A, Hornik K, Murrell P (2009). Escaping RGBland:
306 Selecting Colors for Statistical Graphics. \emph{Computational Statistics &
307 Data Analysis}, \bold{53}, 3259--3270.
308 \doi{10.1016/j.csda.2008.11.033}
309 Preprint available from
310 \url{https://www.zeileis.org/papers/Zeileis+Hornik+Murrell-2009.pdf}.
311
312 Stauffer R, Mayr GJ, Dabernig M, Zeileis A (2015). Somewhere Over the
313 Rainbow: How to Make Effective Use of Colors in Meteorological
314 Visualizations. \emph{Bulletin of the American Meteorological Society},
315 \bold{96}(2), 203--216.
316 \doi{10.1175/BAMS-D-13-00155.1}
317
318 Zeileis A, Fisher JC, Hornik K, Ihaka R, McWhite CD, Murrell P, Stauffer R, Wilke CO (2020).
319 \dQuote{colorspace: A Toolbox for Manipulating and Assessing Colors and Palettes.}
320 \emph{Journal of Statistical Software}, \bold{96}(1), 1--49. \doi{10.18637/jss.v096.i01}
321
322 Zeileis A, Murrell P (2023).
323 \dQuote{Coloring in R's Blind Spot.}
324 \emph{The R Journal}, \bold{15}(3), 240--256. \doi{10.32614/RJ-2023-071}
325 }
326 \seealso{
327 \code{\link{divergingx_hcl}} \code{\link[grDevices]{hcl.colors}}
328 }
329 \keyword{color}