@@ -84,6 +84,15 @@ setup_plot_labels <- function(plot, layers, data) {
84
84
))
85
85
}
86
86
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
+
87
96
defaults(plot_labels , labels )
88
97
}
89
98
@@ -114,6 +123,10 @@ setup_plot_labels <- function(plot, layers, data) {
114
123
# ' bottom-right of the plot by default.
115
124
# ' @param tag The text for the tag label which will be displayed at the
116
125
# ' 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`.
117
130
# ' @param alt,alt_insight Text used for the generation of alt-text for the plot.
118
131
# ' See [get_alt_text] for examples. `alt` can also be a function that
119
132
# ' takes the plot as input and returns text as output. `alt` also accepts
@@ -128,6 +141,14 @@ setup_plot_labels <- function(plot, layers, data) {
128
141
# ' p + labs(colour = "Cylinders")
129
142
# ' p + labs(x = "New x label")
130
143
# '
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
+ # '
131
152
# ' # The plot title appears at the top-left, with the subtitle
132
153
# ' # display in smaller text underneath it
133
154
# ' p + labs(title = "New plot title")
@@ -146,11 +167,12 @@ setup_plot_labels <- function(plot, layers, data) {
146
167
# ' labs(title = "title") +
147
168
# ' labs(title = NULL)
148
169
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()) {
150
172
# .ignore_empty = "all" is needed to allow trailing commas, which is NOT a trailing comma for dots_list() as it's in ...
151
173
args <- dots_list(... , title = title , subtitle = subtitle , caption = caption ,
152
174
tag = tag , alt = allow_lambda(alt ), alt_insight = alt_insight ,
153
- .ignore_empty = " all" )
175
+ dictionary = dictionary , .ignore_empty = " all" )
154
176
155
177
is_waive <- vapply(args , is.waiver , logical (1 ))
156
178
args <- args [! is_waive ]
0 commit comments