Skip to contents

mark_quantile() fits a quantile regression of y on x per group for each of quantiles and draws the fitted lines (ggplot2's geom_quantile()). The fit is quantreg::rq() of formula (default y ~ x), predicted on 100 points across the group's range of x. Map color = after_stat(factor(quantile)) (or linetype) to tell the lines apart. Needs the quantreg package.

Usage

mark_quantile(
  plot,
  ...,
  quantiles = c(0.25, 0.5, 0.75),
  formula = NULL,
  method.args = list(),
  position = "identity",
  blend = NULL,
  effects = list(),
  sketch = NULL,
  data = NULL
)

Arguments

plot

A PlotSpec (from vplot()).

...

Encodings (tidy-eval): x, y (+ color, group, linewidth, linetype, alpha).

quantiles

The conditional quantiles to fit (default c(0.25, 0.5, 0.75)).

formula

The model formula in x and y (default y ~ x), e.g. y ~ splines::ns(x, 3) for a curve.

method.args

A list of further arguments to quantreg::rq().

position

Position adjustment: a string — "identity" (default), "nudge", "stack", "fill", "dodge", "dodge2", "jitter", "jitterdodge", "sina" — or a parameterised position object such as position_dodge() or position_stack(). Every mark takes "identity" and "nudge". "stack"/"fill" work on bars, areas, points, text and labels (so a label can sit on its stacked bar); "dodge"/"dodge2" on bars, points, text, labels, error bars, line/point ranges, cross-bars, box plots, violins and slab-intervals (so error bars dodge over dodged bars); "jitter" on points, text and labels; "jitterdodge" and "sina" on points. A position a mark does not support is an error naming the ones it does.

blend

Optional blend mode for compositing this layer against what is already drawn beneath it (the panel and earlier layers), one of the CSS mix-blend-mode names, e.g. "multiply", "screen", "darken". The whole layer composites as one isolated group (not per element).

effects

A list of layer render effects applied to the mark at draw time — glow(), outline(), and shadow(). Available on stroked and point marks.

sketch

A sketch() spec giving this layer a hand-drawn look (wobbly outlines, hachure fills), NA/FALSE to force it crisp (overriding a plot-wide theme_sketch()), or NULL (default) to inherit. Geometry marks accept it; text, raster, hex and datashade marks do not.

data

Optional layer data frame; overrides the plot data for this layer.

Value

The modified PlotSpec.

See also

mark_smooth() with method = "rq" for one quantile with the smoother's options.

Examples

# \donttest{
if (requireNamespace("quantreg", quietly = TRUE)) {
  vplot(mtcars) |>
    mark_point(x = disp, y = mpg) |>
    mark_quantile(x = disp, y = mpg)
}

# }