Skip to content

stat_cor

stat_cor(
    mapping: FeatureSpec | None = None,
    *,
    data: PlotData | None = None,
    method: Literal | None = None,
    cor_coef_name: str | 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,
    r_digits: int | None = None,
    p_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 the correlation of x and y and its p-value, per group.

As stat_cor in the R package ggpubr: "R = -0.87, p = 1.3e-10" at the top left of the data, one label per group (colored by group), stacked downward.

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.

  • method ((pearson, spearman, kendall), default: "pearson" ) –

    The correlation and its test (R cor.test).

  • cor_coef_name (str, default: "R" ) –

    Name of the coefficient in the label, e.g. "rho" or "tau".

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

    Separator between the coefficient and the p-value.

  • 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), over the _npc places.

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

    Place in data units (one value, or one per group), over the _npc places.

  • r_digits (int, default: 2 ) –

    Significant digits of the coefficient and the p-value.

  • p_digits (int, default: 2 ) –

    Significant digits of the coefficient and the p-value.

  • 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 = {"height": [150, 160, 165, 170, 175, 182, 190],
...         "weight": [52, 58, 63, 66, 74, 80, 88]}
>>> plot = ggplot(data, aes("height", "weight")) + geom_point() + stat_cor()