Skip to content

Commit d1c963a

Browse files
committed
Implement StatBin2d as subclass of StatSummary2d
1 parent 7aabdb7 commit d1c963a

File tree

1 file changed

+13
-46
lines changed

1 file changed

+13
-46
lines changed

R/stat-bin2d.R

Lines changed: 13 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -47,67 +47,34 @@ stat_bin_2d <- function(mapping = NULL, data = NULL,
4747
stat_bin2d <- stat_bin_2d
4848

4949
#' @rdname ggplot2-ggproto
50+
#' @include stat-summary-2d.R
5051
#' @format NULL
5152
#' @usage NULL
5253
#' @export
53-
StatBin2d <- ggproto("StatBin2d", Stat,
54+
StatBin2d <- ggproto(
55+
"StatBin2d", StatSummary2d,
5456
default_aes = aes(weight = 1, fill = after_stat(count)),
5557
required_aes = c("x", "y"),
5658

57-
setup_params = function(self, data, params) {
59+
compute_group = function(self, data, scales, binwidth = NULL, bins = 30,
60+
breaks = NULL, origin = NULL, drop = TRUE,
61+
boundary = 0, closed = NULL, center = NULL) {
5862

59-
if (is.character(params$drop)) {
60-
params$drop <- !identical(params$drop, "none")
61-
}
62-
63-
params <- fix_bin_params(params, fun = snake_class(self), version = "3.5.2")
64-
vars <- c("origin", "binwidth", "breaks", "center", "boundary")
65-
params[vars] <- lapply(params[vars], dual_param)
66-
params$closed <- dual_param(params$closed, list(x = "right", y = "right"))
67-
68-
params
69-
},
70-
71-
compute_group = function(data, scales, binwidth = NULL,
72-
bins = 30, breaks = NULL,
73-
center = NULL, boundary = 0, closed = NULL,
74-
origin = NULL, drop = TRUE) {
63+
data$z <- data$weight %||% 1
64+
data$weight <- NULL
7565

76-
bins <- dual_param(bins, list(x = 30, y = 30))
77-
78-
xbin <- compute_bins(
79-
data$x, scales$x, breaks$x, binwidth$x, bins$x,
80-
center$x, boundary$x, closed$x
81-
)
82-
ybin <- compute_bins(
83-
data$y, scales$y, breaks$y, binwidth$y, bins$y,
84-
center$y, boundary$y, closed$y
66+
out <- StatSummary2d$compute_group(
67+
data, scales, binwidth = binwidth, bins = bins, breaks = breaks,
68+
drop = drop, fun = "sum", boundary = boundary, closed = closed,
69+
center = center
8570
)
8671

87-
data$weight <- data$weight %||% 1
88-
89-
cut_id <- list(
90-
xbin = as.integer(bin_cut(data$x, xbin)),
91-
ybin = as.integer(bin_cut(data$y, ybin))
92-
)
93-
out <- tapply_df(data$weight, cut_id, sum, drop = drop)
94-
95-
xdim <- bin_loc(xbin$breaks, out$xbin)
96-
out$x <- xdim$mid
97-
out$width <- xdim$length
98-
99-
ydim <- bin_loc(ybin$breaks, out$ybin)
100-
out$y <- ydim$mid
101-
out$height <- ydim$length
102-
10372
out$count <- out$value
10473
out$ncount <- out$count / max(out$count, na.rm = TRUE)
10574
out$density <- out$count / sum(out$count, na.rm = TRUE)
10675
out$ndensity <- out$density / max(out$density, na.rm = TRUE)
10776
out
108-
},
109-
110-
dropped_aes = "weight" # No longer available after transformation
77+
}
11178
)
11279

11380
dual_param <- function(x, default = list(x = NULL, y = NULL)) {

0 commit comments

Comments
 (0)