Skip to content

Commit 08f9d8e

Browse files
committed
first implementation of labs(dict)
1 parent 57ba97f commit 08f9d8e

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

R/labels.R

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

87+
if (length(plot_labels$dict) > 0) {
88+
labels <- lapply(labels, revalue, replace = plot_labels$dict)
89+
}
90+
8791
defaults(plot_labels, labels)
8892
}
8993

@@ -114,6 +118,9 @@ setup_plot_labels <- function(plot, layers, data) {
114118
#' bottom-right of the plot by default.
115119
#' @param tag The text for the tag label which will be displayed at the
116120
#' top-left of the plot by default.
121+
#' @param dict A named character vector to serve as dictionary. Automatically
122+
#' derived labels, such as those based on variables will be matched with
123+
#' `names(dict)` and replaced by the matching entry in `dict`.
117124
#' @param alt,alt_insight Text used for the generation of alt-text for the plot.
118125
#' See [get_alt_text] for examples. `alt` can also be a function that
119126
#' takes the plot as input and returns text as output. `alt` also accepts
@@ -128,6 +135,14 @@ setup_plot_labels <- function(plot, layers, data) {
128135
#' p + labs(colour = "Cylinders")
129136
#' p + labs(x = "New x label")
130137
#'
138+
#' # Set labels by variable name instead of aesthetic
139+
#' p + labs(dict = c(
140+
#' disp = "Displacment", # Not in use
141+
#' cyl = "Number of cylinders",
142+
#' mpg = "Miles per gallon",
143+
#' wt = "Weight (1000 lbs)"
144+
#' ))
145+
#'
131146
#' # The plot title appears at the top-left, with the subtitle
132147
#' # display in smaller text underneath it
133148
#' p + labs(title = "New plot title")
@@ -146,10 +161,11 @@ setup_plot_labels <- function(plot, layers, data) {
146161
#' labs(title = "title") +
147162
#' labs(title = NULL)
148163
labs <- function(..., title = waiver(), subtitle = waiver(), caption = waiver(),
149-
tag = waiver(), alt = waiver(), alt_insight = waiver()) {
164+
tag = waiver(), dict = waiver(), alt = waiver(),
165+
alt_insight = waiver()) {
150166
# .ignore_empty = "all" is needed to allow trailing commas, which is NOT a trailing comma for dots_list() as it's in ...
151167
args <- dots_list(..., title = title, subtitle = subtitle, caption = caption,
152-
tag = tag, alt = allow_lambda(alt), alt_insight = alt_insight,
168+
tag = tag, alt = allow_lambda(alt), alt_insight = alt_insight, dict = dict,
153169
.ignore_empty = "all")
154170

155171
is_waive <- vapply(args, is.waive, logical(1))

man/labs.Rd

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

0 commit comments

Comments
 (0)