-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathlabels.R
405 lines (373 loc) · 12.9 KB
/
labels.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
#' Update axis/legend labels
#'
#' @param p plot to modify
#' @param labels named list of new labels
#' @keywords internal
#' @export
#' @examples
#' p <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
#' update_labels(p, list(x = "New x"))
#' update_labels(p, list(x = expression(x / y ^ 2)))
#' update_labels(p, list(x = "New x", y = "New Y"))
#' update_labels(p, list(colour = "Fail silently"))
update_labels <- function(p, labels) {
p <- plot_clone(p)
p$labels <- defaults(labels, p$labels)
p
}
# Called in `ggplot_build()` to set default labels not specified by user.
setup_plot_labels <- function(plot, layers, data) {
# Initiate empty labels
labels <- list()
# Find labels from every layer
for (i in seq_along(layers)) {
layer <- layers[[i]]
mapping <- layer$computed_mapping
if (inherits(mapping, "unlabelled_uneval")) {
next
}
mapping <- strip_stage(mapping)
mapping <- strip_dots(mapping, strip_pronoun = TRUE)
exclude <- names(layer$aes_params)
mapping <- mapping[setdiff(names(mapping), exclude)]
# Acquire default labels
mapping_default <- make_labels(mapping)
stat_default <- lapply(
make_labels(layer$stat$default_aes),
function(l) {
attr(l, "fallback") <- TRUE
l
}
)
default <- defaults(mapping_default, stat_default)
# Search for label attribute in symbolic mappings
symbolic <- vapply(
mapping, FUN.VALUE = logical(1),
function(x) is_quosure(x) && quo_is_symbol(x)
)
symbols <- intersect(names(mapping)[symbolic], names(data[[i]]))
attribs <- lapply(setNames(nm = symbols), function(x) {
attr(data[[i]][[x]], "label", exact = TRUE)
})
attribs <- attribs[lengths(attribs) > 0]
layer_labels <- defaults(attribs, default)
# Set label priority:
# 1. Existing labels that aren't fallback labels
# 2. The labels of this layer, including fallback labels
# 3. Existing fallback labels
current <- labels
fallbacks <- vapply(current, function(l) isTRUE(attr(l, "fallback")), logical(1))
labels <- defaults(current[!fallbacks], layer_labels)
if (any(fallbacks)) {
labels <- defaults(labels, current)
}
}
# Warn for spurious labels that don't have a mapping.
# Note: sometimes, 'x' and 'y' might not have a mapping, like in
# `geom_function()`. We can display these labels anyway, so we include them.
plot_labels <- plot$labels
known_labels <- c(names(labels), fn_fmls_names(labs), "x", "y")
extra_labels <- setdiff(names(plot_labels), known_labels)
if (length(extra_labels) > 0) {
extra_labels <- paste0(
"{.code ", extra_labels, " = \"", plot_labels[extra_labels], "\"}"
)
names(extra_labels) <- rep("*", length(extra_labels))
cli::cli_warn(c(
"Ignoring unknown labels:",
extra_labels
))
}
# User labels can be functions, so apply these to the default labels
plot_labels <- lapply(setNames(nm = names(plot_labels)), function(nm) {
label <- plot_labels[[nm]]
if (!is.function(label)) {
return(label)
}
label(labels[[nm]] %||% "")
})
dict <- plot_labels$dictionary
if (length(dict) > 0) {
labels <- lapply(labels, function(x) {
dict <- dict[names(dict) %in% x]
x[match(names(dict), x)] <- dict
x
})
}
defaults(plot_labels, labels)
}
#' Modify axis, legend, and plot labels
#'
#' Good labels are critical for making your plots accessible to a wider
#' audience. Always ensure the axis and legend labels display the full
#' variable name. Use the plot `title` and `subtitle` to explain the
#' main findings. It's common to use the `caption` to provide information
#' about the data source. `tag` can be used for adding identification tags
#' to differentiate between multiple plots.
#'
#' You can also set axis and legend labels in the individual scales (using
#' the first argument, the `name`). If you're changing other scale options, this
#' is recommended.
#'
#' If a plot already has a title, subtitle, caption, etc., and you want to
#' remove it, you can do so by setting the respective argument to `NULL`. For
#' example, if plot `p` has a subtitle, then `p + labs(subtitle = NULL)` will
#' remove the subtitle from the plot.
#'
#' @param label The title of the respective axis (for `xlab()` or `ylab()`) or
#' of the plot (for `ggtitle()`).
#' @param title The text for the title.
#' @param subtitle The text for the subtitle for the plot which will be
#' displayed below the title.
#' @param caption The text for the caption which will be displayed in the
#' bottom-right of the plot by default.
#' @param tag The text for the tag label which will be displayed at the
#' top-left of the plot by default.
#' @param dictionary A named character vector to serve as dictionary.
#' Automatically derived labels, such as those based on variables will
#' be matched with `names(dictionary)` and replaced by the matching
#' entry in `dictionary`.
#' @param alt,alt_insight Text used for the generation of alt-text for the plot.
#' See [get_alt_text] for examples. `alt` can also be a function that
#' takes the plot as input and returns text as output. `alt` also accepts
#' rlang [lambda][rlang::as_function()] function notation.
#' @param ... A list of new name-value pairs. The name should be an aesthetic.
#' @export
#'
#' @seealso
#' The `r link_book("plot and axis titles section", "annotations#sec-titles")`
#' @examples
#' p <- ggplot(mtcars, aes(mpg, wt, colour = cyl)) + geom_point()
#' p + labs(colour = "Cylinders")
#' p + labs(x = "New x label")
#'
#' # Set labels by variable name instead of aesthetic
#' p + labs(dict = c(
#' disp = "Displacment", # Not in use
#' cyl = "Number of cylinders",
#' mpg = "Miles per gallon",
#' wt = "Weight (1000 lbs)"
#' ))
#'
#' # The plot title appears at the top-left, with the subtitle
#' # display in smaller text underneath it
#' p + labs(title = "New plot title")
#' p + labs(title = "New plot title", subtitle = "A subtitle")
#'
#' # The caption appears in the bottom-right, and is often used for
#' # sources, notes or copyright
#' p + labs(caption = "(based on data from ...)")
#'
#' # The plot tag appears at the top-left, and is typically used
#' # for labelling a subplot with a letter.
#' p + labs(title = "title", tag = "A")
#'
#' # If you want to remove a label, set it to NULL.
#' p +
#' labs(title = "title") +
#' labs(title = NULL)
labs <- function(..., title = waiver(), subtitle = waiver(), caption = waiver(),
tag = waiver(), dictionary = waiver(), alt = waiver(),
alt_insight = waiver()) {
# .ignore_empty = "all" is needed to allow trailing commas, which is NOT a trailing comma for dots_list() as it's in ...
args <- dots_list(..., title = title, subtitle = subtitle, caption = caption,
tag = tag, alt = allow_lambda(alt), alt_insight = alt_insight,
dictionary = dictionary, .ignore_empty = "all")
is_waive <- vapply(args, is.waiver, logical(1))
args <- args[!is_waive]
# remove duplicated arguments
args <- args[!duplicated(names(args))]
args <- rename_aes(args)
structure(args, class = c("labels", "gg"))
}
#' @rdname labs
#' @export
xlab <- function(label) {
labs(x = label)
}
#' @rdname labs
#' @export
ylab <- function(label) {
labs(y = label)
}
#' @rdname labs
#' @export
ggtitle <- function(label, subtitle = waiver()) {
labs(title = label, subtitle = subtitle)
}
#' @rdname labs
#' @export
#' @param plot A ggplot object
#' @description
#' `get_labs()` retrieves completed labels from a plot.
get_labs <- function(plot = get_last_plot()) {
plot <- ggplot_build(plot)
labs <- plot$plot$labels
xy_labs <- rename(
c(x = plot$layout$resolve_label(plot$layout$panel_scales_x[[1]], labs),
y = plot$layout$resolve_label(plot$layout$panel_scales_y[[1]], labs)),
c(x.primary = "x", x.secondary = "x.sec",
y.primary = "y", y.secondary = "y.sec")
)
labs <- defaults(xy_labs, labs)
guides <- plot$plot$guides
if (length(guides$aesthetics) == 0) {
return(labs)
}
for (aes in guides$aesthetics) {
param <- guides$get_params(aes)
aes <- param$aesthetic # Can have length > 1 when guide was merged
title <- vec_set_names(rep(list(param$title), length(aes)), aes)
labs <- defaults(title, labs)
}
labs
}
#' Extract alt text from a plot
#'
#' This function returns a text that can be used as alt-text in webpages etc.
#' Currently it will use the `alt` label, added with `+ labs(alt = <...>)`, or
#' a return an empty string, but in the future it might try to generate an alt
#' text from the information stored in the plot.
#'
#' @param p a ggplot object
#' @inheritParams rlang::args_dots_used
#'
#' @return A text string
#'
#' @export
#' @aliases alt_text
#'
#' @examples
#' p <- ggplot(mpg, aes(displ, hwy)) +
#' geom_point()
#'
#' # Returns an empty string
#' get_alt_text(p)
#'
#' # A user provided alt text
#' p <- p + labs(
#' alt = paste("A scatterplot showing the negative correlation between engine",
#' "displacement as a function of highway miles per gallon")
#' )
#'
#' get_alt_text(p)
#'
get_alt_text <- function(p, ...) {
warn_dots_used()
UseMethod("get_alt_text")
}
#' @export
get_alt_text.ggplot <- function(p, ...) {
alt <- p$labels[["alt"]] %||% ""
if (!is.function(alt)) {
return(alt)
}
p$labels[["alt"]] <- NULL
build <- ggplot_build(p)
build$plot$labels[["alt"]] <- alt
get_alt_text(build)
}
#' @export
get_alt_text.ggplot_built <- function(p, ...) {
alt <- p$plot$labels[["alt"]] %||% ""
p$plot$labels[["alt"]] <- NULL
if (is.function(alt)) alt(p$plot) else alt
}
#' @export
get_alt_text.gtable <- function(p, ...) {
attr(p, "alt-label") %||% ""
}
#' Generate an alt text from a plot
#'
#' This function returns a text that can be used as alt-text in webpages etc.
#' It will synthesize one from the information in the plot itself, but you can
#' add a conclusion to the synthesized text using `+ labs(alt_insight = <...>)`.
#'
#' There is no way an automatically generated description can compete with one
#' written by a human with knowledge of what the plot shows and in which
#' context. We urge users to write their own alt text if at all possible.
#' Guidance to how an effective alt-text is written can be found in
#' [Writing Alt Text for Data Visualization](https://medium.com/nightingale/writing-alt-text-for-data-visualization-2a218ef43f81)
#' and [Effective Practices for Description of Science Content within Digital Talking Books](https://www.wgbh.org/foundation/ncam/guidelines/effective-practices-for-description-of-science-content-within-digital-talking-books)
#'
#' @param p a ggplot object
#'
#' @return A text string
#'
#' @noRd
#'
#' @examples
#' p <- ggplot(mpg, aes(displ, hwy)) +
#' geom_point()
#'
#' get_alt_text(p)
#'
#' p <- p + ggtitle("The relationship between displacement and yield in cars")
#' get_alt_text(p)
#'
#' # It will use scale information if available
#' p <- p + scale_x_continuous("highway miles per gallon")
#' get_alt_text(p)
#'
#' # Add a short description of the main conclusion of the plot
#' p <- p + labs(alt_insight = "The higher the yield, the lower the displacement")
#' get_alt_text(p)
#'
#' # A user provided alt text takes precedence
#' p <- p + labs(
#' alt = paste("A scatterplot showing the negative correlation between engine",
#' "displacement as a function of highway miles per gallon")
#' )
#'
#' get_alt_text(p)
#'
generate_alt_text <- function(p) {
# Combine titles
if (!is.null(p$labels$title %||% p$labels$subtitle)) {
title <- sub("\\.?$", "", c(p$labels$title, p$labels$subtitle))
if (length(title) == 2) {
title <- paste0(title[1], ": ", title[2])
}
title <- paste0(title, ". ")
title <- safe_string(title)
} else {
title <- ""
}
# Get axes descriptions
axes <- paste0(" showing ", scale_description(p, "x"), " and ", scale_description(p, "y"))
axes <- safe_string(axes)
# Get layer types
layers <- vapply(p$layers, function(l) snake_class(l$geom), character(1))
layers <- sub("_", " ", sub("^geom_", "", unique0(layers)))
if (length(layers) == 1) {
layers <- paste0(" using a ", layers, " layer")
} else {
layers <- paste0(" using ", oxford_comma(layers), " layers")
}
layers <- safe_string(layers)
# Combine
alt <- paste0(title, "A plot", axes, layers, ".")
if (!is.null(p$labels$alt_insight)) {
alt <- paste0(alt, " ", p$labels$alt_insight)
}
as.character(alt)
}
safe_string <- function(string) {
if (length(string) == 0) "" else string
}
scale_description <- function(p, name) {
scale <- p$scales$get_scales(name)
if (is.null(scale)) {
lab <- p$labels[[name]]
type <- "the"
} else {
lab <- scale$make_title(scale$name %|W|% p$labels[[name]])
type <- "a continuous"
if (scale$is_discrete()) type <- "a discrete"
if (inherits(scale, "ScaleBinned")) type <- "a binned"
}
if (is.null(lab)) {
return(NULL)
}
paste0(lab, " on ", type, " ", name, "-axis")
}