stat_compare_means ¶
stat_compare_means(
mapping: FeatureSpec | None = None,
*,
data: PlotData | None = None,
method: Literal | None = None,
paired: bool | None = None,
comparisons: Sequence[Sequence[Level]] | None = None,
ref_group: Level | None = None,
hide_ns: bool | None = None,
label: Literal["p.format", "p.signif"] | None = None,
label_sep: str | None = None,
label_x: Number | None = None,
label_y: Number | Sequence[Number] | None = None,
step_increase: Number | None = None,
tip_length: Number | None = None,
p_adjust_method: Literal | 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
Test the groups of y along a discrete x and label the p-values.
As stat_compare_means in the R package ggpubr. Without comparisons or
ref_group, one global test across the x categories is labelled at the top
left ("Kruskal-Wallis, p = 1.5e-09"); when other aesthetics (color)
split each x into groups, those groups are tested per x. With ref_group,
every other category is tested against the reference and labelled above it.
With comparisons, each pair gets a bracket (geom_bracket) stacked above
the data, labelled with its p-value.
Parameters:
-
mapping(FeatureSpec, default:None) –Aesthetic mapping from
aes():x(categories) andy. -
data(DataFrame or dict, default:None) –Layer data; inherits the plot data when None.
-
method((test, test, anova, test), default:"t.test") –The test: Welch's t test, Wilcoxon rank sum (signed rank when paired), one-way ANOVA or Kruskal-Wallis. Default
"wilcox.test"for two groups (and forcomparisonsandref_group),"kruskal.test"for more. -
paired(bool, default:False) –Paired t or Wilcoxon tests; values pair up in row order.
-
comparisons(list of pairs, default:None) –Pairs of x categories to compare, by name or 1-based position, e.g.
[("0.5", "1"), ("1", "2")]. -
ref_group(str or int, default:None) –Reference category, or
".all."to compare each category with all values. -
hide_ns(bool, default:False) –With
label="p.signif", hidens(not significant) marks. -
label((format, signif), default:"p.format") –"p.format": the p-value;"p.signif": stars (****<= 1e-4,***<= 0.001,**<= 0.01,*<= 0.05, elsens). Default: the method and p-value ("Wilcoxon, p = 0.064"); for brackets, the p-value. -
label_sep(str, default:", ") –Separator between the method and the p-value.
-
label_x(float, default:None) –x of the global label, in data units.
-
label_y(float or list of float, default:None) –y of the labels, or the base of the brackets (one per comparison).
-
step_increase(float, default:None) –Gap between stacked brackets, as a share of the y range: 0.12, or 0 when
label_yis given. -
tip_length(float, default:0.03) –Bracket tip length, as a share of the y range.
-
p_adjust_method(Literal, default:None) –Adjust the layer's p-values for multiple comparisons (R
p.adjust):"holm","hochberg","hommel","bonferroni","BH"(or"fdr"),"BY"or"none". The default"none"shows unadjusted p-values, as ggpubr. -
geom(str, default:None) –"text"for labels,"bracket"forcomparisons(the defaults). -
position(str or FeatureSpec, default:None) –Position adjustment.
-
show_legend(bool, default:None) –True shows this layer in the legend.
-
inherit_aes(bool, default:True) –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 and brackets, e.g.
size,color.
Returns:
-
LayerSpec–The layer.
Raises:
-
SpecError–If a name is not accepted or a comparison is not a pair.
-
UnsupportedValueError–If an option has the wrong type.
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]}
>>> plot = (ggplot(data, aes("diet", "weight")) + geom_boxplot()
... + stat_compare_means(comparisons=[("veg", "meat"), ("veg", "fish")]))