Skip to content
This repository
Newer
Older
100644 105 lines (93 sloc) 3.559 kb
953fe471 »
2010-12-31 Convert all documentation to roxygen
1 #' Continuous quantiles.
2 #'
1244716e »
2014-02-24 Trim whitespace
3 #' @section Aesthetics:
d7971cbf »
2012-05-23 Fix document aesthetics.
4 #' \Sexpr[results=rd,stage=build]{ggplot2:::rd_aesthetics("stat", "quantile")}
5 #'
953fe471 »
2010-12-31 Convert all documentation to roxygen
6 #' @param quantiles conditional quantiles of y to calculate and display
7 #' @param formula formula relating y variables to x variables
81c34838 »
2011-12-28 Complete documentation of stats
8 #' @param method Quantile regression method to use. Currently only supports
9 #' \code{\link[quantreg]{rq}}.
171b6ed5 »
2011-12-29 Document stat_* na.rm
10 #' @param na.rm If \code{FALSE} (the default), removes missing values with
11 #' a warning. If \code{TRUE} silently removes missing values.
35c2a79a »
2011-12-28 Inherit stat parameters
12 #' @inheritParams stat_identity
953fe471 »
2010-12-31 Convert all documentation to roxygen
13 #' @return a data.frame with additional columns:
14 #' \item{quantile}{quantile of distribution}
15 #' @export
16 #' @examples
c5b872ed »
2012-01-25 Don't test any example that takes over 5 seconds
17 #' \donttest{
953fe471 »
2010-12-31 Convert all documentation to roxygen
18 #' msamp <- movies[sample(nrow(movies), 1000), ]
7def884b »
2012-08-03 Loop over taus in stat_quantile.
19 #' m <- ggplot(msamp, aes(year, rating)) + geom_point()
953fe471 »
2010-12-31 Convert all documentation to roxygen
20 #' m + stat_quantile()
21 #' m + stat_quantile(quantiles = 0.5)
7def884b »
2012-08-03 Loop over taus in stat_quantile.
22 #' q10 <- seq(0.05, 0.95, by=0.05)
23 #' m + stat_quantile(quantiles = q10)
1244716e »
2014-02-24 Trim whitespace
24 #'
7def884b »
2012-08-03 Loop over taus in stat_quantile.
25 #' # You can also use rqss to fit smooth quantiles
26 #' m + stat_quantile(method = "rqss")
1244716e »
2014-02-24 Trim whitespace
27 #' # Note that rqss doesn't pick a smoothing constant automatically, so
7def884b »
2012-08-03 Loop over taus in stat_quantile.
28 #' # you'll need to tweak lambda yourself
29 #' m + stat_quantile(method = "rqss", lambda = 10)
30 #' m + stat_quantile(method = "rqss", lambda = 100)
1244716e »
2014-02-24 Trim whitespace
31 #'
d5a97f6f » wch
2012-09-14 stat_quantile: documentation tweak
32 #' # Use 'votes' as weights for the quantile calculation
953fe471 »
2010-12-31 Convert all documentation to roxygen
33 #' m + stat_quantile(aes(weight=votes))
1244716e »
2014-02-24 Trim whitespace
34 #'
953fe471 »
2010-12-31 Convert all documentation to roxygen
35 #' # Change scale
7def884b »
2012-08-03 Loop over taus in stat_quantile.
36 #' m + stat_quantile(aes(colour = ..quantile..), quantiles = q10)
37 #' m + stat_quantile(aes(colour = ..quantile..), quantiles = q10) +
38 #' scale_colour_gradient2(midpoint = 0.5)
1244716e »
2014-02-24 Trim whitespace
39 #'
953fe471 »
2010-12-31 Convert all documentation to roxygen
40 #' # Set aesthetics to fixed value
7def884b »
2012-08-03 Loop over taus in stat_quantile.
41 #' m + stat_quantile(colour = "red", size = 2, linetype = 2)
1244716e »
2014-02-24 Trim whitespace
42 #'
953fe471 »
2010-12-31 Convert all documentation to roxygen
43 #' # Use qplot instead
44 #' qplot(year, rating, data=movies, geom="quantile")
c5b872ed »
2012-01-25 Don't test any example that takes over 5 seconds
45 #' }
1244716e »
2014-02-24 Trim whitespace
46 stat_quantile <- function (mapping = NULL, data = NULL, geom = "quantile", position = "identity",
47 quantiles = c(0.25, 0.5, 0.75), formula = NULL, method = "rq",
48 na.rm = FALSE, ...) {
49 StatQuantile$new(mapping = mapping, data = data, geom = geom,
50 position = position, quantiles = quantiles, formula = formula,
ede677cb » JakeRuss
2011-06-28 Removed xxx.r (build_accessor file) and @name tags, added functions t…
51 method = method, na.rm = na.rm, ...)
52 }
53
61984574 »
2007-11-06 Add ggplot
54 StatQuantile <- proto(Stat, {
46c1796d »
2011-01-04 Fix loading problems
55 objname <- "quantile"
56
61984574 »
2007-11-06 Add ggplot
57 default_geom <- function(.) GeomQuantile
b007f85a »
2010-01-05 Correct grouping for stat_quantile results
58 default_aes <- function(.) aes()
aea9a351 »
2008-01-03 Improving documentation
59 required_aes <- c("x", "y")
61984574 »
2007-11-06 Add ggplot
60
7def884b »
2012-08-03 Loop over taus in stat_quantile.
61 calculate <- function(., data, scales, quantiles = c(0.25, 0.5, 0.75),
62 formula = NULL, xseq = NULL, method = "rq", lambda = 1, na.rm = FALSE,
63 ...) {
64
61984574 »
2007-11-06 Add ggplot
65 try_require("quantreg")
1244716e »
2014-02-24 Trim whitespace
66
7def884b »
2012-08-03 Loop over taus in stat_quantile.
67 if (is.null(formula)) {
68 if (method == "rqss") {
1508269c » wch
2012-09-14 stat_quantile: Add try_require for MatrixModels when using rqss
69 try_require("MatrixModels")
1244716e »
2014-02-24 Trim whitespace
70 formula <- eval(substitute(y ~ qss(x, lambda = lambda)),
7def884b »
2012-08-03 Loop over taus in stat_quantile.
71 list(lambda = lambda))
72 } else {
73 formula <- y ~ x
74 }
75 message("Smoothing formula not specified. Using: ",
76 deparse(formula))
77 }
1244716e »
2014-02-24 Trim whitespace
78
79 if (is.null(data$weight)) data$weight <- 1
61984574 »
2007-11-06 Add ggplot
80
7def884b »
2012-08-03 Loop over taus in stat_quantile.
81 if (is.null(xseq)) {
82 xmin <- min(data$x, na.rm = TRUE)
83 xmax <- max(data$x, na.rm = TRUE)
84 xseq <- seq(xmin, xmax, length = 100)
85 }
86 grid <- data.frame(x = xseq)
61984574 »
2007-11-06 Add ggplot
87
88 data <- as.data.frame(data)
6180ce06 »
2008-02-24 Improving documentation and general tidying up
89 data <- remove_missing(data, na.rm, c("x", "y"), name = "stat_quantile")
c6918feb »
2009-06-11 Refer to rq by name
90 method <- match.fun(method)
1244716e »
2014-02-24 Trim whitespace
91
92 ldply(quantiles, quant_pred, data = data, method = method,
7def884b »
2012-08-03 Loop over taus in stat_quantile.
93 formula = formula, weight = weight, grid = grid, ...)
61984574 »
2007-11-06 Add ggplot
94 }
95 })
7def884b »
2012-08-03 Loop over taus in stat_quantile.
96
97 quant_pred <- function(quantile, data, method, formula, weight, grid, ...) {
98 model <- method(formula, data = data, tau = quantile, weight = weight, ...)
1244716e »
2014-02-24 Trim whitespace
99
7def884b »
2012-08-03 Loop over taus in stat_quantile.
100 grid$y <- predict(model, newdata = grid)
101 grid$quantile <- quantile
102 grid$group <- paste(data$group[1], quantile, sep = "-")
1244716e »
2014-02-24 Trim whitespace
103
7def884b »
2012-08-03 Loop over taus in stat_quantile.
104 grid
105 }
Something went wrong with that request. Please try again.