/
legend.R
408 lines (380 loc) · 17.4 KB
/
legend.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# legend_timecycle ----
#' Make an HCL legend for a cyclical sequence of distributions
#'
#' @description This function creates a legend to accompany a map describing
#' a cyclical sequence of distributions.
#'
#' @param palette data frame containing a color palette generated by
#' [palette_timecycle].
#' @param specificity logical indicating whether to visualize intensity
#' and layer information for three specificity values (i.e., 0, 50, 100) or
#' for a single specificity value (i.e., 100). Typically, a single specificity
#' value is appropriate for [map_multiples] visualizations.
#' @param origin_label character vector with a single element to be used as the
#' label at the 12 o'clock position of the legend wheel.
#' @param label_i character vector with a single element describing the meaning
#' of intensity values.
#' @param label_l character vector with a single element describing the meaning
#' of layer values.
#' @param label_s character vector with three elements describing differences
#' in the meaning of three specificity values (i.e., 0, 50, 100).
#' @param return_df logical indicating whether to return the legend as a
#' `ggplot2` object or return a data frame containing the necessary data to
#' build the legend.
#'
#' @return A `ggplot2` plot object of the legend. Alternatively,
#' `return_df = TRUE` will return a data frame containing the data needed
#' to build the legend. The data frame columns are:
#' - `specificity`: the degree to which intensity values are unevenly
#' distributed across layers; mapped to chroma.
#' - `layer_id`: integer identifying the layer containing the maximum
#' intensity value; mapped to hue.
#' - `color`: the hexadecimal color associated with the given layer and
#' specificity values.
#' - `intensity`: maximum cell value across layers divided by the maximum
#' value across all layers and cells; mapped to alpha level.
#'
#' @family legend
#' @seealso [legend_timeline] for linear sequences of distributions and
#' [legend_set] for distributions of distinct groups.
#' @export
#' @examples
#' # load field sparrow data
#' data(fiespa_occ)
#'
#' # generate hcl palette
#' pal <- palette_timecycle(fiespa_occ)
#'
#' # create legend for palette
#' legend_timecycle(pal)
legend_timecycle <- function(palette, specificity = TRUE, origin_label = NULL,
label_i = "Maximum\nintensity",
label_l = "Layer",
label_s = c("Low specificity",
"Moderate specificity",
"High specificity"),
return_df = FALSE) {
if (!is.data.frame(palette) || !inherits(palette, "palette_timecycle")) {
stop("palette must be a color palette generated by palette_timecycle().")
}
if (!all(c("specificity", "layer_id", "color") %in% names(palette)) ||
!all(c(0, 50, 100) %in% palette[["specificity"]])) {
stop("Improperly formatted color palette, try using palette_timecycle().")
}
stopifnot(is.logical(specificity), length(specificity) == 1)
if (!is.null(origin_label)) {
stopifnot(is.character(origin_label), length(origin_label) == 1)
}
stopifnot(is.character(label_i), length(label_i) == 1)
stopifnot(is.character(label_l), length(label_l) == 1)
stopifnot(is.character(label_s), length(label_s) == 3)
names(label_s) <- c(0, 50, 100)
stopifnot(is.logical(return_df), length(return_df) == 1)
# whether to show 3 or 1 wheels
if (isTRUE(specificity)) {
wheel <- palette[palette$specificity %in% c(0, 50, 100), ]
} else {
wheel <- palette[palette$specificity == 100, ]
label_s <- c("100" = "")
}
# add intensity values
wheel <- merge(wheel, data.frame(intensity = seq(0, 1, 0.05)))
wheel <- wheel[order(wheel[["specificity"]], wheel[["layer_id"]],
wheel[["intensity"]]), ]
row.names(wheel) <- NULL
class(wheel) <- c("legend_timecycle", "data.frame")
# return data without plotting if requested
if (isTRUE(return_df)) {
return(wheel)
}
# make named vector of colors
tile_colors <- sort(unique(wheel$color))
names(tile_colors) <- tile_colors
# describe ggplot
p <- ggplot2::ggplot(data = wheel) +
ggplot2::aes_(x = ~ layer_id, y = ~ intensity, fill = ~ color,
alpha = ~ intensity) +
ggplot2::facet_wrap(~ specificity, nrow = 1,
labeller = ggplot2::labeller(specificity = label_s)) +
ggplot2::geom_tile(size = 0) +
ggplot2::scale_fill_manual(values = tile_colors) +
ggplot2::scale_alpha_continuous(range = c(0, 1)) +
ggplot2::geom_linerange(x = 0.5, ymin = 0, ymax = 1, size = 0.5,
linetype = 3) +
ggplot2::theme(plot.background = ggplot2::element_rect(fill = "white"),
panel.background = ggplot2::element_rect(fill = "white"),
panel.grid = ggplot2::element_blank(),
panel.spacing = ggplot2::unit(0, "lines"),
strip.background = ggplot2::element_blank(),
strip.text = ggplot2::element_text(size = 11),
axis.ticks = ggplot2::element_blank(),
axis.text = ggplot2::element_blank()) +
ggplot2::xlab(label_l) +
ggplot2::ylab(label_i) +
ggplot2::guides(fill = FALSE, alpha = FALSE) +
ggplot2::coord_polar(theta = "x", start = 0)
# add origin label
if (!is.null(origin_label)) {
p <- p +
ggplot2::geom_text(ggplot2::aes(x = 0.5, y = 1, label = origin_label),
fontface = "plain", vjust = -1, color = "gray60", size = 3)
}
return(p)
}
# legend_timeline ----
#' Make an HCL legend for a linear sequence of distributions
#'
#' @description This function creates a legend to accompany a map describing
#' a linear sequence of distributions.
#'
#' @param palette data frame containing a color palette generated by
#' [palette_timeline].
#' @param specificity logical indicating whether to visualize intensity
#' and layer information for three specificity values (i.e., 0, 50, 100) or
#' for a single specificity value (i.e., 100). Typically, a single specificity
#' value is appropriate for [map_multiples] visualizations.
#' @param time_labels character vector with two elements to be used as labels
#' for the start and end points of the time axis (i.e. x-axis) in the legend.
#' @param label_i character vector with a single element describing the meaning
#' of intensity values.
#' @param label_l character vector with a single element describing the meaning
#' of layer values.
#' @param label_s character vector with three elements describing differences
#' in the meaning of specificity across three legend wheels.
#' @param axis_i character vector with two elements describing the meaning of
#' low and high intensity values.
#' @param return_df logical indicating whether to return the legend as a
#' `ggplot2` object or return a data frame containing the necessary data to
#' build the legend.
#'
#' @return A `ggplot2` plot object of the legend. Alternatively,
#' `return_df = TRUE` will return a data frame containing a data frame
#' containing the data needed to build the legend. The data frame columns are:
#' - `specificity`: the degree to which intensity values are unevenly
#' distributed across layers; mapped to chroma.
#' - `layer_id`: integer identifying the layer containing the maximum
#' intensity value; mapped to hue.
#' - `color`: the hexadecimal color associated with the given layer and
#' specificity values.
#' - `intensity`: maximum cell value across layers divided by the maximum
#' value across all layers and cells; mapped to alpha level.
#'
#' @family legend
#' @seealso [legend_timecycle] for cyclical sequences of distributions and
#' [legend_set] for distributions of distinct groups.
#' @export
#' @examples
#' # load fisher data
#' data(fisher_ud)
#'
#' # generate hcl palette
#' pal <- palette_timeline(fisher_ud)
#'
#' # create legend for palette
#' legend_timeline(pal)
legend_timeline <- function(palette, specificity = TRUE, time_labels = NULL,
label_i = "Maximum\nintensity",
label_l = "Layer",
label_s = c("Low specificity",
"Moderate specificity",
"High specificity"),
axis_i = c("low", "high"),
return_df = FALSE) {
if (!is.data.frame(palette) || !inherits(palette, "palette_timeline")) {
stop("palette must be a color palette generated by palette_timeline().")
}
if (!all(c("specificity", "layer_id", "color") %in% names(palette)) ||
!all(c(0, 50, 100) %in% palette[["specificity"]])) {
stop("Improperly formatted color palette, try using palette_timeline().")
}
stopifnot(is.logical(specificity), length(specificity) == 1)
if (!is.null(time_labels)) {
stopifnot(is.character(time_labels), length(time_labels) == 2)
}
stopifnot(is.character(label_i), length(label_i) == 1)
stopifnot(is.character(label_l), length(label_l) == 1)
stopifnot(is.character(label_s), length(label_s) == 3)
names(label_s) <- c(0, 50, 100)
stopifnot(is.character(axis_i), length(axis_i) == 2)
stopifnot(is.logical(return_df), length(return_df) == 1)
# whether to show 3 or 1 timelines
if (isTRUE(specificity)) {
wheel <- palette[palette$specificity %in% c(0, 50, 100), ]
} else {
wheel <- palette[palette$specificity == 100, ]
label_s <- c("100" = "")
}
# add intensity
wheel <- merge(wheel, data.frame(intensity = seq(0, 1, 0.02)))
wheel <- wheel[order(wheel[["specificity"]], wheel[["layer_id"]],
wheel[["intensity"]]), ]
class(wheel) <- c("legend_timeline", "data.frame")
# return data without plotting if requested
if (isTRUE(return_df)) {
return(wheel)
}
# make specificity labels for legend
wheel$specificity <- factor(wheel$specificity, levels = c(100, 50, 0))
# make named vector of colors
tile_colors <- sort(unique(wheel$color))
names(tile_colors) <- tile_colors
# describe ggplot
p <- ggplot2::ggplot(data = wheel) +
ggplot2::aes_(x = ~ layer_id, y = ~ intensity, fill = ~ color,
alpha = ~ intensity) +
ggplot2::facet_wrap(~ specificity, ncol = 1,
labeller = ggplot2::labeller(specificity = label_s)) +
ggplot2::geom_tile(size = 0) +
ggplot2::scale_fill_manual(values = tile_colors) +
ggplot2::scale_alpha_continuous(range = c(0, 1)) +
ggplot2::theme(plot.background = ggplot2::element_rect(fill = "white"),
panel.background = ggplot2::element_blank(),
panel.grid = ggplot2::element_blank(),
panel.spacing = ggplot2::unit(0, "lines"),
aspect.ratio = 0.4,
strip.background = ggplot2::element_blank(),
strip.text = ggplot2::element_text(size = 11),
axis.ticks = ggplot2::element_blank(),
axis.text.x = ggplot2::element_text(size = 9, hjust = 0.5,
vjust = 0.5),
axis.text.y = ggplot2::element_text(size = 9, hjust = 0.5,
vjust = 0.5,
angle = 90)) +
ggplot2::scale_x_continuous(breaks = c(1, max(palette[["layer_id"]])),
labels = time_labels) +
ggplot2::scale_y_continuous(breaks = c(0.15, 0.85),
labels = axis_i) +
ggplot2::xlab(label_l) +
ggplot2::ylab(label_i) +
ggplot2::guides(fill = FALSE, alpha = FALSE) +
ggplot2::coord_fixed(expand = F)
return(p)
}
# legend_set ----
#' Make an HCL legend for an unordered set of distributions
#'
#' @description This function creates a legend to accompany a map describing
#' an unordered set of distributions.
#'
#' @param palette data frame containing a color palette generated by
#' [palette_set].
#' @param specificity logical indicating whether to visualize intensity and
#' layer information for the full range of potential specificity values (i.e.,
#' 0-100) or for a single specificity value (i.e., 100). Typically, a single
#' specificity value is appropriate for [map_multiples] visualizations.
#' @param group_labels (axis_l) character vector with labels for each distribution.
#' @param label_i character vector with a single element describing the meaning
#' of specificity.
#' @param label_s character vector with a single element describing the meaning
#' of intensity values.
#' @param axis_i character vector with two elements describing the meaning of
#' low and high intensity values.
#' @param axis_s character vector with two elements describing the meaning of
#' low and high specificity values.
#' @param return_df logical indicating whether to return the legend as a
#' `ggplot2` object or return a data frame containing the necessary data to
#' build the legend.
#'
#' @return A `ggplot2` plot object of the legend. Alternatively,
#' `return_df = TRUE` will return a data frame containing a data frame
#' containing the data needed to build the legend. The data frame columns are:
#' - `specificity`: the degree to which intensity values are unevenly
#' distributed across layers; mapped to chroma.
#' - `layer_id`: integer identifying the layer containing the maximum
#' intensity value; mapped to hue.
#' - `color`: the hexadecimal color associated with the given layer and
#' specificity values.
#' - `intensity`: maximum cell value across layers divided by the maximum
#' value across all layers and cells; mapped to alpha level.
#'
#' @family legend
#' @seealso [legend_timecycle] for cyclical sequences of distributions and
#' [legend_timeline] for linear sequences of distributions.
#' @export
#' @examples
#' # load elephant data
#' data(elephant_ud)
#'
#' # generate hcl palette
#' pal <- palette_set(elephant_ud)
#'
#' # create legend for palettes
#' legend_set(pal)
legend_set <- function(palette, specificity = TRUE, group_labels = NULL,
label_i = "Maximum\nintensity",
label_s = "Specificity",
axis_i = c("low", "high"),
axis_s = c("low", "high"),
return_df = FALSE) {
if (!is.data.frame(palette) || !inherits(palette, "palette_set")) {
stop("palette must be a color palette generated by palette_set().")
}
if (!all(c("specificity", "layer_id", "color") %in% names(palette)) ||
!all(c(0, 50, 100) %in% palette[["specificity"]])) {
stop("Improperly formatted color palette, try using palette_set().")
}
stopifnot(is.logical(specificity), length(specificity) == 1)
if (!is.null(group_labels)) {
stopifnot(is.character(group_labels),
length(group_labels) == length(unique(palette[["layer_id"]])))
}
stopifnot(is.character(label_i), length(label_i) == 1)
stopifnot(is.character(label_s), length(label_s) == 1)
stopifnot(is.character(axis_i), length(axis_i) == 2)
stopifnot(is.character(axis_s), length(axis_s) == 2)
stopifnot(is.logical(return_df), length(return_df) == 1)
# whether to show multiple specificity values and labels
if (isTRUE(specificity)) {
wheel <- palette
} else {
wheel <- palette[palette$specificity == 100, ]
label_s <- ""
}
# add intensity
wheel <- merge(wheel, data.frame(intensity = seq(0, 1, 0.01)))
wheel <- wheel[order(wheel[["specificity"]], wheel[["layer_id"]],
wheel[["intensity"]]), ]
row.names(wheel) <- NULL
class(wheel) <- c("legend_set", "data.frame")
# return data without plotting if requested
if (isTRUE(return_df)) {
return(wheel)
}
# make labels for groups
unique_ids <- sort(unique(palette[["layer_id"]]))
if (is.null(group_labels)) {
group_labels <- unique_ids
names(group_labels) <- unique_ids
} else {
names(group_labels) <- unique_ids
}
# make named vector of colors
tile_colors <- sort(unique(wheel$color))
names(tile_colors) <- tile_colors
# describe ggplot
p <- ggplot2::ggplot(data = wheel) +
ggplot2::aes_(x = ~ specificity, y = ~ intensity, fill = ~ color,
alpha = ~ intensity) +
ggplot2::facet_wrap(~ layer_id, ncol = length(unique_ids),
labeller = ggplot2::labeller(layer_id = group_labels)) +
ggplot2::geom_tile(size = 0) +
ggplot2::scale_fill_manual(values = tile_colors) +
ggplot2::scale_alpha_continuous(range = c(0, 1)) +
ggplot2::theme(plot.background = ggplot2::element_rect(fill = "white"),
panel.background = ggplot2::element_rect(fill = "white"),
panel.grid = ggplot2::element_blank(),
panel.spacing = ggplot2::unit(0.5, "lines"),
aspect.ratio = 1,
strip.background = ggplot2::element_blank(),
strip.text = ggplot2::element_text(size = 11),
axis.ticks = ggplot2::element_blank(),
axis.text.x = ggplot2::element_text(size = 9, hjust = 0.5, vjust = 0.5),
axis.text.y = ggplot2::element_text(size = 9, hjust = 0.5, vjust = 0.5, angle = 90)) +
ggplot2::scale_x_continuous(breaks = c(15, 85), labels = axis_s) +
ggplot2::scale_y_continuous(breaks = c(0.15, 0.85), labels = axis_i) +
ggplot2::xlab(label_s) +
ggplot2::ylab(label_i) +
ggplot2::guides(fill = FALSE, alpha = FALSE, color = FALSE) +
ggplot2::coord_fixed(expand = F)
return(p)
}