Skip to content

geom_pointdensity

geom_pointdensity(
    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,
    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

Scatter plot with each point colored by the density of points around it.

As geom_pointdensity in the R package ggpointdensity: a point layer over stat="pointdensity", with color mapped to ..density... Useful for dense scatter plots such as UMAP embeddings, where overplotting hides how many points sit in a region.

Parameters:

  • mapping (FeatureSpec, default: None ) –

    Aesthetic mapping from aes(). Needs x and y. Map color to "..ndensity.." for densities scaled to 1 per group.

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

    Layer data; inherits the plot data when None.

  • stat (str, default: None ) –

    Statistical transformation, by name; "pointdensity" by default.

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

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

    This layer's key in a manual legend: a label, or layer_key().

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

    "neighbors" counts the points (the point itself included) within an ellipse around each point whose size follows the x and y ranges. "kde2d" reads a 2D Gaussian kernel density estimate (MASS::kde2d, 100 x 100 grid) at each point. "auto" uses "kde2d" when a group has more than 20000 rows, as ggpointdensity does, else "neighbors".

  • adjust (float, default: 1 ) –

    Multiplies the neighbour radius ("neighbors") or the bandwidth ("kde2d"). Larger values give smoother densities.

  • 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 (size, alpha, shape, ...). A constant color replaces the density coloring.

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 *
>>> import numpy as np
>>> generator = np.random.default_rng(1)
>>> data = {"height": generator.normal(170, 8, 2000),
...         "weight": generator.normal(70, 10, 2000)}
>>> plot = (ggplot(data, aes("height", "weight")) + geom_pointdensity(size=1.5)
...         + scale_color_viridis())