Skip to content

stat_density

stat_density(
    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,
    trim: bool | None = None,
    kernel: str | None = None,
    adjust: Number | None = None,
    bw: str | Number | None = None,
    n: int | None = None,
    fs_max: Number | None = None,
    quantiles: Sequence[float] | None = None,
    bounds: Sequence[Number | None] | None = None,
    color_by: str | None = None,
    fill_by: str | None = None,
    na_rm: bool | None = None,
    **other_args: Any,
) -> FeatureSpec

Kernel density estimate of x (stat="density"); drawn as a density curve by default.

Computes ..density.., ..count.. and ..scaled...

stat_density(...) is geom_density(stat="density", ...); another geom draws the same computed rows with that geom.

Parameters:

  • mapping (FeatureSpec, default: None ) –

    Aesthetic mapping from aes(). weight weighs the rows.

  • 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; "density" when None.

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

    Position adjustment: a name such as "stack" or a position_*() spec; "identity" when None.

  • 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" estimates the density of y.

  • trim (bool, default: False ) –

    True evaluates the density over the group's data range only; otherwise over the x range of every layer.

  • kernel (str, default: "gaussian" ) –

    Kernel: "gaussian", "rectangular", "triangular", "biweight", "epanechikov", "optcosine" or "cosine".

  • adjust (float, default: 1 ) –

    Multiplier of the bandwidth.

  • bw (str or float, default: "nrd0" ) –

    Bandwidth: a positive number, or "nrd0" or "nrd".

  • n (int, default: 512 ) –

    Number of points the density is evaluated at (2 to 1024).

  • fs_max (float, default: 5000 ) –

    Above this many rows, only rows within 5 bandwidths of a point are summed.

  • quantiles (list of float, default: [0.25, 0.5, 0.75] ) –

    Quantiles that split the area into bands (..quantile..), each in [0, 1].

  • bounds (list of [float or None, float or None], default: None ) –

    Known lower and upper bounds of the data (None or inf for an open end), as ggplot2: values outside are dropped and the kernels are reflected at each finite bound, so the density does not leak past it. 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 *
>>> data = {"height": [150, 152, 158, 160, 161, 163, 165, 168, 170, 171, 175, 182]}
>>> plot = ggplot(data, aes("height")) + stat_density(geom="line", adjust=0.8)