18
18
# ' [transformation object][scales::trans_new()]
19
19
# ' - A numeric vector of positions
20
20
# ' - A function that takes the limits as input and returns breaks
21
- # ' as output (e.g., a function returned by [scales::extended_breaks()])
21
+ # ' as output (e.g., a function returned by [scales::extended_breaks()]).
22
+ # ' Also accepts rlang [lambda][rlang::as_function()] function notation.
22
23
# ' @param minor_breaks One of:
23
24
# ' - `NULL` for no minor breaks
24
25
# ' - `waiver()` for the default breaks (one minor break between
25
26
# ' each major break)
26
27
# ' - A numeric vector of positions
27
- # ' - A function that given the limits returns a vector of minor breaks.
28
+ # ' - A function that given the limits returns a vector of minor breaks. Also
29
+ # ' accepts rlang [lambda][rlang::as_function()] function notation.
28
30
# ' @param n.breaks An integer guiding the number of major breaks. The algorithm
29
31
# ' may choose a slightly different number to ensure nice break labels. Will
30
32
# ' only have an effect if `breaks = waiver()`. Use `NULL` to use the default
35
37
# ' transformation object
36
38
# ' - A character vector giving labels (must be same length as `breaks`)
37
39
# ' - A function that takes the breaks as input and returns labels
38
- # ' as output
40
+ # ' as output. Also accepts rlang [lambda][rlang::as_function()] function
41
+ # ' notation.
39
42
# ' @param limits One of:
40
43
# ' - `NULL` to use the default scale range
41
44
# ' - A numeric vector of length two providing limits of the scale.
42
45
# ' Use `NA` to refer to the existing minimum or maximum
43
46
# ' - A function that accepts the existing (automatic) limits and returns
44
- # ' new limits
47
+ # ' new limits. Also accepts rlang [lambda][rlang::as_function()] function
48
+ # ' notation.
45
49
# ' Note that setting limits on positional scales will **remove** data outside of the limits.
46
50
# ' If the purpose is to zoom, use the limit argument in the coordinate system
47
51
# ' (see [coord_cartesian()]).
48
52
# ' @param rescaler A function used to scale the input values to the
49
53
# ' range \[0, 1]. This is always [scales::rescale()], except for
50
54
# ' diverging and n colour gradients (i.e., [scale_colour_gradient2()],
51
55
# ' [scale_colour_gradientn()]). The `rescaler` is ignored by position
52
- # ' scales, which always use [scales::rescale()].
56
+ # ' scales, which always use [scales::rescale()]. Also accepts rlang
57
+ # ' [lambda][rlang::as_function()] function notation.
53
58
# ' @param oob One of:
54
59
# ' - Function that handles limits outside of the scale limits
55
- # ' (out of bounds).
60
+ # ' (out of bounds). Also accepts rlang [lambda][rlang::as_function()]
61
+ # ' function notation.
56
62
# ' - The default ([scales::censor()]) replaces out of
57
63
# ' bounds values with `NA`.
58
64
# ' - [scales::squish()] for squishing out of bounds values into range.
@@ -104,6 +110,14 @@ continuous_scale <- function(aesthetics, scale_name, palette, name = waiver(),
104
110
limits <- trans $ transform(limits )
105
111
}
106
112
113
+ # Convert formula to function if appropriate
114
+ limits <- allow_lambda(limits )
115
+ breaks <- allow_lambda(breaks )
116
+ labels <- allow_lambda(labels )
117
+ rescaler <- allow_lambda(rescaler )
118
+ oob <- allow_lambda(oob )
119
+ minor_breaks <- allow_lambda(minor_breaks )
120
+
107
121
ggproto(NULL , super ,
108
122
call = match.call(),
109
123
@@ -142,13 +156,15 @@ continuous_scale <- function(aesthetics, scale_name, palette, name = waiver(),
142
156
# ' - `waiver()` for the default breaks (the scale limits)
143
157
# ' - A character vector of breaks
144
158
# ' - A function that takes the limits as input and returns breaks
145
- # ' as output
159
+ # ' as output. Also accepts rlang [lambda][rlang::as_function()] function
160
+ # ' notation.
146
161
# ' @param limits One of:
147
162
# ' - `NULL` to use the default scale values
148
163
# ' - A character vector that defines possible values of the scale and their
149
164
# ' order
150
165
# ' - A function that accepts the existing (automatic) values and returns
151
- # ' new ones
166
+ # ' new ones. Also accepts rlang [lambda][rlang::as_function()] function
167
+ # ' notation.
152
168
# ' @param drop Should unused factor levels be omitted from the scale?
153
169
# ' The default, `TRUE`, uses the levels that appear in the data;
154
170
# ' `FALSE` uses all the levels in the factor.
@@ -168,6 +184,11 @@ discrete_scale <- function(aesthetics, scale_name, palette, name = waiver(),
168
184
169
185
check_breaks_labels(breaks , labels )
170
186
187
+ # Convert formula input to function if appropriate
188
+ limits <- allow_lambda(limits )
189
+ breaks <- allow_lambda(breaks )
190
+ labels <- allow_lambda(labels )
191
+
171
192
if (! is.function(limits ) && (length(limits ) > 0 ) && ! is.discrete(limits )) {
172
193
warn(
173
194
glue(
@@ -217,7 +238,7 @@ discrete_scale <- function(aesthetics, scale_name, palette, name = waiver(),
217
238
# ' instead of exactly evenly spaced between the limits. If `TRUE` (default)
218
239
# ' the scale will ask the transformation object to create breaks, and this
219
240
# ' may result in a different number of breaks than requested. Ignored if
220
- # ' breaks are given explicetly .
241
+ # ' breaks are given explicitly .
221
242
# ' @param right Should values on the border between bins be part of the right
222
243
# ' (upper) bin?
223
244
# ' @param show.limits should the limits of the scale appear as ticks
@@ -244,6 +265,13 @@ binned_scale <- function(aesthetics, scale_name, palette, name = waiver(),
244
265
limits <- trans $ transform(limits )
245
266
}
246
267
268
+ # Convert formula input to function if appropriate
269
+ limits <- allow_lambda(limits )
270
+ breaks <- allow_lambda(breaks )
271
+ labels <- allow_lambda(labels )
272
+ rescaler <- allow_lambda(rescaler )
273
+ oob <- allow_lambda(oob )
274
+
247
275
ggproto(NULL , super ,
248
276
call = match.call(),
249
277
@@ -1167,3 +1195,7 @@ check_transformation <- function(x, transformed, name, axis) {
1167
1195
trans_support_nbreaks <- function (trans ) {
1168
1196
" n" %in% names(formals(trans $ breaks ))
1169
1197
}
1198
+
1199
+ allow_lambda <- function (x ) {
1200
+ if (is_formula(x )) as_function(x ) else x
1201
+ }
0 commit comments