1#' Quantile regression
2#'
3#' This fits a quantile regression to the data and draws the fitted quantiles
4#' with lines. This is as a continuous analogue to [geom_boxplot()].
5#'
6#' @eval rd_aesthetics("geom", "quantile")
7#' @export
8#' @inheritParams layer
9#' @inheritParams geom_point
10#' @inheritParams geom_path
11#' @param method.args List of additional arguments passed on to the modelling
12#'   function defined by `method`.
13#' @param geom,stat Use to override the default connection between
14#'   `geom_quantile()` and `stat_quantile()`.
15#' @examples
16#' m <-
17#'   ggplot(mpg, aes(displ, 1 / hwy)) +
18#'   geom_point()
19#' m + geom_quantile()
20#' m + geom_quantile(quantiles = 0.5)
21#' q10 <- seq(0.05, 0.95, by = 0.05)
22#' m + geom_quantile(quantiles = q10)
23#'
24#' # You can also use rqss to fit smooth quantiles
25#' m + geom_quantile(method = "rqss")
26#' # Note that rqss doesn't pick a smoothing constant automatically, so
27#' # you'll need to tweak lambda yourself
28#' m + geom_quantile(method = "rqss", lambda = 0.1)
29#'
30#' # Set aesthetics to fixed value
31#' m + geom_quantile(colour = "red", size = 2, alpha = 0.5)
32geom_quantile <- function(mapping = NULL, data = NULL,
33                          stat = "quantile", position = "identity",
34                          ...,
35                          lineend = "butt",
36                          linejoin = "round",
37                          linemitre = 10,
38                          na.rm = FALSE,
39                          show.legend = NA,
40                          inherit.aes = TRUE) {
41
42  layer(
43    data = data,
44    mapping = mapping,
45    stat = stat,
46    geom = GeomQuantile,
47    position = position,
48    show.legend = show.legend,
49    inherit.aes = inherit.aes,
50    params = list(
51      lineend = lineend,
52      linejoin = linejoin,
53      linemitre = linemitre,
54      na.rm = na.rm,
55      ...
56    )
57  )
58}
59
60#' @rdname ggplot2-ggproto
61#' @format NULL
62#' @usage NULL
63#' @export
64#' @include geom-path.r
65GeomQuantile <- ggproto("GeomQuantile", GeomPath,
66  default_aes = defaults(
67    aes(weight = 1, colour = "#3366FF", size = 0.5),
68    GeomPath$default_aes
69  )
70)
71