Skip to content

Commit 7aabdb7

Browse files
committed
same treatment for stat_summary2d()
1 parent 0b620cf commit 7aabdb7

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

R/stat-summary-2d.R

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -92,31 +92,50 @@ StatSummary2d <- ggproto("StatSummary2d", Stat,
9292
required_aes = c("x", "y", "z"),
9393
dropped_aes = "z", # z gets dropped during statistical transformation
9494

95+
setup_params = function(self, data, params) {
96+
97+
if (is.character(params$drop)) {
98+
params$drop <- !identical(params$drop, "none")
99+
}
100+
101+
params <- fix_bin_params(params, fun = snake_class(self), version = "3.5.2")
102+
vars <- c("origin", "binwidth", "breaks", "center", "boundary")
103+
params[vars] <- lapply(params[vars], dual_param)
104+
params$closed <- dual_param(params$closed, list(x = "right", y = "right"))
105+
106+
params
107+
},
108+
95109
compute_group = function(data, scales, binwidth = NULL, bins = 30,
96110
breaks = NULL, origin = NULL, drop = TRUE,
97-
fun = "mean", fun.args = list()) {
98-
origin <- dual_param(origin, list(NULL, NULL))
99-
binwidth <- dual_param(binwidth, list(NULL, NULL))
100-
breaks <- dual_param(breaks, list(NULL, NULL))
111+
fun = "mean", fun.args = list(),
112+
boundary = 0, closed = NULL, center = NULL) {
101113
bins <- dual_param(bins, list(x = 30, y = 30))
102114

103-
xbreaks <- bin2d_breaks(scales$x, breaks$x, origin$x, binwidth$x, bins$x)
104-
ybreaks <- bin2d_breaks(scales$y, breaks$y, origin$y, binwidth$y, bins$y)
105-
106-
xbin <- cut(data$x, xbreaks, include.lowest = TRUE, labels = FALSE)
107-
ybin <- cut(data$y, ybreaks, include.lowest = TRUE, labels = FALSE)
115+
xbin <- compute_bins(
116+
data$x, scales$x, breaks$x, binwidth$x, bins$x,
117+
center$x, boundary$x, closed$x
118+
)
119+
ybin <- compute_bins(
120+
data$y, scales$y, breaks$y, binwidth$y, bins$y,
121+
center$y, boundary$y, closed$y
122+
)
123+
cut_id <- list(
124+
xbin = as.integer(bin_cut(data$x, xbin)),
125+
ybin = as.integer(bin_cut(data$y, ybin))
126+
)
108127

109128
fun <- as_function(fun)
110129
f <- function(x) {
111130
inject(fun(x, !!!fun.args))
112131
}
113-
out <- tapply_df(data$z, list(xbin = xbin, ybin = ybin), f, drop = drop)
132+
out <- tapply_df(data$z, cut_id, f, drop = drop)
114133

115-
xdim <- bin_loc(xbreaks, out$xbin)
134+
xdim <- bin_loc(xbin$breaks, out$xbin)
116135
out$x <- xdim$mid
117136
out$width <- xdim$length
118137

119-
ydim <- bin_loc(ybreaks, out$ybin)
138+
ydim <- bin_loc(ybin$breaks, out$ybin)
120139
out$y <- ydim$mid
121140
out$height <- ydim$length
122141

0 commit comments

Comments
 (0)