Generate outcome draws from a smooth fit. This stat is similar to stat_smooth(), but there are a few important differences. First, there is no method argument. Only smooth fits fitted via mgcv::gam() are currently supported. If you want a linear fit, set a linear formula via formula = y ~ x. Second, there is no se argument. This stat cannot draw confidence bands. See confidence_band() for a workaround if you want to add confidence bands. Internally, the stat uses the function sample_outcomes() to calculate outcomes.

stat_smooth_draws(mapping = NULL, times = 10, data = NULL,
geom = "smooth", position = "identity", ..., formula = y ~ s(x, bs
= "cs"), n = 80, fullrange = FALSE, gam.args = list(method =
"REML"), na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)

## Arguments

mapping Set of aesthetic mappings created by aes() or aes_(). If specified and inherit.aes = TRUE (the default), it is combined with the default mapping at the top level of the plot. You must supply mapping if there is no plot mapping. Number of outcomes to draw. The data to be displayed in this layer. There are three options: If NULL, the default, the data is inherited from the plot data as specified in the call to ggplot(). A data.frame, or other object, will override the plot data. All objects will be fortified to produce a data frame. See fortify() for which variables will be created. A function will be called with a single argument, the plot data. The return value must be a data.frame, and will be used as the layer data. Use to override the default connection between geom_smooth() and stat_smooth(). Position adjustment, either as a string, or the result of a call to a position adjustment function. Other arguments passed on to layer(). These are often aesthetics, used to set an aesthetic to a fixed value, like colour = "red" or size = 3. They may also be parameters to the paired geom/stat. Formula to use in smoothing function. Default is a cubic spline, y ~ s(x, bs = "cs"). To generate a linear fit, set formula = y ~ x. Number of points at which to evaluate smoother. Should the fit span the full range of the plot, or just the data? List of additional arguments passed on to the GAM call. If FALSE, the default, missing values are removed with a warning. If TRUE, missing values are silently removed. logical. Should this layer be included in the legends? NA, the default, includes if any aesthetics are mapped. FALSE never includes, and TRUE always includes. It can also be a named logical vector to finely select the aesthetics to display. If FALSE, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions that define both data and aesthetics and shouldn't inherit behaviour from the default plot specification, e.g. borders().

## Details

This stat fits the gam with Restricted Maximum Likelihood (REML) and uses the smoothing parameter uncertainty corrected covariance matrix to generate outcomes (unconditional = TRUE in sample_outcomes()). If you choose a different gam fitting method the stat sets unconditional = FALSE.

Note that for static plots, you will generally have to set the group aesthetic appropriately (e.g., aes(group = stat(.draw))). However, for animated plots you will normally not want to set the group aesthetic in this way. To enable animations by default, stat_smooth_draws() does not set a group aesthetic. See examples for further details.

## Examples

library(ggplot2)

# static plots, need to set group aesthetic manually
ggplot(mtcars, aes(hp, mpg)) +
geom_point() +
stat_smooth_draws(aes(group = stat(.draw)), size = 0.5) +
theme_bw()
# if we want to group by multiple variables, we have to use their
# mapped name (here, colour instead of Species) because we're
# creating the groups after after initial data mapping
ggplot(iris, aes(Sepal.Length, Sepal.Width, colour = Species)) +
geom_point() +
stat_smooth_draws(
formula = y ~ x,
aes(group = interaction(stat(.draw), colour)),
size = 0.5
) +
theme_bw()
# NOT RUN {
# animated plots
library(gganimate)

ggplot(mtcars, aes(hp, mpg)) +
geom_point() +
stat_smooth_draws(size = 0.5) +
transition_states(stat(.draw), 1, 2)

ggplot(iris, aes(Sepal.Length, Sepal.Width, colour = Species)) +
geom_point() +
stat_smooth_draws(formula = y ~ x, times = 20, size = 0.5) +
transition_states(stat(.draw), 1, 2)
# }