{ "cells": [ { "cell_type": "markdown", "id": "558d28c0", "metadata": {}, "source": [ "# Geometries with Dual Orientation\n", "\n", "- `geom_linerange()`\n", "- `geom_pointrange()`\n", "- `geom_errorbar()`\n", "- `geom_crossbar()`\n", "- `geom_ribbon()`\n", "\n", "You can flip opientation of these geometries simply by flipping their positional aesthetics mapping.\n", "\n", "**Note:** when flipping aesthetics, in certain cases, you will have to also change the **position adjustment** of the geometry.\n", "\n", "For example, when flipping orientation from vertical to horizontal, `position_dodge()` \\\n", "should be replaced with `position_dodgev()` (i.e. \"vertical dodge\").\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "da3d73f3", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T11:51:56.750382Z", "iopub.status.busy": "2024-04-26T11:51:56.750382Z", "iopub.status.idle": "2024-04-26T11:51:57.787572Z", "shell.execute_reply": "2024-04-26T11:51:57.787572Z" } }, "outputs": [], "source": [ "import pandas as pd\n", "\n", "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "id": "38f3f8b6", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T11:51:57.787572Z", "iopub.status.busy": "2024-04-26T11:51:57.787572Z", "iopub.status.idle": "2024-04-26T11:51:57.803315Z", "shell.execute_reply": "2024-04-26T11:51:57.803315Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 3, "id": "99409355", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T11:51:57.803315Z", "iopub.status.busy": "2024-04-26T11:51:57.803315Z", "iopub.status.idle": "2024-04-26T11:51:57.960549Z", "shell.execute_reply": "2024-04-26T11:51:57.960549Z" } }, "outputs": [ { "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", "
suppdoselen_minlen_maxlength
0OJ0.58.221.512.25
1OJ1.014.527.323.45
2OJ2.022.430.925.95
3VC0.54.211.57.15
4VC1.013.622.516.50
5VC2.018.533.925.95
\n", "
" ], "text/plain": [ " supp dose len_min len_max length\n", "0 OJ 0.5 8.2 21.5 12.25\n", "1 OJ 1.0 14.5 27.3 23.45\n", "2 OJ 2.0 22.4 30.9 25.95\n", "3 VC 0.5 4.2 11.5 7.15\n", "4 VC 1.0 13.6 22.5 16.50\n", "5 VC 2.0 18.5 33.9 25.95" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.read_csv(\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/ToothGrowth.csv\")\n", "err_df = df.groupby(by=['supp', 'dose']).agg({'len': ['min', 'max', 'median']}).reset_index()\n", "err_df.columns = ['supp','dose', 'len_min', 'len_max', 'length']\n", "err_df" ] }, { "cell_type": "markdown", "id": "3fd0e4eb-b6cb-4b58-891a-b49e0a446a37", "metadata": {}, "source": [ "#### Flipping an Errorbar" ] }, { "cell_type": "code", "execution_count": 4, "id": "c4550c86-c040-4aaf-909c-5050509887ff", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T11:51:57.960549Z", "iopub.status.busy": "2024-04-26T11:51:57.960549Z", "iopub.status.idle": "2024-04-26T11:51:58.071055Z", "shell.execute_reply": "2024-04-26T11:51:58.071055Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "errorbar_v = geom_errorbar(aes(x='dose', ymin='len_min', ymax='len_max'), position=position_dodge(0.95))\n", "errorbar_h = geom_errorbar(aes(y='dose', xmin='len_min', xmax='len_max'), position=position_dodgev(0.95))\n", "\n", "gggrid([\n", " ggplot(err_df, aes(color='supp')) + errorbar_v + ggtitle(\"Vertical\"),\n", " ggplot(err_df, aes(color='supp')) + errorbar_h + ggtitle(\"Horizontal\")\n", "])" ] }, { "cell_type": "markdown", "id": "51861133-bb8b-47fd-8720-a80d75e0701f", "metadata": {}, "source": [ "#### Other Examples of Horizontal Orientation" ] }, { "cell_type": "code", "execution_count": 5, "id": "7f52c920", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T11:51:58.071055Z", "iopub.status.busy": "2024-04-26T11:51:58.071055Z", "iopub.status.idle": "2024-04-26T11:51:58.102304Z", "shell.execute_reply": "2024-04-26T11:51:58.102304Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = ggplot(err_df, aes(y='dose', x='length', xmin='len_min', xmax='len_max', color='supp')) +\\\n", " xlab(\"Tooth length [mm]\")\n", "\n", "gggrid([\n", " p + geom_crossbar(position=position_dodgev(0.95)) + ggtitle('geom_crossbar()'),\n", " p + geom_pointrange(position=position_dodgev(0.95)) + ggtitle('geom_pointrange()'),\n", " p + geom_linerange(position=position_dodgev(0.95)) + ggtitle('geom_linerange()'),\n", " p + geom_ribbon() + ggtitle('geom_ribbon()')\n", "], ncol=2, vspace=20.0)" ] } ], "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.13" } }, "nbformat": 4, "nbformat_minor": 5 }