5
5
# ' a warning. If \code{TRUE} silently removes missing values.
6
6
# ' @param n if NULL, do not interpolate. If not NULL, this is the number
7
7
# ' of points to interpolate with.
8
+ # ' @param pad If \code{TRUE}, pad the ecdf with additional points (-Inf, 0)
9
+ # ' and (Inf, 1)
8
10
# ' @section Computed variables:
9
11
# ' \describe{
10
12
# ' \item{x}{x in data}
22
24
# ' ggplot(df, aes(x, colour = g)) + stat_ecdf()
23
25
# ' }
24
26
stat_ecdf <- function (mapping = NULL , data = NULL , geom = " step" ,
25
- position = " identity" , n = NULL , na.rm = FALSE ,
27
+ position = " identity" , n = NULL ,
28
+ pad = TRUE , na.rm = FALSE ,
26
29
show.legend = NA , inherit.aes = TRUE , ... ) {
27
30
layer(
28
31
data = data ,
@@ -46,31 +49,20 @@ stat_ecdf <- function(mapping = NULL, data = NULL, geom = "step",
46
49
# ' @usage NULL
47
50
# ' @export
48
51
StatEcdf <- ggproto(" StatEcdf" , Stat ,
49
- compute_group = function (data , scales , n = NULL ) {
50
-
52
+ compute_group = function (data , scales , n = NULL , pad = TRUE ) {
51
53
# If n is NULL, use raw values; otherwise interpolate
52
54
if (is.null(n )) {
53
- xvals <- unique(data $ x )
55
+ x <- unique(data $ x )
54
56
} else {
55
- xvals <- seq(min(data $ x ), max(data $ x ), length.out = n )
57
+ x <- seq(min(data $ x ), max(data $ x ), length.out = n )
56
58
}
57
59
58
- y <- ecdf(data $ x )(xvals )
59
-
60
- # make point with y = 0, from plot.stepfun
61
- rx <- range(xvals )
62
- if (length(xvals ) > 1L ) {
63
- dr <- max(0.08 * diff(rx ), median(diff(xvals )))
64
- } else {
65
- dr <- abs(xvals )/ 16
60
+ if (pad ) {
61
+ x <- c(- Inf , x , Inf )
66
62
}
63
+ y <- ecdf(data $ x )(x )
67
64
68
- x0 <- rx [1 ] - dr
69
- x1 <- rx [2 ] + dr
70
- y0 <- 0
71
- y1 <- 1
72
-
73
- data.frame (x = c(x0 , xvals , x1 ), y = c(y0 , y , y1 ))
65
+ data.frame (x = x , y = y )
74
66
},
75
67
76
68
default_aes = aes(y = ..y.. ),
0 commit comments