{ "cells": [ { "cell_type": "markdown", "id": "2ea69c69", "metadata": {}, "source": [ "# Geoms and Positions\n", "\n", "This page groups extra layers by display task: dense points, categorical\n", "points, labels, margins, and low-ink summaries.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "9e803765", "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": "18f4fbcf", "metadata": {}, "source": [ "## Point density and connected points\n", "\n", "`geom_pointdensity()` colors points by local density. `geom_pointpath()` draws\n", "points and a matching path from one layer.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "f071f92e", "metadata": {}, "outputs": [], "source": [ "iris_small = iris.copy()\n", "\n", "(\n", " ggplot(iris_small, aes(\"sepal_length\", \"petal_length\"))\n", " + geom_pointdensity(size=2.3)\n", " + theme_scientific()\n", " + labs(x=\"Sepal length\", y=\"Petal length\")\n", ")\n" ] }, { "cell_type": "code", "execution_count": null, "id": "9afde5e0", "metadata": {}, "outputs": [], "source": [ "path_data = flights[flights[\"year\"].isin([1949, 1950])].copy()\n", "path_data[\"month_index\"] = path_data.groupby(\"year\").cumcount() + 1\n", "\n", "(\n", " ggplot(path_data, aes(\"month_index\", \"passengers\", color=\"factor(year)\"))\n", " + geom_pointpath(linesize=0.7)\n", " + scale_color_tableau(k=2)\n", " + theme_few()\n", " + labs(x=\"Month\", y=\"Passengers\", color=\"Year\")\n", ")\n" ] }, { "cell_type": "markdown", "id": "feb9941e", "metadata": {}, "source": [ "## Categorical point placement\n", "\n", "`geom_beeswarm()` and `geom_quasirandom()` separate overlapping points while\n", "preserving the categorical axis.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "a499b9e1", "metadata": {}, "outputs": [], "source": [ "(\n", " ggplot(tooth, aes(\"dose\", \"len\", color=\"supp\"))\n", " + geom_boxplot(alpha=0.25, outlier_shape=None)\n", " + geom_beeswarm(size=2.1, cex=1.6)\n", " + scale_color_colorblind(k=2)\n", " + theme_classic2()\n", " + labs(x=\"Dose\", y=\"Tooth length\", color=\"Supplement\")\n", ")\n" ] }, { "cell_type": "code", "execution_count": null, "id": "03421e6a", "metadata": {}, "outputs": [], "source": [ "(\n", " ggplot(tooth, aes(\"dose\", \"len\", fill=\"supp\"))\n", " + geom_half_violin(alpha=0.55, side=\"l\")\n", " + geom_half_boxplot(width=0.18, side=\"r\")\n", " + geom_quasirandom(aes(color=\"supp\"), width=0.12, size=1.8)\n", " + scale_fill_tableau(k=2)\n", " + scale_color_tableau(k=2)\n", " + theme_clean()\n", " + labs(x=\"Dose\", y=\"Tooth length\")\n", ")\n" ] }, { "cell_type": "markdown", "id": "2ae7a32c", "metadata": {}, "source": [ "## Labels and callouts\n", "\n", "The text layers handle repelled labels, boxed labels, and a small subset of\n", "markdown-style formatting.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "d8a96dce", "metadata": {}, "outputs": [], "source": [ "label_data = penguin_data.groupby(\"species\", observed=True).tail(2).copy()\n", "\n", "(\n", " ggplot(penguin_data, aes(\"bill_length_mm\", \"bill_depth_mm\", color=\"species\"))\n", " + geom_point(alpha=0.55)\n", " + geom_text_repel(aes(label=\"species\"), data=label_data, show_legend=False)\n", " + scale_color_few(k=3)\n", " + theme_few()\n", ")\n" ] }, { "cell_type": "code", "execution_count": null, "id": "32efdc9f", "metadata": {}, "outputs": [], "source": [ "notes = pd.DataFrame({\n", " \"x\": [35, 55],\n", " \"y\": [21, 13],\n", " \"label\": [\"**Deep bills**
Adelie-rich region\", \"Longer bills\\nGentoo and Chinstrap\"],\n", "})\n", "\n", "(\n", " ggplot(penguin_data, aes(\"bill_length_mm\", \"bill_depth_mm\"))\n", " + geom_point(alpha=0.35)\n", " + geom_richtext(aes(\"x\", \"y\", label=\"label\"), data=notes, inherit_aes=False)\n", " + geom_textbox(\n", " aes(x=\"x\", y=\"y - 1.8\", label=\"label\"),\n", " data=notes,\n", " inherit_aes=False,\n", " text_width=18,\n", " fill=\"#f7f7f7\",\n", " )\n", " + theme_pubclean()\n", ")\n" ] }, { "cell_type": "markdown", "id": "fdd6165a", "metadata": {}, "source": [ "## Margins, raster-like polygons, and Tufte summaries\n", "\n", "Use margin geoms when a rectangle should extend to a panel edge. Use the Tufte\n", "geoms when the summary should use fewer marks than a standard boxplot.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "04f493b5", "metadata": {}, "outputs": [], "source": [ "raster_data = pd.DataFrame({\n", " \"x\": np.repeat(np.arange(5), 5),\n", " \"y\": np.tile(np.arange(5), 5),\n", "})\n", "raster_data[\"z\"] = np.sin(raster_data[\"x\"]) + np.cos(raster_data[\"y\"])\n", "\n", "(\n", " ggplot(raster_data, aes(\"x\", \"y\", fill=\"z\"))\n", " + geom_polygonraster()\n", " + theme_clean()\n", " + labs(x=\"Column\", y=\"Row\", fill=\"Value\")\n", ")\n" ] }, { "cell_type": "code", "execution_count": null, "id": "4b4dd4a4", "metadata": {}, "outputs": [], "source": [ "(\n", " ggplot(tooth, aes(\"dose\", \"len\"))\n", " + geom_tufteboxplot()\n", " + geom_rangeframe()\n", " + theme_tufte()\n", " + labs(x=\"Dose\", y=\"Tooth length\")\n", ")\n" ] }, { "cell_type": "markdown", "id": "157e0336", "metadata": {}, "source": [ "`position_lineartrans()` and `position_disjoint_ranges()` are lower-level\n", "position adjustments. They are intended for custom layers that need a simple\n", "coordinate transform or automatic packing of interval rows.\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "name": "python", "pygments_lexer": "ipython3" } }, "nbformat": 4, "nbformat_minor": 5 }