@@ -84,6 +84,10 @@ setup_plot_labels <- function(plot, layers, data) {
84
84
))
85
85
}
86
86
87
+ if (length(plot_labels $ dict ) > 0 ) {
88
+ labels <- lapply(labels , revalue , replace = plot_labels $ dict )
89
+ }
90
+
87
91
defaults(plot_labels , labels )
88
92
}
89
93
@@ -114,6 +118,9 @@ setup_plot_labels <- function(plot, layers, data) {
114
118
# ' bottom-right of the plot by default.
115
119
# ' @param tag The text for the tag label which will be displayed at the
116
120
# ' 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`.
117
124
# ' @param alt,alt_insight Text used for the generation of alt-text for the plot.
118
125
# ' See [get_alt_text] for examples. `alt` can also be a function that
119
126
# ' takes the plot as input and returns text as output. `alt` also accepts
@@ -128,6 +135,14 @@ setup_plot_labels <- function(plot, layers, data) {
128
135
# ' p + labs(colour = "Cylinders")
129
136
# ' p + labs(x = "New x label")
130
137
# '
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
+ # '
131
146
# ' # The plot title appears at the top-left, with the subtitle
132
147
# ' # display in smaller text underneath it
133
148
# ' p + labs(title = "New plot title")
@@ -146,10 +161,11 @@ setup_plot_labels <- function(plot, layers, data) {
146
161
# ' labs(title = "title") +
147
162
# ' labs(title = NULL)
148
163
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()) {
150
166
# .ignore_empty = "all" is needed to allow trailing commas, which is NOT a trailing comma for dots_list() as it's in ...
151
167
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 ,
153
169
.ignore_empty = " all" )
154
170
155
171
is_waive <- vapply(args , is.waive , logical (1 ))
0 commit comments