{ "cells": [ { "cell_type": "markdown", "id": "e9768625", "metadata": {}, "source": [ "# Statistical Annotations\n", "\n", "Use this page for ggpubr-style statistical annotations: compute tests inside a\n", "plot, compute a table first, or place labels manually.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "90bf0438", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "from plotnine_extra import *\n", "from plotnine_extra.data import ToothGrowth, flights, iris, penguins\n", "\n", "tooth = ToothGrowth.assign(dose=ToothGrowth[\"dose\"].astype(str))\n", "penguin_data = penguins.dropna(subset=[\n", " \"bill_length_mm\",\n", " \"bill_depth_mm\",\n", " \"body_mass_g\",\n", " \"species\",\n", "]).copy()\n" ] }, { "cell_type": "code", "execution_count": null, "id": "b564c13d", "metadata": {}, "outputs": [], "source": [ "tooth = ToothGrowth.assign(dose=ToothGrowth[\"dose\"].astype(str))\n", "tooth.head()\n" ] }, { "cell_type": "markdown", "id": "4f434181", "metadata": {}, "source": [ "## Calculate a table before plotting\n", "\n", "`compare_means()` is useful when the statistical table should be inspected or\n", "saved before annotation.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "d358ef3c", "metadata": {}, "outputs": [], "source": [ "compare_means(\"len ~ dose\", tooth, method=\"t.test\")\n" ] }, { "cell_type": "markdown", "id": "ddf73308", "metadata": {}, "source": [ "## Add a global test label\n", "\n", "`stat_compare_means()` places one global test label by default.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "70a34cfa", "metadata": {}, "outputs": [], "source": [ "(\n", " ggboxplot(tooth, \"dose\", \"len\", fill=\"dose\", add=\"jitter\")\n", " + stat_compare_means(method=\"anova\", label=\"p.format\")\n", " + scale_fill_tableau(k=3)\n", " + theme_pubr()\n", " + labs(x=\"Dose\", y=\"Tooth length\")\n", ")\n" ] }, { "cell_type": "markdown", "id": "882a4cb7", "metadata": {}, "source": [ "## Add pairwise brackets\n", "\n", "`stat_pwc()` computes pairwise comparisons and draws brackets. Use\n", "`label=\"p.signif\"` for compact significance symbols.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "b72e0b5a", "metadata": {}, "outputs": [], "source": [ "(\n", " ggboxplot(tooth, \"dose\", \"len\", fill=\"dose\", add=\"quasirandom\")\n", " + stat_pwc(method=\"t.test\", label=\"p.signif\", p_adjust_method=\"holm\")\n", " + scale_fill_colorblind(k=3)\n", " + theme_classic2()\n", " + labs(x=\"Dose\", y=\"Tooth length\")\n", ")\n" ] }, { "cell_type": "markdown", "id": "aaceb2cf", "metadata": {}, "source": [ "## Paired data need an explicit subject id\n", "\n", "For paired tests, pass `wid`. The stat requires complete, non-duplicated\n", "subject/group pairs.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "27ec41c7", "metadata": {}, "outputs": [], "source": [ "paired = pd.DataFrame({\n", " \"subject\": [\"s1\", \"s1\", \"s2\", \"s2\", \"s3\", \"s3\", \"s4\", \"s4\"],\n", " \"time\": [\"before\", \"after\"] * 4,\n", " \"score\": [4.2, 5.1, 3.8, 4.7, 5.0, 6.1, 4.5, 5.0],\n", "})\n", "\n", "(\n", " ggpaired(paired, x=\"time\", y=\"score\", id=\"subject\")\n", " + stat_pwc(method=\"t.test\", paired=True, wid=\"subject\", comparisons=[(\"before\", \"after\")])\n", " + theme_clean()\n", " + labs(x=None, y=\"Score\")\n", ")\n" ] }, { "cell_type": "markdown", "id": "22914bb6", "metadata": {}, "source": [ "## Place pre-computed labels manually\n", "\n", "`stat_pvalue_manual()` accepts string group labels. Use `x_levels` when the plot\n", "order must be explicit.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "9bcdd7b3", "metadata": {}, "outputs": [], "source": [ "pvals = pd.DataFrame({\n", " \"group1\": [\"0.5\", \"1.0\"],\n", " \"group2\": [\"1.0\", \"2.0\"],\n", " \"p\": [0.041, 0.003],\n", " \"y.position\": [35, 39],\n", "})\n", "\n", "(\n", " ggboxplot(tooth, \"dose\", \"len\", fill=\"dose\")\n", " + stat_pvalue_manual(pvals, label=\"p.signif\", x_levels=[\"0.5\", \"1.0\", \"2.0\"])\n", " + scale_fill_few(k=3)\n", " + theme_pubr()\n", ")\n" ] }, { "cell_type": "markdown", "id": "80012961", "metadata": {}, "source": [ "## Formatting helpers\n", "\n", "The p-value helpers format table output and custom annotation labels.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "9c00296b", "metadata": {}, "outputs": [], "source": [ "[\n", " format_p_value(0.00042, style=\"p.format.signif\"),\n", " create_p_label(0.031),\n", " list_p_format_styles(),\n", "]\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "name": "python", "pygments_lexer": "ipython3" } }, "nbformat": 4, "nbformat_minor": 5 }