Skip to content

stat_pvalue_manual

stat_pvalue_manual(
    data: PlotData,
    *,
    label: str | None = None,
    y_position: str
    | Number
    | Sequence[Number] = "y.position",
    xmin: str = "group1",
    xmax: str | None = "group2",
    x: str | None = None,
    tip_length: Number = 0.03,
    step_increase: Number = 0,
    step_group_by: str | None = None,
    hide_ns: bool | Literal["p", "p.adj"] = False,
    bracket_nudge_y: Number = 0,
    bracket_shorten: Number | None = None,
    remove_bracket: bool = False,
    vjust: Number | None = None,
    position: Position | None = None,
    show_legend: bool | None = None,
    manual_key: ManualKey | None = None,
    **other_args: Any,
) -> LayerSpec

Draw brackets with p-value labels from a table of comparisons.

As stat_pvalue_manual in the R package ggpubr. Each row of data is a bracket from category xmin to xmax (by default the group1 and group2 columns, as compare_means() returns them) at y_position, labelled with label. With x (or a table of comparisons against ".all."), the labels are drawn at x without brackets.

Parameters:

  • data (DataFrame or dict) –

    The comparisons, one per row. Columns not used here are kept, so a facet variable in the table places each bracket in its panel.

  • label (str, default: None ) –

    A column to show, or a template of columns in braces, e.g. "p = {p.adj}". Default: the first of label, labels, p.adj.signif, p.adj, padj, p.signif, p.value, pval, p.val, p. Numbers print as R prints them.

  • y_position (str, float or list of float, default: "y.position" ) –

    A column of bracket heights, one height, or one per row (after hide_ns), in data units.

  • xmin (str, default: "group1" ) –

    Column of the left ends (x category names). As in ggpubr, the x axis must be discrete: for a numeric x, map as_discrete("dose").

  • xmax (str or None, default: "group2" ) –

    Column of the right ends; None draws labels only, at xmin.

  • x (str, default: None ) –

    Column of label positions: labels only, no brackets.

  • tip_length (float, default: 0.03 ) –

    Bracket tip length, as a share of the y range.

  • step_increase (float, default: 0 ) –

    Lift each bracket by this share of the y range over the one before.

  • step_group_by (str, default: None ) –

    Column whose groups each restart the steps.

  • hide_ns (bool or {p, adj}, default: False ) –

    Drop rows that are not significant: True uses the first of p.adj.signif, p.adj, p.signif and p ("ns", or above 0.05).

  • bracket_nudge_y (float, default: 0 ) –

    Lift every bracket, in data units.

  • bracket_shorten (float, default: None ) –

    Pull both bracket ends toward the middle: 0 keeps the full width.

  • remove_bracket (bool, default: False ) –

    With one group1 level (comparisons against a reference), labels only, at xmax.

  • vjust (float, default: None ) –

    Vertical justification of labels drawn without brackets. Default 0.5 for star labels, else 0, as ggpubr.

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

    Position adjustment.

  • show_legend (bool, default: None ) –

    True shows this layer in the legend.

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

    This layer's key in a manual legend.

  • **other_args (Any) –

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

Returns:

  • LayerSpec –

    The layer (it does not inherit the plot's mapping). As for any layer that maps y to another column, the y axis title becomes "y"; set it with labs(y=...).

Raises:

Examples:

>>> from ggrust import *
>>> data = {"diet": ["veg"] * 6 + ["meat"] * 6 + ["fish"] * 6,
...         "weight": [61, 58, 66, 70, 63, 59, 72, 80, 77, 69, 85, 74,
...                    65, 68, 71, 64, 70, 66]}
>>> table = compare_means("weight ~ diet", data)
>>> plot = (ggplot(data, aes("diet", "weight")) + geom_boxplot()
...         + stat_pvalue_manual(table, label="p = {p.adj}", y_position=88,
...                              step_increase=0.1))

A table written by hand, one row per bracket:

>>> brackets = {"group1": ["veg", "meat"], "group2": ["meat", "fish"],
...             "label": ["p = 0.004", "not tested"], "y.position": [88, 93]}
>>> plot = (ggplot(data, aes("diet", "weight")) + geom_boxplot()
...         + stat_pvalue_manual(brackets, label="label", tip_length=0.01))

A table written by hand, one row per bracket