Skip to content

geom_density

geom_density(
    mapping: FeatureSpec | None = None,
    *,
    data: PlotData | None = None,
    stat: str | None = None,
    position: Position | None = None,
    show_legend: bool | None = None,
    inherit_aes: bool | None = None,
    manual_key: ManualKey | None = None,
    sampling: Sampling | 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,
    quantile_lines: bool | None = None,
    color_by: str | None = None,
    fill_by: str | None = None,
    na_rm: bool | None = None,
    **other_args: Any,
) -> LayerSpec

Kernel density estimate of x, drawn as a filled area (stat="density").

One density per group. The computed variables are ..density.. (y by default), ..count.. (density x total weight, which is the number of rows without weight), ..scaled.. (density / its maximum) and ..quantile... Bandwidths follow R's bw.nrd0 and bw.nrd.

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.

  • stat (str, default: None ) –

    Statistical transformation, 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.

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

    A legend key for this layer, as in lets_plot; not supported yet (ggrust warns).

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

    lets_plot sampling; accepted and ignored, as ggrust draws every row.

  • 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; not supported yet (ggrust warns).

  • 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].

  • quantile_lines (bool, default: False ) –

    True draws a line from the curve down to the base at each quantile.

  • 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) –

    Constant aesthetics such as fill="steelblue" or alpha=0.5, as in lets_plot.

Returns:

  • LayerSpec –

    The layer.

Examples:

>>> from ggrust import *
>>> frame = {"height": [160, 165, 170, 172, 175, 180, 185], "sex": list("FFFMMMM")}
>>> plot = ggplot(frame, aes("height", fill="sex")) + geom_density(alpha=0.4)