Skip to content

Commit 4873a0c

Browse files
committed
consistency of stat_bin2d()
1 parent 5477d8a commit 4873a0c

File tree

1 file changed

+30
-15
lines changed

1 file changed

+30
-15
lines changed

R/stat-bin2d.R

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,30 +52,45 @@ StatBin2d <- ggproto("StatBin2d", Stat,
5252
default_aes = aes(weight = 1, fill = after_stat(count)),
5353
required_aes = c("x", "y"),
5454

55-
compute_group = function(data, scales, binwidth = NULL, bins = 30,
56-
breaks = NULL, origin = NULL, drop = TRUE) {
55+
setup_params = function(self, data, params) {
56+
params <- fix_bin_params(params, fun = snake_class(self), version = "3.5.2")
5757

58-
origin <- dual_param(origin, list(NULL, NULL))
59-
binwidth <- dual_param(binwidth, list(NULL, NULL))
60-
breaks <- dual_param(breaks, list(NULL, NULL))
61-
bins <- dual_param(bins, list(x = 30, y = 30))
58+
vars <- c("origin", "binwidth", "breaks", "center", "boundary")
59+
params[vars] <- lapply(params[vars], dual_param)
60+
params$closed <- dual_param(params$closed, list(x = "right", y = "right"))
61+
62+
params
63+
},
64+
65+
compute_group = function(data, scales, binwidth = NULL,
66+
bins = 30, breaks = NULL,
67+
center = NULL, boundary = NULL, closed = NULL,
68+
origin = NULL, drop = TRUE) {
6269

63-
xbreaks <- bin2d_breaks(scales$x, breaks$x, origin$x, binwidth$x, bins$x)
64-
ybreaks <- bin2d_breaks(scales$y, breaks$y, origin$y, binwidth$y, bins$y)
70+
bins <- dual_param(bins, list(x = 30, y = 30))
6571

66-
xbin <- cut(data$x, xbreaks, include.lowest = TRUE, labels = FALSE)
67-
ybin <- cut(data$y, ybreaks, include.lowest = TRUE, labels = FALSE)
72+
xbin <- compute_bins(
73+
data$x, scales$x, breaks$x, binwidth$x, bins$x,
74+
center$x, boundary$x, closed$x
75+
)
76+
ybin <- compute_bins(
77+
data$y, scales$y, breaks$y, binwidth$y, bins$y,
78+
center$y, boundary$y, closed$y
79+
)
6880

69-
if (is.null(data$weight))
70-
data$weight <- 1
81+
data$weight <- data$weight %||% 1
7182

72-
out <- tapply_df(data$weight, list(xbin = xbin, ybin = ybin), sum, drop = drop)
83+
cut_id <- list(
84+
xbin = as.integer(bin_cut(data$x, xbin)),
85+
ybin = as.integer(bin_cut(data$y, ybin))
86+
)
87+
out <- tapply_df(data$weight, cut_id, sum, drop = drop)
7388

74-
xdim <- bin_loc(xbreaks, out$xbin)
89+
xdim <- bin_loc(xbin$breaks, out$xbin)
7590
out$x <- xdim$mid
7691
out$width <- xdim$length
7792

78-
ydim <- bin_loc(ybreaks, out$ybin)
93+
ydim <- bin_loc(ybin$breaks, out$ybin)
7994
out$y <- ydim$mid
8095
out$height <- ydim$length
8196

0 commit comments

Comments
 (0)