{ "cells": [ { "cell_type": "markdown", "id": "05991f9b-ebbc-4024-8dbf-44d76721b791", "metadata": {}, "source": [ "# `geom_bracket()`" ] }, { "cell_type": "code", "execution_count": 1, "id": "16d6f6fd-d055-4621-ba51-a63404e05a0b", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "\n", "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "id": "330b4b6e-46b7-40b3-b538-383f37e61b6f", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 3, "id": "fe073512-dec0-4246-b69d-cfbee2899157", "metadata": {}, "outputs": [], "source": [ "def dodged_coord(group_id, subgroup_id, subgroups_count, width=.95):\n", " median = (subgroups_count - 1) / 2\n", " offset = (subgroup_id - median) * width\n", " scaler = 1.0 / subgroups_count\n", " return group_id + offset * scaler" ] }, { "cell_type": "code", "execution_count": 4, "id": "dcbc863d-9bbd-4a6e-b256-c41ad041a5be", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(234, 12)\n" ] }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Unnamed: 0manufacturermodeldisplyearcyltransdrvctyhwyflclass
01audia41.819994auto(l5)f1829pcompact
12audia41.819994manual(m5)f2129pcompact
23audia42.020084manual(m6)f2031pcompact
34audia42.020084auto(av)f2130pcompact
45audia42.819996auto(l5)f1626pcompact
\n", "
" ], "text/plain": [ " Unnamed: 0 manufacturer model displ year cyl trans drv cty hwy \\\n", "0 1 audi a4 1.8 1999 4 auto(l5) f 18 29 \n", "1 2 audi a4 1.8 1999 4 manual(m5) f 21 29 \n", "2 3 audi a4 2.0 2008 4 manual(m6) f 20 31 \n", "3 4 audi a4 2.0 2008 4 auto(av) f 21 30 \n", "4 5 audi a4 2.8 1999 6 auto(l5) f 16 26 \n", "\n", " fl class \n", "0 p compact \n", "1 p compact \n", "2 p compact \n", "3 p compact \n", "4 p compact " ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.read_csv(\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/refs/heads/master/data/mpg.csv\")\n", "print(df.shape)\n", "df.head()" ] }, { "cell_type": "code", "execution_count": 5, "id": "2a24d318-65a6-491d-8005-f3a2b706059d", "metadata": {}, "outputs": [], "source": [ "p = ggplot(df, aes(\"drv\", \"hwy\")) + geom_boxplot()\n", "grouped_p = ggplot(df, aes(x=\"drv\", y=\"hwy\")) + geom_boxplot(aes(fill=as_discrete(\"year\")))" ] }, { "cell_type": "markdown", "id": "0fabe2ed-f77a-4ee0-9b50-7dfcc3355447", "metadata": {}, "source": [ "## Basic Plot" ] }, { "cell_type": "code", "execution_count": 6, "id": "359f9558-a4b9-43df-8f5f-b8e1772e7631", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "brackets_data = {\n", " \"xmin\": [\"f\", \"f\"],\n", " \"xmax\": [\"4\", \"r\"],\n", " \"y\": [46, 48],\n", " \"lower_y\": [10, 8],\n", " \"label\": [0.01, 0.02],\n", " \"g\": [\"A\", \"B\"]\n", "}\n", "\n", "bracket_plot = p + geom_bracket(aes(xmin=\"xmin\", xmax=\"xmax\", y=\"y\", label=\"label\"), data=brackets_data)\n", "bracket_plot" ] }, { "cell_type": "markdown", "id": "16dc9372-101d-448e-a265-f65558fc28dc", "metadata": {}, "source": [ "## Brackets Between Groups" ] }, { "cell_type": "code", "execution_count": 7, "id": "44e31fcb-ed24-4918-99eb-061f60dfd155", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "brackets_grouped_data = {\n", " \"xmin\": [-.2375, 0.7625, 1.7625],\n", " \"xmax\": [.2375, 1.2375, 2.2375],\n", " \"y\": [46, 30, 28],\n", " \"label\": [0.01, 0.02, 0.03],\n", " \"g\": [\"A\", \"B\", \"C\"],\n", "}\n", "\n", "grouped_p + geom_bracket(aes(xmin=\"xmin\", xmax=\"xmax\", y=\"y\", label=\"label\"), data=brackets_grouped_data)" ] }, { "cell_type": "markdown", "id": "79ef43cf-5f8d-41c8-b961-7c1162836d43", "metadata": {}, "source": [ "## Orientation" ] }, { "cell_type": "code", "execution_count": 8, "id": "50c0d1ed-0de2-444d-a23d-a6e15832b0d2", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + \\\n", " geom_boxplot(aes(\"hwy\", \"drv\"), data=df) + \\\n", " geom_bracket(aes(ymin=\"xmin\", ymax=\"xmax\", x=\"y\", label=\"label\"), data=brackets_data)" ] }, { "cell_type": "markdown", "id": "d9485031-bae5-4997-892f-53fcdddf122b", "metadata": {}, "source": [ "## Remove Label" ] }, { "cell_type": "code", "execution_count": 9, "id": "66877ffa-311d-4e51-b70d-54ece3a007ec", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " p + geom_bracket(aes(xmin=\"xmin\", xmax=\"xmax\", y=\"y\", label=\"label\"), data=brackets_data),\n", " p + geom_bracket(aes(xmin=\"xmin\", xmax=\"xmax\", y=\"y\"), data=brackets_data),\n", " p + geom_bracket(aes(xmin=\"xmin\", xmax=\"xmax\", y=\"y\", label=\"label\"), data=brackets_data, size=0),\n", " p + geom_bracket(aes(xmin=\"xmin\", xmax=\"xmax\", y=\"y\", label=\"label\"), data=brackets_data, alpha=0, segment_alpha=1),\n", "], ncol=2)" ] }, { "cell_type": "markdown", "id": "22beefff-3886-4e54-bf46-b7530db8da21", "metadata": {}, "source": [ "## Aesthetics" ] }, { "cell_type": "markdown", "id": "2b6f31a1-4c5a-4844-a123-c700de10e177", "metadata": {}, "source": [ "### Own Aesthetics" ] }, { "cell_type": "markdown", "id": "951116c4-ea5c-425d-b592-5fbe5e1ba255", "metadata": {}, "source": [ "#### `tip_length_start`/`tip_length_end`" ] }, { "cell_type": "code", "execution_count": 10, "id": "cbf83e69-59b9-43ba-8c78-7828f2a67930", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " p + geom_bracket(aes(xmin=\"xmin\", xmax=\"xmax\", y=\"y\", label=\"label\"), data=brackets_data) + \\\n", " ggtitle(\"Default\"),\n", " p + geom_bracket(aes(xmin=\"xmin\", xmax=\"xmax\", y=\"y\", label=\"label\", tip_length_end=[40, 50]), data=brackets_data) + \\\n", " ggtitle(\"aes(tip_length_end=...)\"),\n", " p + geom_bracket(aes(xmin=\"xmin\", xmax=\"xmax\", y=\"y\", label=\"label\"), data=brackets_data, tip_length_end=15) + \\\n", " ggtitle(\"tip_length_end=15\"),\n", " p + geom_bracket(aes(xmin=\"xmin\", xmax=\"xmax\", y=\"lower_y\", label=\"label\"), data=brackets_data, tip_length_start=-5, tip_length_end=-5) + \\\n", " ggtitle(\"tip_length_xxx=-5\"),\n", " p + geom_bracket(aes(xmin=\"xmin\", xmax=\"xmax\", y=\"lower_y\", label=\"label\"), data=brackets_data, tip_length_start=-5) + \\\n", " ggtitle(\"tip_length_start=-5\"),\n", "], ncol=2)" ] }, { "cell_type": "markdown", "id": "5fd8fea2-3dd8-4e0f-94c8-98bed72bf074", "metadata": {}, "source": [ "### Text Aesthetics" ] }, { "cell_type": "code", "execution_count": 11, "id": "95d4ded2-5521-47f0-9e63-44ffc0134b01", "metadata": {}, "outputs": [], "source": [ "def aes_plots(name, const_value, value=\"g\"):\n", " mapping = dict(xmin=\"xmin\", xmax=\"xmax\", y=\"y\", label=\"label\")\n", " return [\n", " p + geom_bracket(aes(**{**mapping, **{name: value}}), data=brackets_data) + ggtitle(f\"aes({name}='{value}')\"),\n", " p + geom_bracket(aes(**mapping), data=brackets_data, **{name: const_value}) + ggtitle(f\"{name}: {const_value}\"),\n", " ]" ] }, { "cell_type": "code", "execution_count": 12, "id": "80a84b2f-0639-46d2-90cf-97c6c25f3a73", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid(aes_plots(\"alpha\", .5))" ] }, { "cell_type": "code", "execution_count": 13, "id": "0bf13cba-1b4b-4cfc-b0e1-c2b6ceb3c1c5", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid(aes_plots(\"color\", \"green\"))" ] }, { "cell_type": "code", "execution_count": 14, "id": "833eb3ef-6a69-4b48-b3d7-5ac5285296d7", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid(aes_plots(\"size\", 5))" ] }, { "cell_type": "code", "execution_count": 15, "id": "46e06d60-d7f2-4017-9630-ebfc1bb4d3b0", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "aes_plots(\"family\", \"courier\")[1]" ] }, { "cell_type": "code", "execution_count": 16, "id": "f2a5eb8a-f617-463b-95e0-ee09ca877496", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "aes_plots(\"fontface\", 'bold_italic')[1]" ] }, { "cell_type": "code", "execution_count": 17, "id": "0f1a44f9-d3cd-4a2a-9def-dca3a403e09d", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " aes_plots(\"hjust\", 0)[1],\n", " aes_plots(\"hjust\", .5)[1],\n", " aes_plots(\"hjust\", 1)[1],\n", "])" ] }, { "cell_type": "code", "execution_count": 18, "id": "71f19b14-c2a0-410d-968a-4f0026546dcc", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " aes_plots(\"vjust\", 0)[1],\n", " aes_plots(\"vjust\", .5)[1],\n", " aes_plots(\"vjust\", 1)[1],\n", "])" ] }, { "cell_type": "code", "execution_count": 19, "id": "1aa557fb-0838-4ce7-b46b-6a9c36ad87d3", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + geom_bracket(aes(xmin=\"xmin\", xmax=\"xmax\", y=\"lower_y\", label=\"label\"), data=brackets_data,\n", " tip_length_start=-5, tip_length_end=-5, vjust=2) + \\\n", " ggtitle(\"tip_length_xxx=-5, vjust=2\")" ] }, { "cell_type": "code", "execution_count": 20, "id": "98cf27cb-a342-4d48-bbc8-61c68fb405ed", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "aes_plots(\"angle\", 30)[1]" ] }, { "cell_type": "code", "execution_count": 21, "id": "1ee6b556-3a9a-4642-a5ba-1f87f612b3ee", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " ggplot() + geom_bracket(xmin=-1, xmax=1, label=\"ABC\\nDEF\") + ggtitle(\"Default lineheight\"),\n", " ggplot() + geom_bracket(xmin=-1, xmax=1, label=\"ABC\\nDEF\", lineheight=2) + ggtitle(\"lineheight=2\"),\n", "]) + ggtitle(\"Multiline label\")" ] }, { "cell_type": "markdown", "id": "1baa82ea-6b1a-43dd-ab9a-fd8f8114a838", "metadata": {}, "source": [ "### Segment Aesthetics" ] }, { "cell_type": "code", "execution_count": 22, "id": "9f984b89-0cf2-4550-abdb-0067b0bbbd7f", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid(aes_plots(\"linetype\", 'dotted'))" ] }, { "cell_type": "code", "execution_count": 23, "id": "bdab1426-be00-4d56-a5fa-5b6ec7e78b36", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid(aes_plots(\"segment_color\", \"green\"))" ] }, { "cell_type": "code", "execution_count": 24, "id": "eeabaf34-1a32-47ed-8535-0937df27f56f", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid(aes_plots(\"segment_size\", 4))" ] }, { "cell_type": "code", "execution_count": 25, "id": "bede8be8-4325-4705-a673-66e225d041a0", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid(aes_plots(\"segment_alpha\", .5))" ] }, { "cell_type": "markdown", "id": "10f402a5-146f-489a-9b25-73af3d0a8a9f", "metadata": {}, "source": [ "## Parameters" ] }, { "cell_type": "markdown", "id": "38e39f17-80d1-40e1-a93e-8fecac7bca68", "metadata": {}, "source": [ "### Own Parameters" ] }, { "cell_type": "markdown", "id": "f0467286-6ec0-4dc2-9945-81b09cd9d752", "metadata": {}, "source": [ "#### `bracket_shorten`" ] }, { "cell_type": "code", "execution_count": 26, "id": "c6ae7d1e-f040-4253-98a7-c2f4141c2886", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " p + geom_bracket(aes(xmin=\"xmin\", xmax=\"xmax\", y=\"y\", label=\"label\"), data=brackets_data) + ggtitle(\"Default\"),\n", " p + geom_bracket(aes(xmin=\"xmin\", xmax=\"xmax\", y=\"y\", label=\"label\"), data=brackets_data, bracket_shorten=.75) + ggtitle(\"bracket_shorten=.75\"),\n", " p + geom_bracket(aes(xmin=\"xmin\", xmax=\"xmax\", y=\"y\", label=\"label\"), data=brackets_data, bracket_shorten=1.25) + ggtitle(\"bracket_shorten=1.25\"),\n", "])" ] }, { "cell_type": "markdown", "id": "39c0b9fb-b9d6-4f99-b807-c07fd23a33fe", "metadata": {}, "source": [ "#### `tip_length_unit`" ] }, { "cell_type": "code", "execution_count": 27, "id": "f09e83d3-c8ee-4e78-a3e5-402381c2cd94", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " p + geom_bracket(aes(xmin=\"xmin\", xmax=\"xmax\", y=\"y\", label=\"label\"), data=brackets_data) + \\\n", " ggtitle(\"Default\"),\n", " p + geom_bracket(aes(xmin=\"xmin\", xmax=\"xmax\", y=\"y\", label=\"label\"), data=brackets_data, tip_length_unit='px') + \\\n", " ggtitle(\"tip_length_unit='px'\"),\n", " p + geom_bracket(aes(xmin=\"xmin\", xmax=\"xmax\", y=\"y\", label=\"label\"), data=brackets_data, tip_length_unit='identity') + \\\n", " ggtitle(\"tip_length_unit='identity'\"),\n", "])" ] }, { "cell_type": "markdown", "id": "d7197665-fcf7-4866-8133-5481deff0cf1", "metadata": {}, "source": [ "### Text Parameters" ] }, { "cell_type": "markdown", "id": "dee57bad-c0da-4915-8f61-8f7a0ae68669", "metadata": {}, "source": [ "#### `label_format`" ] }, { "cell_type": "code", "execution_count": 28, "id": "18331c15-3e3a-4540-8ad3-39d7e13d5e22", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + geom_bracket(aes(xmin=\"xmin\", xmax=\"xmax\", y=\"y\", label=\"label\"), data=brackets_data, label_format=\".2~e\")" ] }, { "cell_type": "markdown", "id": "0ce53cd5-bad4-4f13-b7ec-bfd8d292c9fb", "metadata": {}, "source": [ "#### `na_text`" ] }, { "cell_type": "code", "execution_count": 29, "id": "fd87bd3f-4e14-47a5-a992-74983cf5821c", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "brackets_na_data = {\n", " \"xmin\": [\"f\", \"f\"],\n", " \"xmax\": [\"4\", \"r\"],\n", " \"y\": [46, 48],\n", " \"label\": [0.01, None],\n", " \"g\": [\"A\", \"B\"]\n", "}\n", "\n", "p + geom_bracket(aes(xmin=\"xmin\", xmax=\"xmax\", y=\"y\", label=\"label\"), data=brackets_na_data, na_text=\"None\")" ] }, { "cell_type": "markdown", "id": "e51640f8-0c38-4dcc-b4c8-c519d76b2e3d", "metadata": {}, "source": [ "#### `nudge_x`/`nudge_y`, `nudge_unit`" ] }, { "cell_type": "code", "execution_count": 30, "id": "3cc765a6-255e-48f5-bc0e-70ea2a063229", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " p + geom_bracket(aes(xmin=\"xmin\", xmax=\"xmax\", y=\"y\", label=\"label\"), data=brackets_data) + ggtitle(\"Default\"),\n", " p + geom_bracket(aes(xmin=\"xmin\", xmax=\"xmax\", y=\"y\", label=\"label\"), data=brackets_data, nudge_y=-2) + ggtitle(\"nudge_y=-2\"),\n", " p + geom_bracket(aes(xmin=\"xmin\", xmax=\"xmax\", y=\"y\", label=\"label\"), data=brackets_data, nudge_x=.25) + ggtitle(\"nudge_x=.25\"),\n", " p + geom_bracket(aes(xmin=\"xmin\", xmax=\"xmax\", y=\"y\", label=\"label\"), data=brackets_data, nudge_x=10, nudge_unit='px') + \\\n", " ggtitle(\"nudge_x=10, nudge_unit='px'\"),\n", "], ncol=2)" ] }, { "cell_type": "markdown", "id": "a218dbc5-ebdd-41a4-845d-47f744f8bc6e", "metadata": {}, "source": [ "#### `size_unit`" ] }, { "cell_type": "code", "execution_count": 31, "id": "bc8f9e5f-2e59-4c1d-9f72-52d918887224", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " p + geom_bracket(aes(xmin=\"xmin\", xmax=\"xmax\", y=\"y\", label=\"label\"), data=brackets_data, size=5) + ggtitle(\"Default size_unit\"),\n", " p + geom_bracket(aes(xmin=\"xmin\", xmax=\"xmax\", y=\"y\", label=\"label\"), data=brackets_data, size=5, size_unit='y') + ggtitle(\"size_unit='y'\"),\n", "]) + ggsize(800, 600)" ] }, { "cell_type": "markdown", "id": "1154a58e-850b-4442-88d0-8226b38d60e1", "metadata": {}, "source": [ "### Standard Parameters" ] }, { "cell_type": "markdown", "id": "6636771a-a103-43dc-9724-54a1d96b1976", "metadata": {}, "source": [ "#### `position`" ] }, { "cell_type": "code", "execution_count": 32, "id": "c3757a14-c5e1-4b6f-ac81-49d31c690fba", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "WARN: The function position_dodgev() is deprecated and will be removed in future releases.\n" ] }, { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "brackets_grouped_data2 = {\n", " \"xmin\": [\"f\", \"f\", \"f\", \"f\"],\n", " \"xmax\": [\"4\", \"4\", \"r\", \"r\"],\n", " \"y\": [50, 50, 56, 56],\n", " \"label\": [0.01, 0.03, 0.02, 0.04],\n", " \"year\": [1999, 2008, 1999, 2008]\n", "}\n", "\n", "gggrid([\n", " p + geom_bracket(aes(xmin=\"xmin\", xmax=\"xmax\", y=\"y\", label=\"label\"), data=brackets_data, position=position_nudge(x=.25)) + \\\n", " ggtitle(\"position_nudge(x=.25)\"),\n", " p + geom_bracket(aes(xmin=\"xmin\", xmax=\"xmax\", y=\"y\", label=\"label\"), data=brackets_data, position=position_nudge(y=-2)) + \\\n", " ggtitle(\"position_nudge(y=-2)\"),\n", " grouped_p + geom_bracket(aes(xmin=\"xmin\", xmax=\"xmax\", y=\"y\", label=\"label\", color=as_discrete(\"year\")), data=brackets_grouped_data2, position=position_dodgev()),\n", "], ncol=2)" ] }, { "cell_type": "markdown", "id": "9d1cc1ad-bb7d-4e58-8450-c5847f2f44a2", "metadata": {}, "source": [ "#### `show_legend`" ] }, { "cell_type": "code", "execution_count": 33, "id": "62b02faf-e6d8-458a-b7f4-ea502b1fd80b", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + geom_bracket(aes(xmin=\"xmin\", xmax=\"xmax\", y=\"y\", label=\"label\",\n", " size=\"g\", angle=\"g\", linetype=\"g\", segment_color=\"g\", segment_alpha=\"g\", segment_size=\"g\"),\n", " data=brackets_data, show_legend=True)" ] }, { "cell_type": "markdown", "id": "bc4aa021-f8d3-4c21-85ba-c51b491a43ae", "metadata": {}, "source": [ "#### `manual_key`" ] }, { "cell_type": "code", "execution_count": 34, "id": "803d3c32-5ea4-4191-a796-c486b3b2d768", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + geom_bracket(aes(xmin=\"xmin\", xmax=\"xmax\", y=\"y\", label=\"label\"), data=brackets_data, color=\"blue\", manual_key=\"p-value\")" ] }, { "cell_type": "markdown", "id": "a46f6587-3b71-4e1a-9094-1c5aa4adfba0", "metadata": {}, "source": [ "#### `sampling`" ] }, { "cell_type": "code", "execution_count": 35, "id": "fd6038b5-89d7-42cc-be4e-604dc5bf5e80", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + geom_bracket(aes(xmin=\"xmin\", xmax=\"xmax\", y=\"y\", label=\"label\"), data=brackets_data, sampling=sampling_random(1))" ] }, { "cell_type": "markdown", "id": "eee82daa-96ec-49c4-99fe-4e1d87975b28", "metadata": {}, "source": [ "#### `color_by`" ] }, { "cell_type": "code", "execution_count": 36, "id": "fa22015c-5eea-48b9-ac8c-7e959b4d62a8", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + geom_bracket(aes(xmin=\"xmin\", xmax=\"xmax\", y=\"y\", label=\"label\", paint_a=\"g\"), data=brackets_data, color_by=\"paint_a\")" ] }, { "cell_type": "markdown", "id": "dad2dd26-070e-4cdf-9ea5-4426aecbc57a", "metadata": {}, "source": [ "## Interaction With Other Layers" ] }, { "cell_type": "markdown", "id": "e85a5826-9aeb-4895-b881-620232f10c39", "metadata": {}, "source": [ "### Marginal" ] }, { "cell_type": "code", "execution_count": 37, "id": "53aaa6eb-e677-4d13-a7dd-7774222ed3ae", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bracket_plot + ggmarginal(\"r\", layer=geom_histogram())" ] }, { "cell_type": "markdown", "id": "dafcdcab-c24b-4f21-8257-03054c699194", "metadata": {}, "source": [ "### Toolbar" ] }, { "cell_type": "code", "execution_count": 38, "id": "1b5f7837-7137-4402-b461-f2668844c923", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bracket_plot + ggtb()" ] }, { "cell_type": "markdown", "id": "1e760e89-eef3-4d36-9c40-479bde075028", "metadata": {}, "source": [ "### Facets" ] }, { "cell_type": "code", "execution_count": 39, "id": "d1c8f452-9003-427d-9963-7c3f1011723e", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "grouped_p + \\\n", " geom_bracket(aes(xmin=\"xmin\", xmax=\"xmax\", y=\"y\", label=\"label\", color=as_discrete(\"year\")), data=brackets_grouped_data2) + \\\n", " facet_grid(x=\"year\")" ] }, { "cell_type": "markdown", "id": "f0dea8c6-4b13-410e-aea0-1b4638560fd1", "metadata": {}, "source": [ "### Coordinate Systems" ] }, { "cell_type": "markdown", "id": "ea070121-310f-44d0-9214-fec6538f4a48", "metadata": {}, "source": [ "#### Flip" ] }, { "cell_type": "code", "execution_count": 40, "id": "102f0514-cfb9-470f-9e7e-d17848748f47", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bracket_plot + coord_flip()" ] }, { "cell_type": "markdown", "id": "b1912c67-0f5b-49a2-866e-5cc3515c9fcc", "metadata": {}, "source": [ "## Tests" ] }, { "cell_type": "code", "execution_count": 41, "id": "9fa4734d-f049-452a-a4eb-6e559805d25f", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tests = [\n", " {\n", " 'title': \"Empty data\",\n", " 'data': {\n", " 'xmin': [],\n", " 'xmax': [],\n", " 'y': [],\n", " 'label': [],\n", " }\n", " },\n", " {\n", " 'title': \"One element\",\n", " 'data': {\n", " 'xmin': [0],\n", " 'xmax': [1],\n", " 'y': [0],\n", " 'label': [\"a\"],\n", " }\n", " },\n", " {\n", " 'title': \"NaN's in data\",\n", " 'data': {\n", " 'xmin': [0, None, 0, 0, 0],\n", " 'xmax': [1, 1, None, 1, 1],\n", " 'y': [0, 1, 2, None, 4],\n", " 'label': [\"a\", \"a\", \"a\", \"a\", None],\n", " }\n", " },\n", "]\n", "\n", "gggrid([\n", " ggplot(t['data']) + \\\n", " geom_bracket(aes(xmin='xmin', xmax='xmax', y='y', label='label')) + \\\n", " ggtitle(t['title'])\n", " for t in tests\n", "], ncol=2)" ] }, { "cell_type": "code", "execution_count": 42, "id": "2d20c279-9587-4894-b1e8-cdde2ae8e295", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def get_test_plot(dy, shift):\n", " y_base = shift * dy\n", " box_data = {\n", " \"x\": [\"A\", \"B\", \"C\"],\n", " \"y\": [y_base, y_base + dy, y_base - dy],\n", " \"ymin\": [y_base - 5 * dy, y_base - 4 * dy, y_base - 6 * dy],\n", " \"ymax\": [y_base + 5 * dy, y_base + 6 * dy, y_base + 4 * dy],\n", " }\n", " bracket_data = {\n", " \"xmin\": [\"A\", \"A\"],\n", " \"xmax\": [\"B\", \"C\"],\n", " \"y\": [y_base + 7 * dy, y_base + 8 * dy],\n", " \"label\": [0.01, 0.02],\n", " }\n", " return ggplot() + \\\n", " geom_crossbar(aes(x=\"x\", y=\"y\", ymin=\"ymin\", ymax=\"ymax\"), data=box_data) + \\\n", " geom_bracket(aes(xmin=\"xmin\", xmax=\"xmax\", y=\"y\", label=\"label\"), data=bracket_data)\n", "\n", "gggrid([\n", " get_test_plot(1e-11, 3.14e6),\n", " get_test_plot(1e-11, -3.14e6),\n", " get_test_plot(1e11, 3.14e6),\n", " get_test_plot(1e11, -3.14e6),\n", "], ncol=2)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.19" } }, "nbformat": 4, "nbformat_minor": 5 }