22
22
# ' @param breaks Alternatively, you can supply a numeric vector giving
23
23
# ' the bin boundaries. Overrides `binwidth`, `bins`, `center`,
24
24
# ' and `boundary`. Can also be a function that takes group-wise values as input and returns bin boundaries.
25
+ # ' @param follow.scale Alternatively, the bin edges can be copied from the scale
26
+ # ' breaks, either `"major"` or `"minor"`. Ignored when `"off"`. Note that if
27
+ # . the scale's limits are updated by other layers or expansions then its
28
+ # . breaks are recomputed and might end up different to the value copied for
29
+ # . the bin edges. This is not an issue when the scale uses a fixed breaks
30
+ # . vector.
25
31
# ' @param closed One of `"right"` or `"left"` indicating whether right
26
32
# ' or left edges of bins are included in the bin.
27
33
# ' @param pad If `TRUE`, adds empty bins at either end of x. This ensures
@@ -58,8 +64,8 @@ stat_bin <- function(mapping = NULL, data = NULL,
58
64
breaks = NULL ,
59
65
closed = c(" right" , " left" ),
60
66
pad = FALSE ,
67
+ follow.scale = c(" off" , " minor" , " major" ),
61
68
na.rm = FALSE ,
62
- follow.scale = FALSE ,
63
69
keep.zeroes = " all" ,
64
70
orientation = NA ,
65
71
show.legend = NA ,
@@ -81,8 +87,8 @@ stat_bin <- function(mapping = NULL, data = NULL,
81
87
breaks = breaks ,
82
88
closed = closed ,
83
89
pad = pad ,
84
- na.rm = na.rm ,
85
90
follow.scale = follow.scale ,
91
+ na.rm = na.rm ,
86
92
orientation = orientation ,
87
93
keep.zeroes = keep.zeroes ,
88
94
...
@@ -138,7 +144,15 @@ StatBin <- ggproto("StatBin", Stat,
138
144
cli :: cli_abort(" Only one of {.arg boundary} and {.arg center} may be specified in {.fn {snake_class(self)}}." )
139
145
}
140
146
141
- if (is.null(params $ breaks ) && is.null(params $ binwidth ) && is.null(params $ bins )) {
147
+ if (! is.null(params $ follow.scale )) {
148
+ params $ follow.scale <- match.arg(params $ follow.scale , c(" off" , " minor" , " major" ))
149
+ if (params $ follow.scale == " off" ) params $ follow.scale <- NULL
150
+ }
151
+ if (! is.null(params $ follow.scale ) && ! is.null(params $ breaks )) {
152
+ cli :: cli_abort(" Only one of {.arg follow.scale} and {.arg breaks} may be specified in {.fn {snake_class(self)}}." )
153
+ }
154
+
155
+ if (is.null(params $ breaks ) && is.null(params $ binwidth ) && is.null(params $ bins ) && is.null(params $ follow.scale )) {
142
156
cli :: cli_inform(" {.fn {snake_class(self)}} using {.code bins = 30}. Pick better value with {.arg binwidth}." )
143
157
params $ bins <- 30
144
158
}
@@ -152,13 +166,15 @@ StatBin <- ggproto("StatBin", Stat,
152
166
center = NULL , boundary = NULL ,
153
167
closed = c(" right" , " left" ), pad = FALSE ,
154
168
breaks = NULL , flipped_aes = FALSE , keep.zeroes = " all" ,
155
- follow.scale = FALSE ,
169
+ follow.scale = NULL ,
156
170
# The following arguments are not used, but must
157
171
# be listed so parameters are computed correctly
158
172
origin = NULL , right = NULL , drop = NULL ) {
159
173
x <- flipped_names(flipped_aes )$ x
160
- if (follow.scale ) {
161
- breaks <- scales [[x ]]$ get_breaks()
174
+ if (! is.null(follow.scale )) {
175
+ breaks <- switch (follow.scale ,
176
+ minor = scales [[x ]]$ get_breaks_minor(),
177
+ major = scales [[x ]]$ get_breaks())
162
178
bins <- bin_breaks(breaks , closed )
163
179
} else if (! is.null(breaks )) {
164
180
if (is.function(breaks )) {
0 commit comments