Skip to content

Commit

Permalink
method.args for stat_quantile. Fixes #1289
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed Aug 26, 2015
1 parent f809b7b commit c59b650
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
5 changes: 3 additions & 2 deletions NEWS
Expand Up @@ -63,8 +63,9 @@ ggplot2 1.0.1.9xxx
* `geom_smooth()` is no longer so chatty. If you want to know what the deafult
smoothing method is, look it up in the documentation! (#1247)

* `geom_smooth()`/`stat_smooth()` no longer pass ... on to the
underlying model. Instead, put additional arguments in `method.args` (#1245).
* `geom_smooth()`/`stat_smooth()` and `geom_quantile()`/`stat_quantile()` no
longer pass ... on to the underlying model. Instead, put additional arguments
in `method.args` (#1245, #1289).

* `element_text()` gains a margins argument which allows you to add additional
margins around text elements in the plot. The default themes have been
Expand Down
4 changes: 3 additions & 1 deletion R/geom-quantile.r
Expand Up @@ -8,6 +8,8 @@
#' @export
#' @inheritParams geom_point
#' @inheritParams geom_path
#' @param method.args List of additional arguments passed on to the modelling
#' function defined by \code{method}.
#' @param geom,stat Use to override the default connection between
#' \code{geom_quantile} and \code{stat_quantile}.
#' @examples
Expand All @@ -21,7 +23,7 @@
#' m + geom_quantile(method = "rqss")
#' # Note that rqss doesn't pick a smoothing constant automatically, so
#' # you'll need to tweak lambda yourself
#' m + geom_quantile(method = "rqss", lambda = 1)
#' m + geom_quantile(method = "rqss", lambda = 0.1)
#'
#' # Set aesthetics to fixed value
#' m + geom_quantile(colour = "red", size = 2, alpha = 0.5)
Expand Down
18 changes: 12 additions & 6 deletions R/stat-quantile.r
Expand Up @@ -13,8 +13,9 @@
#' @rdname geom_quantile
stat_quantile <- function(mapping = NULL, data = NULL, geom = "quantile",
position = "identity", quantiles = c(0.25, 0.5, 0.75),
formula = NULL, method = "rq", na.rm = FALSE,
show.legend = NA, inherit.aes = TRUE, ...) {
formula = NULL, method = "rq", method.args = list(),
na.rm = FALSE, show.legend = NA, inherit.aes = TRUE,
...) {
layer(
data = data,
mapping = mapping,
Expand All @@ -27,6 +28,7 @@ stat_quantile <- function(mapping = NULL, data = NULL, geom = "quantile",
quantiles = quantiles,
formula = formula,
method = method,
method.args = method.args,
na.rm = na.rm
),
params = list(...)
Expand All @@ -43,7 +45,8 @@ StatQuantile <- ggproto("StatQuantile", Stat,

compute_group = function(data, scales, quantiles = c(0.25, 0.5, 0.75),
formula = NULL, xseq = NULL, method = "rq",
lambda = 1, na.rm = FALSE, ...) {
method.args = list(), lambda = 1, na.rm = FALSE,
...) {
try_require("quantreg", "stat_quantile")

if (is.null(formula)) {
Expand All @@ -70,12 +73,15 @@ StatQuantile <- ggproto("StatQuantile", Stat,
method <- match.fun(method)

plyr::ldply(quantiles, quant_pred, data = data, method = method,
formula = formula, weight = weight, grid = grid, ...)
formula = formula, weight = weight, grid = grid, method.args = method.args)
}
)

quant_pred <- function(quantile, data, method, formula, weight, grid, ...) {
model <- method(formula, data = data, tau = quantile, weights = weight, ...)
quant_pred <- function(quantile, data, method, formula, weight, grid,
method.args = method.args) {
args <- c(list(quote(formula), data = quote(data), tau = quote(quantile),
weights = quote(weight)), method.args)
model <- do.call(method, args)

grid$y <- stats::predict(model, newdata = grid)
grid$quantile <- quantile
Expand Down
9 changes: 6 additions & 3 deletions man/geom_quantile.Rd
Expand Up @@ -12,8 +12,8 @@ geom_quantile(mapping = NULL, data = NULL, stat = "quantile",

stat_quantile(mapping = NULL, data = NULL, geom = "quantile",
position = "identity", quantiles = c(0.25, 0.5, 0.75), formula = NULL,
method = "rq", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE,
...)
method = "rq", method.args = list(), na.rm = FALSE, show.legend = NA,
inherit.aes = TRUE, ...)
}
\arguments{
\item{mapping}{Set of aesthetic mappings created by \code{\link{aes}} or
Expand Down Expand Up @@ -66,6 +66,9 @@ the default plot specification, e.g. \code{\link{borders}}.}

\item{method}{Quantile regression method to use. Currently only supports
\code{\link[quantreg]{rq}}.}

\item{method.args}{List of additional arguments passed on to the modelling
function defined by \code{method}.}
}
\description{
This can be used as a continuous analogue of a geom_boxplot.
Expand All @@ -92,7 +95,7 @@ m + geom_quantile(quantiles = q10)
m + geom_quantile(method = "rqss")
# Note that rqss doesn't pick a smoothing constant automatically, so
# you'll need to tweak lambda yourself
m + geom_quantile(method = "rqss", lambda = 1)
m + geom_quantile(method = "rqss", lambda = 0.1)

# Set aesthetics to fixed value
m + geom_quantile(colour = "red", size = 2, alpha = 0.5)
Expand Down

0 comments on commit c59b650

Please sign in to comment.