{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Horizontal error bars and vertical \"dodge\"\n",
"\n",
"`geom_errorbar()` can be plotted horizontally by assigning `y`,`xmin`,`xmax` aesthetics. The height of the error bar is defined by the `height`.\n",
"\n",
"New type of position adjustment `'dodgev'` is used to adjust the position by dodging overlaps to the side. Function `position_dodgev(height)` allows to set the dodge height.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"execution": {
"iopub.execute_input": "2024-04-17T07:29:32.521815Z",
"iopub.status.busy": "2024-04-17T07:29:32.521734Z",
"iopub.status.idle": "2024-04-17T07:29:32.835649Z",
"shell.execute_reply": "2024-04-17T07:29:32.835359Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
\n",
" \n",
" "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from lets_plot import *\n",
"LetsPlot.setup_html()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 1. The \"Tooth Growth\" Dataset"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The ToothGrowth dataset describes the effect of Vitamin C on tooth growth in guinea pigs. Each animal received one of three dose levels of vitamin C (0.5, 1, and 2 mg/day) by one of two delivery methods: orange juice (OJ) or ascorbic acid (VC)."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"execution": {
"iopub.execute_input": "2024-04-17T07:29:32.849203Z",
"iopub.status.busy": "2024-04-17T07:29:32.849074Z",
"iopub.status.idle": "2024-04-17T07:29:33.140718Z",
"shell.execute_reply": "2024-04-17T07:29:33.140442Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" len | \n",
" supp | \n",
" dose | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 4.2 | \n",
" VC | \n",
" 0.5 | \n",
"
\n",
" \n",
" 1 | \n",
" 11.5 | \n",
" VC | \n",
" 0.5 | \n",
"
\n",
" \n",
" 2 | \n",
" 7.3 | \n",
" VC | \n",
" 0.5 | \n",
"
\n",
" \n",
" 3 | \n",
" 5.8 | \n",
" VC | \n",
" 0.5 | \n",
"
\n",
" \n",
" 4 | \n",
" 6.4 | \n",
" VC | \n",
" 0.5 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" len supp dose\n",
"0 4.2 VC 0.5\n",
"1 11.5 VC 0.5\n",
"2 7.3 VC 0.5\n",
"3 5.8 VC 0.5\n",
"4 6.4 VC 0.5"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import pandas as pd\n",
"\n",
"df = pd.read_csv(\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/ToothGrowth.csv\")\n",
"df.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* len : Tooth length\n",
"* dose : Dose in milligrams (0.5, 1, 2)\n",
"* supp : Supplement type (VC or OJ)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's calculate the mean value of tooth length in each group, minimum and maximum values, and use these information to plot error bars."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"execution": {
"iopub.execute_input": "2024-04-17T07:29:33.141818Z",
"iopub.status.busy": "2024-04-17T07:29:33.141743Z",
"iopub.status.idle": "2024-04-17T07:29:33.147831Z",
"shell.execute_reply": "2024-04-17T07:29:33.147650Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" supp | \n",
" dose | \n",
" length | \n",
" len_min | \n",
" len_max | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" OJ | \n",
" 0.5 | \n",
" 13.23 | \n",
" 8.2 | \n",
" 21.5 | \n",
"
\n",
" \n",
" 1 | \n",
" OJ | \n",
" 1.0 | \n",
" 22.70 | \n",
" 14.5 | \n",
" 27.3 | \n",
"
\n",
" \n",
" 2 | \n",
" OJ | \n",
" 2.0 | \n",
" 26.06 | \n",
" 22.4 | \n",
" 30.9 | \n",
"
\n",
" \n",
" 3 | \n",
" VC | \n",
" 0.5 | \n",
" 7.98 | \n",
" 4.2 | \n",
" 11.5 | \n",
"
\n",
" \n",
" 4 | \n",
" VC | \n",
" 1.0 | \n",
" 16.77 | \n",
" 13.6 | \n",
" 22.5 | \n",
"
\n",
" \n",
" 5 | \n",
" VC | \n",
" 2.0 | \n",
" 26.14 | \n",
" 18.5 | \n",
" 33.9 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" supp dose length len_min len_max\n",
"0 OJ 0.5 13.23 8.2 21.5\n",
"1 OJ 1.0 22.70 14.5 27.3\n",
"2 OJ 2.0 26.06 22.4 30.9\n",
"3 VC 0.5 7.98 4.2 11.5\n",
"4 VC 1.0 16.77 13.6 22.5\n",
"5 VC 2.0 26.14 18.5 33.9"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import numpy as np\n",
"\n",
"data = {}\n",
"\n",
"for supp_lvl in np.unique(df['supp']):\n",
" for dose_lvl in np.unique(df['dose']):\n",
" data_to_sum = df[(df['supp'] == supp_lvl) & (df['dose'] == dose_lvl)]\n",
"\n",
" mean = data_to_sum['len'].mean()\n",
" len_min = data_to_sum['len'].min()\n",
" len_max = data_to_sum['len'].max()\n",
"\n",
" data.setdefault('supp', []).append(supp_lvl)\n",
" data.setdefault('dose', []).append(dose_lvl)\n",
" data.setdefault('length', []).append(mean)\n",
" data.setdefault('len_min', []).append(len_min)\n",
" data.setdefault('len_max', []).append(len_max)\n",
" \n",
"pd.DataFrame(data) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 2. Error Bars without a Position Adjustment"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"execution": {
"iopub.execute_input": "2024-04-17T07:29:33.149076Z",
"iopub.status.busy": "2024-04-17T07:29:33.148930Z",
"iopub.status.idle": "2024-04-17T07:29:33.186082Z",
"shell.execute_reply": "2024-04-17T07:29:33.185797Z"
}
},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
],
"text/plain": [
""
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ggplot(data) + \\\n",
" geom_errorbar(aes(y='dose', xmin='len_min', xmax='len_max', color='supp'), height=0.2, size=1.2) + \\\n",
" scale_color_brewer(palette=\"Set1\") + \\\n",
" labs(x=\"Tooth length [mm]\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 3. Error Bars with `position = 'dodgev'`\n",
"\n",
"\n",
"To fix errorbars overlapping, use `position_dodgev(height)` - to move them vertically."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"execution": {
"iopub.execute_input": "2024-04-17T07:29:33.187602Z",
"iopub.status.busy": "2024-04-17T07:29:33.187356Z",
"iopub.status.idle": "2024-04-17T07:29:33.191028Z",
"shell.execute_reply": "2024-04-17T07:29:33.190801Z"
}
},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
],
"text/plain": [
""
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ggplot(data) + \\\n",
" geom_errorbar(aes(y='dose', xmin='len_min', xmax='len_max', color='supp'), height=0.2, size=1.2,\n",
" position=position_dodgev(0.4)) + \\\n",
" scale_color_brewer(palette=\"Set1\") + \\\n",
" labs(x=\"Tooth length [mm]\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 4. Error Bars on Bar Plot"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"execution": {
"iopub.execute_input": "2024-04-17T07:29:33.192367Z",
"iopub.status.busy": "2024-04-17T07:29:33.192162Z",
"iopub.status.idle": "2024-04-17T07:29:33.195932Z",
"shell.execute_reply": "2024-04-17T07:29:33.195735Z"
}
},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
],
"text/plain": [
""
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ggplot(data, aes(y='dose')) + \\\n",
" geom_bar(aes(x='length', fill='supp'), stat='identity', position='dodge', orientation='y') + \\\n",
" geom_errorbar(aes(xmin='len_min', xmax='len_max', group='supp'), height=0.2, size=1.2,\n",
" position=position_dodgev(0.9)) + \\\n",
" scale_fill_brewer(palette=\"Paired\")\n"
]
}
],
"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": 4
}