Skip to content

Commit

Permalink
move make_summary_fun() to setup_params() (#5971)
Browse files Browse the repository at this point in the history
* move `make_summary_fun()` to `setup_params()`

* add news bullet
  • Loading branch information
teunbrand committed Aug 26, 2024
1 parent 332a8ea commit 78b3f3a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@
(@teunbrand, #5938, #4327).
* Fixed bug where empty discrete scales weren't recognised as such
(@teunbrand, #5945).
* (internal) The summary function of `stat_summary()` and `stat_summary_bin()`
is setup once in total instead of once per group (@teunbrand, #5971)

# ggplot2 3.5.1

Expand Down
16 changes: 10 additions & 6 deletions R/stat-summary-bin.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,24 +64,28 @@ stat_summary_bin <- function(mapping = NULL, data = NULL,
StatSummaryBin <- ggproto("StatSummaryBin", Stat,
required_aes = c("x", "y"),

extra_params = c("na.rm", "orientation"),
extra_params = c("na.rm", "orientation", "fun.data", "fun.max", "fun.min", "fun.args"),

setup_params = function(data, params) {
params$flipped_aes <- has_flipped_aes(data, params, ambiguous = TRUE)
params$flipped_aes <- has_flipped_aes(data, params)
params$fun <- make_summary_fun(
params$fun.data, params$fun,
params$fun.max, params$fun.min,
params$fun.args %||% list()
)
params
},

compute_group = function(data, scales, fun.data = NULL, fun = NULL,
fun.max = NULL, fun.min = NULL, fun.args = list(),
compute_group = function(data, scales, fun = NULL,
bins = 30, binwidth = NULL, breaks = NULL,
origin = NULL, right = FALSE, na.rm = FALSE,
flipped_aes = FALSE) {
data <- flip_data(data, flipped_aes)
fun <- make_summary_fun(fun.data, fun, fun.max, fun.min, fun.args)
x <- flipped_names(flipped_aes)$x
breaks <- bin2d_breaks(scales[[x]], breaks, origin, binwidth, bins, right = right)

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

locs <- bin_loc(breaks, out$bin)
out$x <- locs$mid
Expand Down
16 changes: 10 additions & 6 deletions R/stat-summary.R
Original file line number Diff line number Diff line change
Expand Up @@ -181,18 +181,22 @@ stat_summary <- function(mapping = NULL, data = NULL,
StatSummary <- ggproto("StatSummary", Stat,
required_aes = c("x", "y"),

extra_params = c("na.rm", "orientation"),
extra_params = c("na.rm", "orientation", "fun.data", "fun.max", "fun.min", "fun.args"),

setup_params = function(data, params) {
params$flipped_aes <- has_flipped_aes(data, params)
params$fun <- make_summary_fun(
params$fun.data, params$fun,
params$fun.max, params$fun.min,
params$fun.args %||% list()
)
params
},

compute_panel = function(data, scales, fun.data = NULL, fun = NULL,
fun.max = NULL, fun.min = NULL, fun.args = list(),
na.rm = FALSE, flipped_aes = FALSE) {
compute_panel = function(data, scales, fun = NULL,
na.rm = FALSE, flipped_aes = FALSE) {
data <- flip_data(data, flipped_aes)
fun <- make_summary_fun(fun.data, fun, fun.max, fun.min, fun.args)
summarised <- summarise_by_x(data, fun)
summarised <- summarise_by_x(data, fun %||% function(df) mean_se(df$y))
summarised$flipped_aes <- flipped_aes
flip_data(summarised, flipped_aes)
}
Expand Down

0 comments on commit 78b3f3a

Please sign in to comment.