Skip to content

stat_pointdensity

stat_pointdensity(
    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,
    sampling: Sampling | None = None,
    tooltips: Tooltips | None = None,
    method: Literal | None = None,
    adjust: Number | None = None,
    color_by: str | None = None,
    fill_by: str | None = None,
    na_rm: bool | None = None,
    **other_args: Any,
) -> LayerSpec

Density of points around each point (stat="pointdensity"), drawn as points.

As stat_pointdensity in the R package ggpointdensity. Computes ..density.. (neighbour count or kernel density, mapped to color by default) and ..ndensity.. (density over the group's maximum) for every row.

Parameters:

  • mapping (FeatureSpec, default: None ) –

    Aesthetic mapping from aes(). Needs x and y.

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

    Layer data; inherits the plot data when None.

  • geom (str, default: "point" ) –

    Geom that draws the rows, e.g. "text" with aes(label="..density..").

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

    Position adjustment, applied after the densities are computed.

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

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

  • method ((auto, neighbors, kde2d), default: "auto" ) –

    See geom_pointdensity.

  • adjust (float, default: 1 ) –

    Multiplies the neighbour radius or the kernel bandwidth.

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

  • **other_args (Any) –

    Constant aesthetics and the geom's options.

Returns:

  • LayerSpec –

    The layer.

Raises:

  • SpecError –

    If method is not one of the accepted names or adjust is not positive.

  • UnsupportedValueError –

    If method is not a string or adjust is not a number.

Examples:

>>> from ggrust import *
>>> data = {"x": [1.0, 1.1, 1.2, 3.0, 3.1, 5.0], "y": [2.0, 2.1, 1.9, 4.0, 4.2, 1.0]}
>>> plot = ggplot(data, aes("x", "y")) + stat_pointdensity(aes(size="..ndensity.."))