Skip to content

stat_smooth

stat_smooth(
    mapping: FeatureSpec | None = None,
    *,
    data: PlotData | None = None,
    geom: str | None = None,
    position: Position | None = None,
    show_legend: bool | None = None,
    inherit_aes: bool | None = None,
    tooltips: Tooltips | None = None,
    orientation: str | None = None,
    method: Literal["lm", "loess", "lowess"] | None = None,
    n: int | None = None,
    se: bool | None = None,
    level: Number | None = None,
    span: Number | None = None,
    deg: int | None = None,
    seed: int | None = None,
    max_n: int | None = None,
    fullrange: bool | None = None,
    formula: str | None = None,
    method_args: Mapping[str, Number] | None = None,
    color_by: str | None = None,
    fill_by: str | None = None,
    na_rm: bool | None = None,
    **other_args: Any,
) -> FeatureSpec

Fitted curve with a confidence band (stat="smooth"); drawn as a smooth by default.

Computes x, y, ymin, ymax and ..se...

stat_smooth(...) is geom_smooth(stat="smooth", ...); another geom draws the same computed rows with that geom.

Parameters:

  • mapping (FeatureSpec, default: None ) –

    Aesthetic mapping from aes().

  • data (DataFrame or dict, default: None ) –

    Layer data; inherits the plot data when None.

  • geom (str, default: None ) –

    Geom that draws the computed rows, by name; "smooth" when None.

  • position (str or FeatureSpec, default: None ) –

    Position adjustment: a name such as "dodge" or a position_*() spec.

  • show_legend (bool, default: None ) –

    False hides this layer from the legend.

  • inherit_aes (bool, default: None ) –

    False ignores the plot's default mapping for this layer.

  • tooltips (FeatureSpec or list of str, default: None ) –

    Tooltip content: layer_tooltips(), a list of column names, or "none" to disable tooltips (HTML output).

  • orientation (str, default: None ) –

    "y" smooths x along y.

  • method ((lm, loess, lowess), default: "lm" ) –

    Fitting method: least squares ("lm") or local regression ("loess", "lowess").

  • n (int, default: 80 ) –

    Number of points the curve is evaluated at.

  • se (bool, default: True ) –

    False leaves out the confidence band.

  • level (float, default: 0.95 ) –

    Confidence level of the band, in [0.01, 0.99].

  • span (float, default: 0.5 ) –

    Share of the points each loess fit uses, in (0, 1].

  • deg (int, default: 1 ) –

    Degree of the "lm" polynomial; 2 or more fits a polynomial.

  • seed (int, default: 37 ) –

    Seed of the row sampling for loess on more than max_n rows.

  • max_n (int, default: 1000 ) –

    Loess fits a random sample of this many rows per group when it has more.

  • fullrange (bool, default: False ) –

    True draws the curve over the plot's whole x range, not only the group's data, as ggplot2: lines and polynomials extrapolate, loess stops at its data. Not in lets_plot.

  • formula (str, default: None ) –

    "y ~ x" or "y ~ poly(x, k)" (the same as deg=k, with method="lm"), as ggplot2. Not in lets_plot.

  • method_args (dict, default: None ) –

    Arguments of the method, as ggplot2's method.args: {"span": 0.3} for loess. Not in lets_plot.

  • color_by (str, default: None ) –

    Aesthetic the color scale applies to ("color" or "fill").

  • fill_by (str, default: None ) –

    Aesthetic the fill scale applies to ("fill" or "color").

  • na_rm (bool, default: None ) –

    True removes rows with missing values silently; otherwise they are removed with a warning.

  • **other_args (Any) –

    Options of the geom and constant aesthetics such as color="red".

Returns:

  • FeatureSpec –

    The layer.

Examples:

>>> from ggrust import *
>>> frame = {"hours": [1, 2, 3, 4, 5, 6], "score": [52, 55, 61, 64, 70, 71]}
>>> plot = ggplot(frame, aes("hours", "score")) + geom_point() + stat_smooth(se=False)