Skip to content
This repository
Newer
Older
100644 57 lines (48 sloc) 1.528 kb
646e5ed0 » kohske
2012-04-20 First commit of stat-ecdf (ref #508).
1 #' Empirical Cumulative Density Function
1244716e »
2014-02-24 Trim whitespace
2 #'
646e5ed0 » kohske
2012-04-20 First commit of stat-ecdf (ref #508).
3 #' @inheritParams stat_identity
e1dc7022 » wch
2012-06-27 stat_ecdf: code cleanup
4 #' @param n if NULL, do not interpolate. If not NULL, this is the number
5 #' of points to interpolate with.
646e5ed0 » kohske
2012-04-20 First commit of stat-ecdf (ref #508).
6 #' @return a data.frame with additional columns:
9df7632d » kohske
2012-04-21 Doc tweak for stat-ecdf.
7 #' \item{x}{x in data}
646e5ed0 » kohske
2012-04-20 First commit of stat-ecdf (ref #508).
8 #' \item{y}{cumulative density corresponding x}
9 #' @export
10 #' @examples
11 #' \donttest{
6fdb32d9 » wch
2012-06-25 ecdf: Clean up example
12 #' qplot(rnorm(1000), stat = "ecdf", geom = "step")
1244716e »
2014-02-24 Trim whitespace
13 #'
646e5ed0 » kohske
2012-04-20 First commit of stat-ecdf (ref #508).
14 #' df <- data.frame(x = c(rnorm(100, 0, 3), rnorm(100, 0, 10)),
15 #' g = gl(2, 100))
1244716e »
2014-02-24 Trim whitespace
16 #'
646e5ed0 » kohske
2012-04-20 First commit of stat-ecdf (ref #508).
17 #' ggplot(df, aes(x, colour = g)) + stat_ecdf()
18 #' }
e01e84aa » kohske
2012-06-27 stat-ecdf: doesn't interpolate by default.
19 stat_ecdf <- function (mapping = NULL, data = NULL, geom = "step", position = "identity", n = NULL, ...) {
646e5ed0 » kohske
2012-04-20 First commit of stat-ecdf (ref #508).
20 StatEcdf$new(mapping = mapping, data = data, geom = geom, position = position, n = n, ...)
21 }
22
23 StatEcdf <- proto(Stat, {
24 objname <- "ecdf"
1244716e »
2014-02-24 Trim whitespace
25
e01e84aa » kohske
2012-06-27 stat-ecdf: doesn't interpolate by default.
26 calculate <- function(., data, scales, n = NULL, ...) {
859350fa » wch
2012-06-25 ecdf: interpolate when more than n points
27
e1dc7022 » wch
2012-06-27 stat_ecdf: code cleanup
28 # If n is NULL, use raw values; otherwise interpolate
29 if (is.null(n)) {
30 xvals <- unique(data$x)
31 } else {
32 xvals <- seq(min(data$x), max(data$x), length.out = n)
33 }
859350fa » wch
2012-06-25 ecdf: interpolate when more than n points
34
e1dc7022 » wch
2012-06-27 stat_ecdf: code cleanup
35 y <- ecdf(data$x)(xvals)
859350fa » wch
2012-06-25 ecdf: interpolate when more than n points
36
a6074e3d » kohske
2012-06-27 stat-ecdf: ecdf Y ranges from 0 to 1.
37 # make point with y = 0, from plot.stepfun
38 rx <- range(xvals)
e1dc7022 » wch
2012-06-27 stat_ecdf: code cleanup
39 if (length(xvals) > 1L) {
40 dr <- max(0.08 * diff(rx), median(diff(xvals)))
41 } else {
42 dr <- abs(xvals)/16
43 }
a6074e3d » kohske
2012-06-27 stat-ecdf: ecdf Y ranges from 0 to 1.
44
45 x0 <- rx[1] - dr
46 x1 <- rx[2] + dr
47 y0 <- 0
48 y1 <- 1
4dde7ee2 » kohske
2012-06-27 stat-ecdf: remove duplicate x.
49
a6074e3d » kohske
2012-06-27 stat-ecdf: ecdf Y ranges from 0 to 1.
50 data.frame(x = c(x0, xvals, x1), y = c(y0, y, y1))
646e5ed0 » kohske
2012-04-20 First commit of stat-ecdf (ref #508).
51 }
52
53 default_aes <- function(.) aes(y = ..y..)
54 required_aes <- c("x")
66fed10e » wch
2012-06-25 Tweaks to stat_ecdf
55 default_geom <- function(.) GeomStep
859350fa » wch
2012-06-25 ecdf: interpolate when more than n points
56
646e5ed0 » kohske
2012-04-20 First commit of stat-ecdf (ref #508).
57 })
58
Something went wrong with that request. Please try again.