Skip to content

stat_ellipse

stat_ellipse(
    mapping: FeatureSpec | None = None,
    *,
    data: PlotData | None = None,
    geom: Literal["path", "polygon"] | str | None = None,
    position: Position | None = None,
    show_legend: bool | None = None,
    inherit_aes: bool | None = None,
    tooltips: Tooltips | None = None,
    type: Literal | None = None,
    level: Number | None = None,
    segments: int | None = None,
    na_rm: bool | None = None,
    **other_args: Any,
) -> LayerSpec

Confidence ellipse of x and y per group (ggplot2 stat_ellipse).

lets-plot has none; this follows ggplot2, which fits the shape with MASS::cov.trob by default. A group needs at least 4 points; smaller groups, and groups whose points lie on a line, get no ellipse.

Parameters:

  • mapping (FeatureSpec, default: None ) –

    Aesthetic mapping from aes(); needs x and y. Map color, fill or group for one ellipse per group.

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

    Layer data; inherits the plot data when None.

  • geom ((path, polygon), default: "path" ) –

    Geom that draws the outline; "polygon" fills it.

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

    Position adjustment.

  • 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 (HTML output), or "none".

  • type ((t, norm, euclid), default: "t" ) –

    "t" fits a robust multivariate t distribution (MASS::cov.trob), "norm" a multivariate normal, "euclid" draws a circle whose radius is level in data units.

  • level (float, default: 0.95 ) –

    Confidence level of the ellipse (for "euclid": the radius).

  • segments (int, default: 51 ) –

    Number of segments of the outline (it has segments + 1 points).

  • 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 color="red", and the geom's options.

Returns:

  • LayerSpec –

    The layer.

Raises:

  • SpecError –

    If type is not one of the three, or segments is not a whole number of at least 1.

Examples:

>>> from ggrust import *
>>> data = {
...     "height": [150, 155, 160, 163, 168, 158, 175, 180, 172, 185, 178, 190],
...     "weight": [50, 56, 55, 61, 63, 54, 70, 78, 69, 84, 74, 88],
...     "group": ["a"] * 6 + ["b"] * 6,
... }
>>> plot = (
...     ggplot(data, aes("height", "weight", color="group"))
...     + geom_point()
...     + stat_ellipse(type="norm")
... )