Skip to content

Commit 78b3f3a

Browse files
authored
move make_summary_fun() to setup_params() (#5971)
* move `make_summary_fun()` to `setup_params()` * add news bullet
1 parent 332a8ea commit 78b3f3a

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@
148148
(@teunbrand, #5938, #4327).
149149
* Fixed bug where empty discrete scales weren't recognised as such
150150
(@teunbrand, #5945).
151+
* (internal) The summary function of `stat_summary()` and `stat_summary_bin()`
152+
is setup once in total instead of once per group (@teunbrand, #5971)
151153

152154
# ggplot2 3.5.1
153155

R/stat-summary-bin.R

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,24 +64,28 @@ stat_summary_bin <- function(mapping = NULL, data = NULL,
6464
StatSummaryBin <- ggproto("StatSummaryBin", Stat,
6565
required_aes = c("x", "y"),
6666

67-
extra_params = c("na.rm", "orientation"),
67+
extra_params = c("na.rm", "orientation", "fun.data", "fun.max", "fun.min", "fun.args"),
68+
6869
setup_params = function(data, params) {
69-
params$flipped_aes <- has_flipped_aes(data, params, ambiguous = TRUE)
70+
params$flipped_aes <- has_flipped_aes(data, params)
71+
params$fun <- make_summary_fun(
72+
params$fun.data, params$fun,
73+
params$fun.max, params$fun.min,
74+
params$fun.args %||% list()
75+
)
7076
params
7177
},
7278

73-
compute_group = function(data, scales, fun.data = NULL, fun = NULL,
74-
fun.max = NULL, fun.min = NULL, fun.args = list(),
79+
compute_group = function(data, scales, fun = NULL,
7580
bins = 30, binwidth = NULL, breaks = NULL,
7681
origin = NULL, right = FALSE, na.rm = FALSE,
7782
flipped_aes = FALSE) {
7883
data <- flip_data(data, flipped_aes)
79-
fun <- make_summary_fun(fun.data, fun, fun.max, fun.min, fun.args)
8084
x <- flipped_names(flipped_aes)$x
8185
breaks <- bin2d_breaks(scales[[x]], breaks, origin, binwidth, bins, right = right)
8286

8387
data$bin <- cut(data$x, breaks, include.lowest = TRUE, labels = FALSE)
84-
out <- dapply(data, "bin", fun)
88+
out <- dapply(data, "bin", fun %||% function(df) mean_se(df$y))
8589

8690
locs <- bin_loc(breaks, out$bin)
8791
out$x <- locs$mid

R/stat-summary.R

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,18 +181,22 @@ stat_summary <- function(mapping = NULL, data = NULL,
181181
StatSummary <- ggproto("StatSummary", Stat,
182182
required_aes = c("x", "y"),
183183

184-
extra_params = c("na.rm", "orientation"),
184+
extra_params = c("na.rm", "orientation", "fun.data", "fun.max", "fun.min", "fun.args"),
185+
185186
setup_params = function(data, params) {
186187
params$flipped_aes <- has_flipped_aes(data, params)
188+
params$fun <- make_summary_fun(
189+
params$fun.data, params$fun,
190+
params$fun.max, params$fun.min,
191+
params$fun.args %||% list()
192+
)
187193
params
188194
},
189195

190-
compute_panel = function(data, scales, fun.data = NULL, fun = NULL,
191-
fun.max = NULL, fun.min = NULL, fun.args = list(),
192-
na.rm = FALSE, flipped_aes = FALSE) {
196+
compute_panel = function(data, scales, fun = NULL,
197+
na.rm = FALSE, flipped_aes = FALSE) {
193198
data <- flip_data(data, flipped_aes)
194-
fun <- make_summary_fun(fun.data, fun, fun.max, fun.min, fun.args)
195-
summarised <- summarise_by_x(data, fun)
199+
summarised <- summarise_by_x(data, fun %||% function(df) mean_se(df$y))
196200
summarised$flipped_aes <- flipped_aes
197201
flip_data(summarised, flipped_aes)
198202
}

0 commit comments

Comments
 (0)