1
1
# ' @export
2
2
# ' @rdname geom_density_2d
3
3
# ' @param contour If `TRUE`, contour the results of the 2d density
4
- # ' estimation
5
- # ' @param contour_type When `contour = TRUE`, specifies whether the output
6
- # ' is contour lines (`contour_type = "lines"`) or contour bands
7
- # ' (`contour_type = "bands"`). For filled contours, you need to specify
8
- # ' bands.
4
+ # ' estimation.
9
5
# ' @param contour_var Character string identifying the variable to contour
10
6
# ' by. Can be one of `"density"`, `"ndensity"`, or `"count"`. See the section
11
7
# ' on computed variables for details.
12
- # ' @param n number of grid points in each direction
8
+ # ' @param n Number of grid points in each direction.
13
9
# ' @param h Bandwidth (vector of length two). If `NULL`, estimated
14
10
# ' using [MASS::bandwidth.nrd()].
15
11
# ' @param adjust A multiplicative bandwidth adjustment to be used if 'h' is
16
12
# ' 'NULL'. This makes it possible to adjust the bandwidth while still
17
13
# ' using the a bandwidth estimator. For example, `adjust = 1/2` means
18
14
# ' use half of the default bandwidth.
19
15
# ' @section Computed variables:
20
- # ' `stat_density_2d()` computes different variables depending on whether
21
- # ' contouring is turned on or off. With contouring off (`contour = FALSE`),
22
- # ' the following variables are provided:
16
+ # ' `stat_density_2d()` and `stat_density_2d_filled()` compute different
17
+ # ' variables depending on whether contouring is turned on or off. With
18
+ # ' contouring off (`contour = FALSE`), both stats behave the same, and the
19
+ # ' following variables are provided:
23
20
# ' \describe{
24
21
# ' \item{`density`}{The density estimate.}
25
22
# ' \item{`ndensity`}{Density estimate, scaled to a maximum of 1.}
29
26
# '
30
27
# ' With contouring on (`contour = TRUE`), either [stat_contour()] or
31
28
# ' [stat_contour_filled()] (for contour lines or contour bands,
32
- # ' respectively) is run after the density estimate is calculated ,
29
+ # ' respectively) is run after the density estimate has been obtained ,
33
30
# ' and the computed variables are determined by these stats.
31
+ # ' Contours are calculated for one of the three types of density estimates
32
+ # ' obtained before contouring, `density`, `ndensity`, and `count`. Which
33
+ # ' of those should be used is determined by the `contour_var` parameter.
34
34
stat_density_2d <- function (mapping = NULL , data = NULL ,
35
35
geom = " density_2d" , position = " identity" ,
36
36
... ,
37
37
contour = TRUE ,
38
- contour_type = " lines" ,
39
38
contour_var = " density" ,
40
39
n = 100 ,
41
40
h = NULL ,
42
41
adjust = c(1 , 1 ),
43
42
na.rm = FALSE ,
44
43
show.legend = NA ,
45
44
inherit.aes = TRUE ) {
46
- if (isTRUE(contour_type == " bands" )) {
47
- stat <- StatDensity2dFilled
48
- } else {
49
- stat <- StatDensity2d
50
- }
51
-
52
45
layer(
53
46
data = data ,
54
47
mapping = mapping ,
55
- stat = stat ,
48
+ stat = StatDensity2d ,
56
49
geom = geom ,
57
50
position = position ,
58
51
show.legend = show.legend ,
59
52
inherit.aes = inherit.aes ,
60
53
params = list (
61
54
na.rm = na.rm ,
62
55
contour = contour ,
63
- contour_type = contour_type ,
64
56
contour_var = contour_var ,
65
57
n = n ,
66
58
h = h ,
@@ -70,11 +62,50 @@ stat_density_2d <- function(mapping = NULL, data = NULL,
70
62
)
71
63
}
72
64
73
- # ' @export
74
65
# ' @rdname geom_density_2d
75
66
# ' @usage NULL
67
+ # ' @export
76
68
stat_density2d <- stat_density_2d
77
69
70
+ # ' @rdname geom_density_2d
71
+ # ' @export
72
+ stat_density_2d_filled <- function (mapping = NULL , data = NULL ,
73
+ geom = " density_2d_filled" , position = " identity" ,
74
+ ... ,
75
+ contour = TRUE ,
76
+ contour_var = " density" ,
77
+ n = 100 ,
78
+ h = NULL ,
79
+ adjust = c(1 , 1 ),
80
+ na.rm = FALSE ,
81
+ show.legend = NA ,
82
+ inherit.aes = TRUE ) {
83
+ layer(
84
+ data = data ,
85
+ mapping = mapping ,
86
+ stat = StatDensity2dFilled ,
87
+ geom = geom ,
88
+ position = position ,
89
+ show.legend = show.legend ,
90
+ inherit.aes = inherit.aes ,
91
+ params = list (
92
+ na.rm = na.rm ,
93
+ contour = contour ,
94
+ contour_var = contour_var ,
95
+ n = n ,
96
+ h = h ,
97
+ adjust = adjust ,
98
+ ...
99
+ )
100
+ )
101
+ }
102
+
103
+ # ' @rdname geom_density_2d
104
+ # ' @usage NULL
105
+ # ' @export
106
+ stat_density2d_filled <- stat_density_2d_filled
107
+
108
+
78
109
# ' @rdname ggplot2-ggproto
79
110
# ' @format NULL
80
111
# ' @usage NULL
@@ -85,24 +116,20 @@ StatDensity2d <- ggproto("StatDensity2d", Stat,
85
116
required_aes = c(" x" , " y" ),
86
117
87
118
extra_params = c(
88
- " na.rm" , " contour" , " contour_type " , " contour_var" ,
119
+ " na.rm" , " contour" , " contour_var" ,
89
120
" bins" , " binwidth" , " breaks"
90
121
),
91
122
123
+ # stat used for contouring
124
+ contour_stat = StatContour ,
125
+
92
126
compute_layer = function (self , data , params , layout ) {
93
127
# first run the regular layer calculation to infer densities
94
128
data <- ggproto_parent(Stat , self )$ compute_layer(data , params , layout )
95
129
96
130
# if we're not contouring we're done
97
131
if (! isTRUE(params $ contour )) return (data )
98
132
99
- # otherwise, simulate last part compute_layer() in StatContour or StatContourFilled
100
- if (isTRUE(params $ contour_type == " bands" )) {
101
- cont_stat <- StatContourFilled
102
- } else {
103
- cont_stat <- StatContour
104
- }
105
-
106
133
# set up data and parameters for contouring
107
134
contour_var <- params $ contour_var %|| % " density"
108
135
if (! isTRUE(contour_var %in% c(" density" , " ndensity" , " count" ))) {
@@ -119,7 +146,7 @@ StatDensity2d <- ggproto("StatDensity2d", Stat,
119
146
args <- c(list (data = quote(data ), scales = quote(scales )), params )
120
147
dapply(data , " PANEL" , function (data ) {
121
148
scales <- layout $ get_scales(data $ PANEL [1 ])
122
- tryCatch(do.call(cont_stat $ compute_panel , args ), error = function (e ) {
149
+ tryCatch(do.call(self $ contour_stat $ compute_panel , args ), error = function (e ) {
123
150
warn(glue(" Computation failed in `{snake_class(self)}()`:\n {e$message}" ))
124
151
new_data_frame()
125
152
})
@@ -153,11 +180,15 @@ StatDensity2d <- ggproto("StatDensity2d", Stat,
153
180
}
154
181
)
155
182
183
+
184
+
156
185
# ' @rdname ggplot2-ggproto
157
186
# ' @format NULL
158
187
# ' @usage NULL
159
188
# ' @export
160
189
StatDensity2dFilled <- ggproto(" StatDensity2dFilled" , StatDensity2d ,
161
- default_aes = aes(colour = NA , fill = after_stat(level ))
190
+ default_aes = aes(colour = NA , fill = after_stat(level )),
191
+
192
+ contour_stat = StatContourFilled
162
193
)
163
194
0 commit comments