@@ -35,7 +35,8 @@ stat_ydensity <- function(mapping = NULL, data = NULL,
35
35
na.rm = FALSE ,
36
36
orientation = NA ,
37
37
show.legend = NA ,
38
- inherit.aes = TRUE ) {
38
+ inherit.aes = TRUE ,
39
+ bounds = c(- Inf , Inf )) {
39
40
scale <- arg_match0(scale , c(" area" , " count" , " width" ))
40
41
41
42
layer(
@@ -54,6 +55,7 @@ stat_ydensity <- function(mapping = NULL, data = NULL,
54
55
scale = scale ,
55
56
drop = drop ,
56
57
na.rm = na.rm ,
58
+ bounds = bounds ,
57
59
...
58
60
)
59
61
)
@@ -78,7 +80,7 @@ StatYdensity <- ggproto("StatYdensity", Stat,
78
80
79
81
compute_group = function (self , data , scales , width = NULL , bw = " nrd0" , adjust = 1 ,
80
82
kernel = " gaussian" , trim = TRUE , na.rm = FALSE ,
81
- drop = TRUE , flipped_aes = FALSE ) {
83
+ drop = TRUE , flipped_aes = FALSE , bounds = c( - Inf , Inf ) ) {
82
84
if (nrow(data ) < 2 ) {
83
85
if (isTRUE(drop )) {
84
86
cli :: cli_warn(c(
@@ -98,7 +100,7 @@ StatYdensity <- ggproto("StatYdensity", Stat,
98
100
dens <- compute_density(
99
101
data $ y , data [[" weight" ]],
100
102
from = range [1 ] - modifier * bw , to = range [2 ] + modifier * bw ,
101
- bw = bw , adjust = adjust , kernel = kernel
103
+ bw = bw , adjust = adjust , kernel = kernel , bounds = bounds
102
104
)
103
105
104
106
dens $ y <- dens $ x
@@ -118,11 +120,12 @@ StatYdensity <- ggproto("StatYdensity", Stat,
118
120
119
121
compute_panel = function (self , data , scales , width = NULL , bw = " nrd0" , adjust = 1 ,
120
122
kernel = " gaussian" , trim = TRUE , na.rm = FALSE ,
121
- scale = " area" , flipped_aes = FALSE , drop = TRUE ) {
123
+ scale = " area" , flipped_aes = FALSE , drop = TRUE ,
124
+ bounds = c(- Inf , Inf )) {
122
125
data <- flip_data(data , flipped_aes )
123
126
data <- ggproto_parent(Stat , self )$ compute_panel(
124
127
data , scales , width = width , bw = bw , adjust = adjust , kernel = kernel ,
125
- trim = trim , na.rm = na.rm , drop = drop
128
+ trim = trim , na.rm = na.rm , drop = drop , bounds = bounds ,
126
129
)
127
130
if (! drop && any(data $ n < 2 )) {
128
131
cli :: cli_warn(
0 commit comments