Skip to content

Commit c59b650

Browse files
committed
method.args for stat_quantile. Fixes #1289
1 parent f809b7b commit c59b650

File tree

4 files changed

+24
-12
lines changed

4 files changed

+24
-12
lines changed

NEWS

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ ggplot2 1.0.1.9xxx
6363
* `geom_smooth()` is no longer so chatty. If you want to know what the deafult
6464
smoothing method is, look it up in the documentation! (#1247)
6565

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

6970
* `element_text()` gains a margins argument which allows you to add additional
7071
margins around text elements in the plot. The default themes have been

R/geom-quantile.r

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#' @export
99
#' @inheritParams geom_point
1010
#' @inheritParams geom_path
11+
#' @param method.args List of additional arguments passed on to the modelling
12+
#' function defined by \code{method}.
1113
#' @param geom,stat Use to override the default connection between
1214
#' \code{geom_quantile} and \code{stat_quantile}.
1315
#' @examples
@@ -21,7 +23,7 @@
2123
#' m + geom_quantile(method = "rqss")
2224
#' # Note that rqss doesn't pick a smoothing constant automatically, so
2325
#' # you'll need to tweak lambda yourself
24-
#' m + geom_quantile(method = "rqss", lambda = 1)
26+
#' m + geom_quantile(method = "rqss", lambda = 0.1)
2527
#'
2628
#' # Set aesthetics to fixed value
2729
#' m + geom_quantile(colour = "red", size = 2, alpha = 0.5)

R/stat-quantile.r

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
#' @rdname geom_quantile
1414
stat_quantile <- function(mapping = NULL, data = NULL, geom = "quantile",
1515
position = "identity", quantiles = c(0.25, 0.5, 0.75),
16-
formula = NULL, method = "rq", na.rm = FALSE,
17-
show.legend = NA, inherit.aes = TRUE, ...) {
16+
formula = NULL, method = "rq", method.args = list(),
17+
na.rm = FALSE, show.legend = NA, inherit.aes = TRUE,
18+
...) {
1819
layer(
1920
data = data,
2021
mapping = mapping,
@@ -27,6 +28,7 @@ stat_quantile <- function(mapping = NULL, data = NULL, geom = "quantile",
2728
quantiles = quantiles,
2829
formula = formula,
2930
method = method,
31+
method.args = method.args,
3032
na.rm = na.rm
3133
),
3234
params = list(...)
@@ -43,7 +45,8 @@ StatQuantile <- ggproto("StatQuantile", Stat,
4345

4446
compute_group = function(data, scales, quantiles = c(0.25, 0.5, 0.75),
4547
formula = NULL, xseq = NULL, method = "rq",
46-
lambda = 1, na.rm = FALSE, ...) {
48+
method.args = list(), lambda = 1, na.rm = FALSE,
49+
...) {
4750
try_require("quantreg", "stat_quantile")
4851

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

7275
plyr::ldply(quantiles, quant_pred, data = data, method = method,
73-
formula = formula, weight = weight, grid = grid, ...)
76+
formula = formula, weight = weight, grid = grid, method.args = method.args)
7477
}
7578
)
7679

77-
quant_pred <- function(quantile, data, method, formula, weight, grid, ...) {
78-
model <- method(formula, data = data, tau = quantile, weights = weight, ...)
80+
quant_pred <- function(quantile, data, method, formula, weight, grid,
81+
method.args = method.args) {
82+
args <- c(list(quote(formula), data = quote(data), tau = quote(quantile),
83+
weights = quote(weight)), method.args)
84+
model <- do.call(method, args)
7985

8086
grid$y <- stats::predict(model, newdata = grid)
8187
grid$quantile <- quantile

man/geom_quantile.Rd

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ geom_quantile(mapping = NULL, data = NULL, stat = "quantile",
1212

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

6767
\item{method}{Quantile regression method to use. Currently only supports
6868
\code{\link[quantreg]{rq}}.}
69+
70+
\item{method.args}{List of additional arguments passed on to the modelling
71+
function defined by \code{method}.}
6972
}
7073
\description{
7174
This can be used as a continuous analogue of a geom_boxplot.
@@ -92,7 +95,7 @@ m + geom_quantile(quantiles = q10)
9295
m + geom_quantile(method = "rqss")
9396
# Note that rqss doesn't pick a smoothing constant automatically, so
9497
# you'll need to tweak lambda yourself
95-
m + geom_quantile(method = "rqss", lambda = 1)
98+
m + geom_quantile(method = "rqss", lambda = 0.1)
9699

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

0 commit comments

Comments
 (0)