@@ -47,67 +47,34 @@ stat_bin_2d <- function(mapping = NULL, data = NULL,
47
47
stat_bin2d <- stat_bin_2d
48
48
49
49
# ' @rdname ggplot2-ggproto
50
+ # ' @include stat-summary-2d.R
50
51
# ' @format NULL
51
52
# ' @usage NULL
52
53
# ' @export
53
- StatBin2d <- ggproto(" StatBin2d" , Stat ,
54
+ StatBin2d <- ggproto(
55
+ " StatBin2d" , StatSummary2d ,
54
56
default_aes = aes(weight = 1 , fill = after_stat(count )),
55
57
required_aes = c(" x" , " y" ),
56
58
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 ) {
58
62
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
75
65
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
85
70
)
86
71
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
-
103
72
out $ count <- out $ value
104
73
out $ ncount <- out $ count / max(out $ count , na.rm = TRUE )
105
74
out $ density <- out $ count / sum(out $ count , na.rm = TRUE )
106
75
out $ ndensity <- out $ density / max(out $ density , na.rm = TRUE )
107
76
out
108
- },
109
-
110
- dropped_aes = " weight" # No longer available after transformation
77
+ }
111
78
)
112
79
113
80
dual_param <- function (x , default = list (x = NULL , y = NULL )) {
0 commit comments