Skip to content

stat_density2d

stat_density2d(
    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,
    kernel: str | None = None,
    adjust: float | None = None,
    bw: str | float | Sequence[float] | None = None,
    n: int | Sequence[int] | None = None,
    bins: int | None = None,
    binwidth: float | None = None,
    breaks: Sequence[float] | None = None,
    contour_var: Literal["density", "ndensity", "count"]
    | None = None,
    color_by: str | None = None,
    fill_by: str | None = None,
    na_rm: bool | None = None,
    **other_args: Any,
) -> FeatureSpec

Contours of a 2D kernel density estimate of x and y (stat="density2d").

Computes ..level...

stat_density2d(...) is geom_density2d(stat="density2d", ...); another geom draws the same computed rows with that geom.

Parameters:

  • mapping (FeatureSpec, default: None ) –

    Aesthetic mapping from aes(): x, y (required), color, alpha, size, linetype, weight.

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

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

    Position adjustment: a name such as "identity" 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).

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

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

  • adjust (float, default: 1 ) –

    Multiplies the bandwidth; above 1 smooths more.

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

    Bandwidth: a rule ("nrd0" or "nrd", per axis), one number for both axes, or [x, y].

  • n (int or list of int, default: 100 ) –

    Grid points along x and y (2 to 999); one number is used for both axes.

  • bins (int, default: 10 ) –

    Number of levels, evenly spaced over the z range.

  • binwidth (float, default: None ) –

    Distance between levels; wins over bins.

  • breaks (list of float, default: None ) –

    Explicit levels in the units of contour_var; win over bins and binwidth (ggplot2).

  • contour_var ((density, ndensity, count), default: "density" ) –

    What the levels are levels of: the density, the density divided by its maximum ("ndensity", comparable across groups and samples), or the density times the number of points ("count"). ggplot2's parameter; lets_plot has none.

  • 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 = {"x": [1, 2, 2, 3, 3, 3, 4, 4, 5], "y": [2, 1, 2, 3, 2, 3, 4, 3, 5]}
>>> plot = ggplot(data, aes("x", "y")) + stat_density2d(bins=5)