/
scales_whitebox.R
353 lines (316 loc) · 9.38 KB
/
scales_whitebox.R
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#' Gradient scales from **WhiteboxTools** color schemes
#'
#' @description
#'
#' Implementation of the gradient palettes provided by
#' [WhiteboxTools](https://github.com/jblindsay/whitebox-tools). Three
#' scales are provided:
#' - `scale_*_whitebox_d()`: For discrete values.
#' - `scale_*_whitebox_c()`: For continuous values.
#' - `scale_*_whitebox_b()`: For binning continuous values.
#'
#' Additionally, a color palette `whitebox.colors()` is provided. See also
#' [grDevices::terrain.colors()] for details.
#'
#'
#' Additional parameters `...` would be passed on to:
#' - Discrete values: [ggplot2::discrete_scale()].
#' - Continuous values: [ggplot2::continuous_scale()].
#' - Binned continuous values: [ggplot2::binned_scale()].
#'
#' **Note that** \CRANpkg{tidyterra} just documents a selection of these
#' additional parameters, check the \CRANpkg{ggplot2} functions listed above to
#' see the full range of parameters accepted by these scales.
#'
#' @export
#'
#' @name scale_whitebox
#'
#' @inheritDotParams ggplot2::discrete_scale breaks:drop
#' @inheritDotParams ggplot2::continuous_scale breaks:labels
#' @inheritDotParams ggplot2::binned_scale breaks:limits nice.breaks
#' @inheritParams scale_cross_blended
#' @param palette A valid palette name. The name is matched to the list of
#' available palettes, ignoring upper vs. lower case. Values available are:
#'
#' ```{r, echo=FALSE, results="asis", message = FALSE, warning = FALSE}
#'
#' suppressPackageStartupMessages(library(dplyr))
#' whitebox_coltab %>%
#' pull(pal) %>%
#' unique() %>%
#' paste0('`"', ., '"`', collapse = ", ") %>%
#' paste0(".") %>%
#' cat()
#'
#'
#' ```
#'
#' @seealso [terra::plot()], [ggplot2::scale_fill_viridis_c()]
#'
#' See also \CRANpkg{ggplot2} docs on additional `...` parameters:
#'
#' - `scale_*_terrain_d()`: For discrete values.
#' - `scale_*_terrain_c()`: For continuous values.
#' - `scale_*_terrain_b()`: For binning continuous values.
#'
#' @return
#' The corresponding \CRANpkg{ggplot2} layer with the values applied to the
#' `fill/colour` aesthetics.
#'
#' @family gradients
#'
#' @source <https://github.com/jblindsay/whitebox-tools>, under
#' MIT License. Copyright (c) 2017-2021 John Lindsay.
#'
#' @examples
#' \donttest{
#' filepath <- system.file("extdata/volcano2.tif", package = "tidyterra")
#'
#' library(terra)
#' volcano2_rast <- rast(filepath)
#'
#' # Palette
#' plot(volcano2_rast, col = whitebox.colors(100))
#'
#' library(ggplot2)
#' ggplot() +
#' geom_spatraster(data = volcano2_rast) +
#' scale_fill_whitebox_c()
#'
#' # Binned
#' ggplot() +
#' geom_spatraster(data = volcano2_rast) +
#' scale_fill_whitebox_b(breaks = seq(70, 200, 10), palette = "atlas")
#'
#' # With discrete values
#' factor <- volcano2_rast %>% mutate(cats = cut(elevation,
#' breaks = c(100, 120, 130, 150, 170, 200),
#' labels = c(
#' "Very Low", "Low", "Average", "High",
#' "Very High"
#' )
#' ))
#'
#'
#' ggplot() +
#' geom_spatraster(data = factor, aes(fill = cats)) +
#' scale_fill_whitebox_d(na.value = "gray10", palette = "soft")
#' }
scale_fill_whitebox_d <- function(palette = "high_relief", ...,
alpha = 1, direction = 1,
na.translate = FALSE,
drop = TRUE) {
if (alpha < 0 || alpha > 1) {
cli::cli_abort("{.arg alpha} {.field {alpha}} not in {.field [0,1]}")
}
if (!direction %in% c(-1, 1)) {
cli::cli_abort("{.arg direction} must be {.field 1} or {.field -1}")
}
ggplot2::discrete_scale(
aesthetics = "fill",
palette = whitebox_pal(
alpha = alpha,
direction = direction,
palette = palette
),
na.translate = na.translate,
drop = drop,
...
)
}
#' @export
#' @rdname scale_whitebox
scale_colour_whitebox_d <- function(palette = "high_relief", ...,
alpha = 1, direction = 1,
na.translate = FALSE,
drop = TRUE) {
if (alpha < 0 || alpha > 1) {
cli::cli_abort("{.arg alpha} {.field {alpha}} not in {.field [0,1]}")
}
if (!direction %in% c(-1, 1)) {
cli::cli_abort("{.arg direction} must be {.field 1} or {.field -1}")
}
ggplot2::discrete_scale(
aesthetics = "colour",
palette = whitebox_pal(
alpha = alpha,
direction = direction,
palette = palette
),
na.translate = na.translate,
drop = drop,
...
)
}
#' @export
#' @rdname scale_whitebox
scale_fill_whitebox_c <- function(palette = "high_relief", ...,
alpha = 1, direction = 1,
na.value = "transparent",
guide = "colourbar") {
if (alpha < 0 || alpha > 1) {
cli::cli_abort("{.arg alpha} {.field {alpha}} not in {.field [0,1]}")
}
if (!direction %in% c(-1, 1)) {
cli::cli_abort("{.arg direction} must be {.field 1} or {.field -1}")
}
length_pal <- nrow(extract_pal(whitebox_coltab, palette = palette))
ggplot2::continuous_scale(
aesthetics = "fill",
palette = scales::gradient_n_pal(whitebox_pal(
alpha = alpha,
direction = direction,
palette = palette
)(length_pal)),
na.value = na.value,
guide = guide,
...
)
}
#' @export
#' @rdname scale_whitebox
scale_colour_whitebox_c <- function(palette = "high_relief", ...,
alpha = 1, direction = 1,
na.value = "transparent",
guide = "colourbar") {
if (alpha < 0 || alpha > 1) {
cli::cli_abort("{.arg alpha} {.field {alpha}} not in {.field [0,1]}")
}
if (!direction %in% c(-1, 1)) {
cli::cli_abort("{.arg direction} must be {.field 1} or {.field -1}")
}
length_pal <- nrow(extract_pal(whitebox_coltab, palette = palette))
ggplot2::continuous_scale(
aesthetics = "colour",
palette = scales::gradient_n_pal(whitebox_pal(
alpha = alpha,
direction = direction,
palette = palette
)(length_pal)),
na.value = na.value,
guide = guide,
...
)
}
#' @export
#' @rdname scale_whitebox
scale_fill_whitebox_b <- function(palette = "high_relief", ...,
alpha = 1, direction = 1,
na.value = "transparent",
guide = "coloursteps") {
if (alpha < 0 || alpha > 1) {
cli::cli_abort("{.arg alpha} {.field {alpha}} not in {.field [0,1]}")
}
if (!direction %in% c(-1, 1)) {
cli::cli_abort("{.arg direction} must be {.field 1} or {.field -1}")
}
length_pal <- nrow(extract_pal(whitebox_coltab, palette = palette))
ggplot2::binned_scale(
aesthetics = "fill",
palette = scales::gradient_n_pal(whitebox_pal(
alpha = alpha,
direction = direction,
palette = palette
)(length_pal)),
na.value = na.value,
guide = guide,
...
)
}
#' @export
#' @rdname scale_whitebox
scale_colour_whitebox_b <- function(palette = "high_relief", ...,
alpha = 1, direction = 1,
na.value = "transparent",
guide = "coloursteps") {
if (alpha < 0 || alpha > 1) {
cli::cli_abort("{.arg alpha} {.field {alpha}} not in {.field [0,1]}")
}
if (!direction %in% c(-1, 1)) {
cli::cli_abort("{.arg direction} must be {.field 1} or {.field -1}")
}
length_pal <- nrow(extract_pal(whitebox_coltab, palette = palette))
ggplot2::binned_scale(
aesthetics = "colour",
palette = scales::gradient_n_pal(whitebox_pal(
alpha = alpha,
direction = direction,
palette = palette
)(length_pal)),
na.value = na.value,
guide = guide,
...
)
}
#' @export
#' @rdname scale_whitebox
#'
#' @inheritParams wiki.colors
#' @examples
#'
#' # Display all the whitebox palettes
#'
#' pals <- c(
#' "atlas", "high_relief", "arid", "soft", "muted", "purple",
#' "viridi", "gn_yl", "pi_y_g", "bl_yl_rd", "deep"
#' )
#'
#' # Helper fun for plotting
#'
#' ncols <- 128
#' rowcol <- grDevices::n2mfrow(length(pals))
#'
#' opar <- par(no.readonly = TRUE)
#' par(mfrow = rowcol, mar = rep(1, 4))
#'
#' for (i in pals) {
#' image(
#' x = seq(1, ncols), y = 1, z = as.matrix(seq(1, ncols)),
#' col = whitebox.colors(ncols, i), main = i,
#' ylab = "", xaxt = "n", yaxt = "n", bty = "n"
#' )
#' }
#' par(opar)
whitebox.colors <- function(n, palette = "high_relief",
alpha = 1, rev = FALSE) {
if ((n <- as.integer(n[1L])) > 0) {
paltab <- extract_pal(whitebox_coltab, palette = palette)
colors <- as.character(paltab$hex)
endcols <- tidyterra_ramp(colors, n, alpha, rev)
return(endcols)
} else {
character()
}
}
whitebox_pal <- function(alpha = 1, direction = 1, palette) {
# nocov start
function(n) {
pal <- whitebox.colors(n,
rev = direction != 1, alpha = alpha,
palette = palette
)
pal
}
# nocov end
}
extract_pal <- function(df, palette) {
palette <- tolower(palette)
if (!palette %in% df$pal) {
cli::cli_abort("{.arg palette} does not match any given palette")
}
df <- df[df$pal == palette, ]
return(df)
}
#' @export
#' @rdname scale_whitebox
#' @usage NULL
scale_color_whitebox_d <- scale_colour_whitebox_d
#' @export
#' @rdname scale_whitebox
#' @usage NULL
scale_color_whitebox_c <- scale_colour_whitebox_c
#' @export
#' @rdname scale_whitebox
#' @usage NULL
scale_color_whitebox_b <- scale_colour_whitebox_b