Skip to content

stat_centroid

stat_centroid(
    mapping: FeatureSpec | None = None,
    *,
    data: PlotData | None = None,
    geom: str | None = None,
    position: Position | None = None,
    show_legend: bool | None = False,
    inherit_aes: bool | None = None,
    tooltips: Tooltips | None = None,
    fun: Literal | None = None,
    na_rm: bool | None = None,
    **other_args: Any,
) -> LayerSpec

One row per group at the centroid of its x and y (stat="centroid"), as ggpp.

Each group (and facet panel) becomes one row at fun of its x and of its y, over the rows where both are finite. Other mapped columns keep one value per group, so map label (or color) to the column that defines the groups. Also usable as geom_text(stat="centroid") or geom_label(stat="centroid"). Positions are summarised in axis units, as ggplot2: on a log axis the mean is geometric.

Parameters:

  • mapping (FeatureSpec, default: None ) –

    Aesthetic mapping from aes(): x, y (required), and the geom's own, such as label or color.

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

    Layer data; inherits the plot data when None.

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

    Geom that draws the centres, e.g. "text" or "label".

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

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

  • show_legend (bool, default: False ) –

    True adds this layer to the legend (ggpp leaves it out).

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

  • fun ((mean, median), default: "mean" ) –

    Summary of x and of y. "median" resists outlying cells.

  • 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 size=6, and the geom's options.

Returns:

  • LayerSpec –

    The layer.

Examples:

>>> from ggrust import *
>>> import numpy as np
>>> rng = np.random.default_rng(1)
>>> centres = {"apples": (0, 0), "pears": (4, 1), "plums": (2, 4)}
>>> fruit = [name for name in centres for _ in range(100)]
>>> data = {
...     "width": [centres[name][0] + rng.normal() for name in fruit],
...     "height": [centres[name][1] + rng.normal() for name in fruit],
...     "fruit": fruit,
... }
>>> plot = (
...     ggplot(data, aes("width", "height"))
...     + geom_point(aes(color="fruit"), alpha=0.4, show_legend=False)
...     + stat_centroid(aes(label="fruit"), geom="label", fun="median", size=6)
... )