{ "cells": [ { "cell_type": "markdown", "id": "cf07df44", "metadata": {}, "source": [ "# StatCompare\n", "\n", "Python port of the\n", "[`StatCompare`](https://github.com/HMU-WH/ggcompare/blob/main/vignettes/StatCompare.Rmd)\n", "vignette from the R package\n", "[ggcompare](https://github.com/HMU-WH/ggcompare).\n", "\n", "The `stat_compare` layer in `plotnine_extra` improves on\n", "[`ggsignif::geom_signif()`](https://github.com/const-ae/ggsignif)\n", "and\n", "[`ggpubr::stat_compare_means()`](https://github.com/kassambara/ggpubr)\n", "in three ways:\n", "\n", "1. **Stable adaptation to faceting** — per-panel detection of\n", " the x-positions that actually contain data.\n", "2. **Layer-level p-value adjustment** across panels (vs\n", " ggpubr's panel-level only), toggled by `panel_indep`.\n", "3. **Smoothly handles missing groupings** inside individual\n", " panels.\n", "\n", "You usually don't need to specify a test method. Just tell\n", "`stat_compare` whether you want a parametric or\n", "non-parametric test via the `parametric` flag, and the layer\n", "picks the right one:\n", "\n", "| number of groups | parametric | non-parametric |\n", "| --- | --- | --- |\n", "| 2 | Welch t-test | Wilcoxon rank-sum test |\n", "| > 2 | One-way ANOVA | Kruskal-Wallis test |\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "567e1af5", "metadata": { "execution": { "iopub.execute_input": "2026-04-08T09:53:14.737570Z", "iopub.status.busy": "2026-04-08T09:53:14.737332Z", "iopub.status.idle": "2026-04-08T09:54:13.523755Z", "shell.execute_reply": "2026-04-08T09:54:13.521392Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "plotnine_extra 0.2.0\n" ] }, { "data": { "text/html": [ "
| \n", " | manufacturer | \n", "model | \n", "displ | \n", "year | \n", "cyl | \n", "trans | \n", "drv | \n", "cty | \n", "hwy | \n", "fl | \n", "class | \n", "
|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | \n", "audi | \n", "a4 | \n", "1.8 | \n", "1999 | \n", "4 | \n", "auto(l5) | \n", "f | \n", "18 | \n", "29 | \n", "p | \n", "compact | \n", "
| 1 | \n", "audi | \n", "a4 | \n", "1.8 | \n", "1999 | \n", "4 | \n", "manual(m5) | \n", "f | \n", "21 | \n", "29 | \n", "p | \n", "compact | \n", "
| 2 | \n", "audi | \n", "a4 | \n", "2.0 | \n", "2008 | \n", "4 | \n", "manual(m6) | \n", "f | \n", "20 | \n", "31 | \n", "p | \n", "compact | \n", "
| 3 | \n", "audi | \n", "a4 | \n", "2.0 | \n", "2008 | \n", "4 | \n", "auto(av) | \n", "f | \n", "21 | \n", "30 | \n", "p | \n", "compact | \n", "
| 4 | \n", "audi | \n", "a4 | \n", "2.8 | \n", "1999 | \n", "6 | \n", "auto(l5) | \n", "f | \n", "16 | \n", "26 | \n", "p | \n", "compact | \n", "