Skip to content

stat_poly_eq

stat_poly_eq(
    mapping: FeatureSpec | None = None,
    *,
    data: PlotData | None = None,
    formula: str | None = None,
    label: Literal | Sequence[Literal] | None = None,
    label_sep: str | None = None,
    label_x_npc: Literal["left", "center", "right"]
    | Number
    | None = None,
    label_y_npc: Literal["bottom", "center", "top"]
    | Number
    | None = None,
    label_x: Number | Sequence[Number] | None = None,
    label_y: Number | Sequence[Number] | None = None,
    coef_digits: int | None = None,
    rr_digits: int | None = None,
    geom: str | None = None,
    position: Position | None = None,
    show_legend: bool | None = None,
    inherit_aes: bool | None = None,
    manual_key: ManualKey | None = None,
    na_rm: bool | None = None,
    **other_args: Any,
) -> LayerSpec

Label a least squares polynomial fit of y on x (equation, R²), per group.

As stat_poly_eq in the R package ggpmisc. The default label is "R² = 0.75"; label=["eq", "R2"] (ggpmisc use_label("eq", "R2")) also shows the equation "y = 37.3 - 5.34x". Labels sit at the top left of the data, stacked per group, as stat_cor.

Parameters:

  • mapping (FeatureSpec, default: None ) –

    Aesthetic mapping from aes(): x, y and grouping aesthetics.

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

    Layer data; inherits the plot data when None.

  • formula (str, default: "y ~ x" ) –

    The model: "y ~ x", "y ~ poly(x, 2, raw = TRUE)" or "y ~ x + I(x^2)". Pair it with geom_smooth(method="lm", deg=2).

  • label (str or list of str, default: "R2" ) –

    Parts of the label: "eq" (equation), "R2", "adj.R2", "P" (the fit's F test) and "n".

  • label_sep (str, default: ", " ) –

    Separator between the parts.

  • label_x_npc ((left, center, right), default: "left" ) –

    Horizontal place in the x range (or a share of it).

  • label_y_npc ((bottom, center, top), default: "bottom" ) –

    Vertical place in the y range (or a share of it).

  • label_x (float or list of float, default: None ) –

    Place in data units (one value, or one per group).

  • label_y (float or list of float, default: None ) –

    Place in data units (one value, or one per group).

  • coef_digits (int, default: 3 ) –

    Significant digits of the coefficients.

  • rr_digits (int, default: 2 ) –

    Decimals of R².

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

    Geom that draws the labels.

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

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

    This layer's key in a manual legend.

  • na_rm (bool, default: None ) –

    True removes rows with missing values silently.

  • **other_args (Any) –

    Constant aesthetics of the labels, e.g. size, color.

Returns:

  • LayerSpec –

    The layer.

Raises:

Examples:

>>> from ggrust import *
>>> data = {"hours": [1, 2, 3, 4, 5, 6, 7, 8],
...         "score": [52, 55, 61, 64, 70, 71, 78, 83]}
>>> plot = (ggplot(data, aes("hours", "score")) + geom_point()
...         + stat_poly_eq(label=["eq", "R2"]))