5
5
# ' @param n number of grid points in each direction
6
6
# ' @param h Bandwidth (vector of length two). If `NULL`, estimated
7
7
# ' using [MASS::bandwidth.nrd()].
8
+ # ' @param adjust A multiplicate bandwidth adjustment to be used if 'h' is
9
+ # ' 'NULL'. This makes it possible to adjust the bandwidth while still
10
+ # ' using the a bandwidth estimator. For example, `adjust = 1/2` means
11
+ # ' use half of the default bandwidth.
8
12
# ' @section Computed variables:
9
13
# ' Same as [stat_contour()]
10
14
# '
@@ -19,6 +23,7 @@ stat_density_2d <- function(mapping = NULL, data = NULL,
19
23
contour = TRUE ,
20
24
n = 100 ,
21
25
h = NULL ,
26
+ adjust = c(1 , 1 ),
22
27
na.rm = FALSE ,
23
28
show.legend = NA ,
24
29
inherit.aes = TRUE ) {
@@ -35,6 +40,7 @@ stat_density_2d <- function(mapping = NULL, data = NULL,
35
40
contour = contour ,
36
41
n = n ,
37
42
h = h ,
43
+ adjust = adjust ,
38
44
...
39
45
)
40
46
)
@@ -54,11 +60,12 @@ StatDensity2d <- ggproto("StatDensity2d", Stat,
54
60
55
61
required_aes = c(" x" , " y" ),
56
62
57
- compute_group = function (data , scales , na.rm = FALSE , h = NULL ,
63
+ compute_group = function (data , scales , na.rm = FALSE , h = NULL , adjust = c( 1 , 1 ),
58
64
contour = TRUE , n = 100 , bins = NULL ,
59
65
binwidth = NULL ) {
60
66
if (is.null(h )) {
61
67
h <- c(MASS :: bandwidth.nrd(data $ x ), MASS :: bandwidth.nrd(data $ y ))
68
+ h <- h * adjust
62
69
}
63
70
64
71
dens <- MASS :: kde2d(
0 commit comments