{ "cells": [ { "cell_type": "markdown", "id": "c35997a9", "metadata": {}, "source": [ "# Plot Constructors\n", "\n", "Constructor helpers create ggpubr- and plotthis-style plots and return regular\n", "`ggplot` objects. The examples therefore add scales and themes after\n", "construction.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "a8485651", "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": "markdown", "id": "dcf7be51", "metadata": {}, "source": [ "## Distribution and group summaries\n", "\n", "Use constructor arguments for the common mappings, then add scales and themes in\n", "the standard plotnine way.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "80e74cd5", "metadata": {}, "outputs": [], "source": [ "(\n", " ggviolin(tooth, x=\"dose\", y=\"len\", fill=\"supp\", add=\"boxplot\")\n", " + scale_fill_tableau(k=2)\n", " + theme_pubr()\n", " + labs(x=\"Dose\", y=\"Tooth length\", fill=\"Supplement\")\n", ")\n" ] }, { "cell_type": "code", "execution_count": null, "id": "eea19c9a", "metadata": {}, "outputs": [], "source": [ "(\n", " gghistogram(penguin_data, x=\"body_mass_g\", fill=\"species\", bins=24)\n", " + scale_fill_colorblind(k=3)\n", " + theme_clean()\n", " + labs(x=\"Body mass (g)\", fill=\"Species\")\n", ")\n" ] }, { "cell_type": "markdown", "id": "15c25b5b", "metadata": {}, "source": [ "## Scatter, bar, line, and paired plots\n", "\n", "The constructor API stays small by design. For specialized plots, start with a\n", "constructor and then add plotnine layers.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "1b19c9b3", "metadata": {}, "outputs": [], "source": [ "(\n", " ggscatter(penguin_data, \"bill_length_mm\", \"body_mass_g\", color=\"species\", add=\"reg.line\")\n", " + scale_color_few(k=3)\n", " + theme_few()\n", ")\n" ] }, { "cell_type": "code", "execution_count": null, "id": "26e8c177", "metadata": {}, "outputs": [], "source": [ "summary_data = tooth.groupby([\"dose\", \"supp\"], observed=True, as_index=False)[\"len\"].mean()\n", "\n", "(\n", " ggbarplot(summary_data, x=\"dose\", y=\"len\", fill=\"supp\")\n", " + scale_fill_tableau(k=2)\n", " + theme_classic2()\n", " + labs(x=\"Dose\", y=\"Mean tooth length\", fill=\"Supplement\")\n", ")\n" ] }, { "cell_type": "code", "execution_count": null, "id": "0fe0b327", "metadata": {}, "outputs": [], "source": [ "line_data = flights[flights[\"year\"].isin([1950, 1951, 1952])].copy()\n", "line_data[\"month_index\"] = line_data.groupby(\"year\").cumcount() + 1\n", "\n", "(\n", " ggline(line_data, \"month_index\", \"passengers\", color=\"factor(year)\")\n", " + scale_color_tableau(k=3)\n", " + theme_few()\n", " + labs(x=\"Month\", y=\"Passengers\", color=\"Year\")\n", ")\n" ] }, { "cell_type": "markdown", "id": "155b079e", "metadata": {}, "source": [ "## Plotthis-style constructors\n", "\n", "These helpers cover common bioinformatics plots without adding heavy optional\n", "dependencies.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "58be7d87", "metadata": {}, "outputs": [], "source": [ "rng = np.random.default_rng(7)\n", "volcano = pd.DataFrame({\n", " \"gene\": [f\"gene_{i}\" for i in range(80)],\n", " \"log2fc\": rng.normal(0, 1.2, 80),\n", " \"pvalue\": np.clip(rng.beta(0.7, 8, 80), 1e-6, 1),\n", "})\n", "\n", "(\n", " ggvolcano(volcano, label=\"gene\", label_top=5, p_cutoff=0.05, fc_cutoff=1.0)\n", " + theme_clean()\n", ")\n" ] }, { "cell_type": "code", "execution_count": null, "id": "6cc17ac8", "metadata": {}, "outputs": [], "source": [ "embedding = pd.DataFrame({\n", " \"UMAP_1\": rng.normal(size=120),\n", " \"UMAP_2\": rng.normal(size=120),\n", " \"cluster\": rng.choice([\"T\", \"B\", \"Mono\"], size=120),\n", "})\n", "embedding[\"marker\"] = np.exp(-((embedding[\"UMAP_1\"] - 0.7) ** 2 + embedding[\"UMAP_2\"] ** 2))\n", "\n", "(\n", " ggfeaturedim(embedding, feature=\"marker\")\n", " + theme_transparent()\n", " + labs(color=\"Marker\")\n", ")\n" ] }, { "cell_type": "code", "execution_count": null, "id": "e71c3c41", "metadata": {}, "outputs": [], "source": [ "roc_data = pd.DataFrame({\n", " \"truth\": [0, 0, 0, 1, 1, 1, 1, 0],\n", " \"score\": [0.05, 0.2, 0.45, 0.55, 0.75, 0.8, 0.91, 0.35],\n", "})\n", "\n", "plot = ggroc(roc_data, truth=\"truth\", score=\"score\") + theme_tufte()\n", "plot.data.attrs[\"auc\"], plot\n" ] }, { "cell_type": "markdown", "id": "8d7879d7", "metadata": {}, "source": [ "Aliases preserve familiar names from other plotting packages:\n", "`VolcanoPlot`, `DimPlot`, `FeatureDimPlot`, and `ROCCurve`.\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "name": "python", "pygments_lexer": "ipython3" } }, "nbformat": 4, "nbformat_minor": 5 }