Skip to content

Commit 4af509e

Browse files
authored
Label dictionary (#6077)
* first implementation of `labs(dict)` * add test * add news bullet * rename dict --> dictionary * `revalue` was removed
1 parent 918821a commit 4af509e

File tree

4 files changed

+65
-2
lines changed

4 files changed

+65
-2
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# ggplot2 (development version)
22

3+
* New argument `labs(dictionary)` to label based on variable name rather than
4+
based on aesthetic (@teunbrand, #5178)
35
* Fixed bug in out-of-bounds binned breaks (@teunbrand, #6054)
46
* Binned guides now accept expressions as labels (@teunbrand, #6005)
57
* (internal) `Scale$get_labels()` format expressions as lists.

R/labels.R

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,15 @@ setup_plot_labels <- function(plot, layers, data) {
8484
))
8585
}
8686

87+
dict <- plot_labels$dictionary
88+
if (length(dict) > 0) {
89+
labels <- lapply(labels, function(x) {
90+
dict <- dict[names(dict) %in% x]
91+
x[match(names(dict), x)] <- dict
92+
x
93+
})
94+
}
95+
8796
defaults(plot_labels, labels)
8897
}
8998

@@ -114,6 +123,10 @@ setup_plot_labels <- function(plot, layers, data) {
114123
#' bottom-right of the plot by default.
115124
#' @param tag The text for the tag label which will be displayed at the
116125
#' top-left of the plot by default.
126+
#' @param dictionary A named character vector to serve as dictionary.
127+
#' Automatically derived labels, such as those based on variables will
128+
#' be matched with `names(dictionary)` and replaced by the matching
129+
#' entry in `dictionary`.
117130
#' @param alt,alt_insight Text used for the generation of alt-text for the plot.
118131
#' See [get_alt_text] for examples. `alt` can also be a function that
119132
#' takes the plot as input and returns text as output. `alt` also accepts
@@ -128,6 +141,14 @@ setup_plot_labels <- function(plot, layers, data) {
128141
#' p + labs(colour = "Cylinders")
129142
#' p + labs(x = "New x label")
130143
#'
144+
#' # Set labels by variable name instead of aesthetic
145+
#' p + labs(dict = c(
146+
#' disp = "Displacment", # Not in use
147+
#' cyl = "Number of cylinders",
148+
#' mpg = "Miles per gallon",
149+
#' wt = "Weight (1000 lbs)"
150+
#' ))
151+
#'
131152
#' # The plot title appears at the top-left, with the subtitle
132153
#' # display in smaller text underneath it
133154
#' p + labs(title = "New plot title")
@@ -146,11 +167,12 @@ setup_plot_labels <- function(plot, layers, data) {
146167
#' labs(title = "title") +
147168
#' labs(title = NULL)
148169
labs <- function(..., title = waiver(), subtitle = waiver(), caption = waiver(),
149-
tag = waiver(), alt = waiver(), alt_insight = waiver()) {
170+
tag = waiver(), dictionary = waiver(), alt = waiver(),
171+
alt_insight = waiver()) {
150172
# .ignore_empty = "all" is needed to allow trailing commas, which is NOT a trailing comma for dots_list() as it's in ...
151173
args <- dots_list(..., title = title, subtitle = subtitle, caption = caption,
152174
tag = tag, alt = allow_lambda(alt), alt_insight = alt_insight,
153-
.ignore_empty = "all")
175+
dictionary = dictionary, .ignore_empty = "all")
154176

155177
is_waive <- vapply(args, is.waiver, logical(1))
156178
args <- args[!is_waive]

man/labs.Rd

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-labels.R

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,31 @@ test_that("moving guide positions lets titles follow", {
246246
expect_identical(labs[names(expect)], expect)
247247
})
248248

249+
test_that("label dictionaries work", {
250+
251+
p <- ggplot(mtcars, aes(disp, mpg, shape = factor(cyl), size = drat)) +
252+
geom_point() +
253+
labs(dictionary = c(
254+
disp = "Displacement",
255+
mpg = "Miles per gallon",
256+
`factor(cyl)` = "Number of cylinders",
257+
drat = "Rear axle ratio"
258+
))
259+
p <- ggplot_build(p)
260+
261+
x <- p$layout$resolve_label(p$layout$panel_scales_x[[1]], p$plot$labels)
262+
expect_equal(x$primary, "Displacement")
263+
264+
y <- p$layout$resolve_label(p$layout$panel_scales_y[[1]], p$plot$labels)
265+
expect_equal(y$primary, "Miles per gallon")
266+
267+
shape <- p$plot$guides$get_params("shape")$title
268+
expect_equal(shape, "Number of cylinders")
269+
270+
size <- p$plot$guides$get_params("size")$title
271+
expect_equal(size, "Rear axle ratio")
272+
})
273+
249274
# Visual tests ------------------------------------------------------------
250275

251276
test_that("tags are drawn correctly", {

0 commit comments

Comments
 (0)