First commit of stat-ecdf (ref #508).

1 parent 5b0e9cb commit 646e5ed0fb0e9395b2c0c5beae539a2cf0123248 kohske committed with wch Apr 20, 2012
Showing with 94 additions and 0 deletions.
1. +2 −0 DESCRIPTION
2. +1 −0 NAMESPACE
3. +42 −0 R/stat-ecdf.r
4. +49 −0 man/stat_ecdf.Rd
2 DESCRIPTION
 @@ -177,6 +177,7 @@ Collate: 'stat-unique.r' 'stat-vline.r' 'stat-ydensity.r' + 'stat-ecdf.r' 'summary.r' 'templates.r' 'theme-defaults.r' @@ -206,3 +207,4 @@ Collate: 'translate-qplot-lattice.r' 'annotation-logticks.r' 'utilities-help.r' +
1 NAMESPACE
 @@ -363,6 +363,7 @@ export(stat_boxplot) export(stat_contour) export(stat_density) export(stat_density2d) +export(stat_ecdf) export(stat_function) export(stat_hline) export(stat_identity)
42 R/stat-ecdf.r
 @@ -0,0 +1,42 @@ +#' Empirical Cumulative Density Function +#' +#' @inheritParams stat_identity +#' @param n number of points to interpolate along +#' @return a data.frame with additional columns: +#' \item{x}{x's along a grid} +#' \item{y}{cumulative density corresponding x} +#' @export +#' @examples +#' \donttest{ +#' l(ggplot2, T) +#' set.seed(42) +#' x <- c(rnorm(1000)) +#' qplot(x, stat = "ecdf", geom = "step") +#' +#' df <- data.frame(x = c(rnorm(100, 0, 3), rnorm(100, 0, 10)), +#' g = gl(2, 100)) +#' +#' ggplot(df, aes(x, colour = g)) + stat_ecdf() +#' } +stat_ecdf <- function (mapping = NULL, data = NULL, geom = "step", position = "identity", n = 101, ...) { + StatEcdf$new(mapping = mapping, data = data, geom = geom, position = position, n = n, ...) +} + +StatEcdf <- proto(Stat, { + objname <- "ecdf" + + calculate <- function(., data, scales, n = 101, ...) { + range <- scale_dimension(scales$x) + f <- ecdf(data$x) + x <- sort(data$x) + y <- ecdf(data$x)(x) + data.frame(x = x, y = y) + } + + icon <- function(.) GeomPath$icon() + default_aes <- function(.) aes(y = ..y..) + required_aes <- c("x") + default_geom <- function(.) GeomPath + +}) +
49 man/stat_ecdf.Rd
 @@ -0,0 +1,49 @@ +\name{stat_ecdf} +\alias{stat_ecdf} +\title{Empirical Cumulative Density Function} +\usage{ + stat_ecdf(mapping = NULL, data = NULL, geom = "step", + position = "identity", n = 101, ...) +} +\arguments{ + \item{n}{number of points to interpolate along} + + \item{mapping}{The aesthetic mapping, usually constructed + with \code{\link{aes}} or \code{\link{aes_string}}. Only + needs to be set at the layer level if you are overriding + the plot defaults.} + + \item{data}{A layer specific dataset - only needed if you + want to override the plot defaults.} + + \item{geom}{The geometric object to use display the data} + + \item{position}{The position adjustment to use for + overlappling points on this layer} + + \item{...}{other arguments passed on to + \code{\link{layer}}. This can include aesthetics whose + values you want to set, not map. See \code{\link{layer}} + for more details.} +} +\value{ + a data.frame with additional columns: \item{x}{x's along + a grid} \item{y}{cumulative density corresponding x} +} +\description{ + Empirical Cumulative Density Function +} +\examples{ +\donttest{ +l(ggplot2, T) +set.seed(42) +x <- c(rnorm(1000)) +qplot(x, stat = "ecdf", geom = "step") + +df <- data.frame(x = c(rnorm(100, 0, 3), rnorm(100, 0, 10)), + g = gl(2, 100)) + +ggplot(df, aes(x, colour = g)) + stat_ecdf() +} +} +