@@ -36,54 +36,17 @@ stat_binhex <- stat_bin_hex
36
36
# ' @usage NULL
37
37
# ' @export
38
38
StatBinhex <- ggproto(" StatBinhex" , Stat ,
39
- default_aes = aes(fill = ..count .. ),
39
+ default_aes = aes(fill = ..value .. ),
40
40
41
41
required_aes = c(" x" , " y" ),
42
42
43
43
compute_group = function (data , scales , binwidth = NULL , bins = 30 ,
44
44
na.rm = FALSE ) {
45
- if (is.null(binwidth )) {
46
- binwidth <- c(
47
- diff(scales $ x $ dimension()) / bins ,
48
- diff(scales $ y $ dimension()) / bins
49
- )
50
- }
45
+ try_require(" hexbin" , " stat_binhex" )
51
46
52
- hexBin(data $ x , data $ y , binwidth )
47
+ binwidth <- binwidth %|| % hex_binwidth(bins , scales )
48
+ wt <- data $ weight %|| % rep(1L , nrow(data ))
49
+ hexBinSummarise(data $ x , data $ y , wt , binwidth , sum )
53
50
}
54
51
)
55
52
56
- # Bin 2d plane into hexagons
57
- # Wrapper around \code{\link[hexbin]{hcell2xy}} that returns a data frame
58
- #
59
- # @param x positions
60
- # @param y positions
61
- # @param numeric vector of length 2 giving binwidth in x and y directions
62
- # @keyword internal
63
- hexBin <- function (x , y , binwidth ) {
64
- # Convert binwidths into bounds + nbins
65
- xbnds <- c(
66
- plyr :: round_any(min(x ), binwidth [1 ], floor ) - 1e-6 ,
67
- plyr :: round_any(max(x ), binwidth [1 ], ceiling ) + 1e-6
68
- )
69
- xbins <- diff(xbnds ) / binwidth [1 ]
70
-
71
- ybnds <- c(
72
- plyr :: round_any(min(y ), binwidth [2 ], floor ) - 1e-6 ,
73
- plyr :: round_any(max(y ), binwidth [2 ], ceiling ) + 1e-6
74
- )
75
- ybins <- diff(ybnds ) / binwidth [2 ]
76
-
77
- # Call hexbin
78
- hb <- hexbin :: hexbin(
79
- x , xbnds = xbnds , xbins = xbins ,
80
- y , ybnds = ybnds , shape = ybins / xbins
81
- )
82
-
83
- # Convert to data frame
84
- data.frame (
85
- hexbin :: hcell2xy(hb ),
86
- count = hb @ count ,
87
- density = hb @ count / sum(hb @ count , na.rm = TRUE )
88
- )
89
- }
0 commit comments