@@ -92,31 +92,50 @@ StatSummary2d <- ggproto("StatSummary2d", Stat,
92
92
required_aes = c(" x" , " y" , " z" ),
93
93
dropped_aes = " z" , # z gets dropped during statistical transformation
94
94
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
+
95
109
compute_group = function (data , scales , binwidth = NULL , bins = 30 ,
96
110
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 ) {
101
113
bins <- dual_param(bins , list (x = 30 , y = 30 ))
102
114
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
+ )
108
127
109
128
fun <- as_function(fun )
110
129
f <- function (x ) {
111
130
inject(fun(x , !!! fun.args ))
112
131
}
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 )
114
133
115
- xdim <- bin_loc(xbreaks , out $ xbin )
134
+ xdim <- bin_loc(xbin $ breaks , out $ xbin )
116
135
out $ x <- xdim $ mid
117
136
out $ width <- xdim $ length
118
137
119
- ydim <- bin_loc(ybreaks , out $ ybin )
138
+ ydim <- bin_loc(ybin $ breaks , out $ ybin )
120
139
out $ y <- ydim $ mid
121
140
out $ height <- ydim $ length
122
141
0 commit comments