@@ -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)
148169labs <- 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 ]
0 commit comments