{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:31:27.385731Z", "iopub.status.busy": "2024-04-17T07:31:27.385466Z", "iopub.status.idle": "2024-04-17T07:31:27.708722Z", "shell.execute_reply": "2024-04-17T07:31:27.708294Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from lets_plot import *\n", "\n", "LetsPlot.setup_html()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Plotting means and error ranges.\n", "\n", "There are several ways to show error ranges on a plot. Among them are \n", "- *geom_errorbar*\n", "- *geom_crossbar*\n", "- *geom_linerange*\n", "- *geom_pointrange*" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:31:27.721963Z", "iopub.status.busy": "2024-04-17T07:31:27.721821Z", "iopub.status.idle": "2024-04-17T07:31:27.724176Z", "shell.execute_reply": "2024-04-17T07:31:27.723981Z" } }, "outputs": [], "source": [ "# This example was found at: www.cookbook-r.com/Graphs/Plotting_means_and_error_bars_(ggplot2)\n", "data = dict(\n", " supp = ['OJ', 'OJ', 'OJ', 'VC', 'VC', 'VC'],\n", " dose = [0.5, 1.0, 2.0, 0.5, 1.0, 2.0],\n", " length = [13.23, 22.70, 26.06, 7.98, 16.77, 26.14],\n", " len_min = [11.83, 21.2, 24.50, 4.24, 15.26, 23.35],\n", " len_max = [15.63, 24.9, 27.11, 10.72, 19.28, 28.93]\n", ")" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:31:27.725326Z", "iopub.status.busy": "2024-04-17T07:31:27.725211Z", "iopub.status.idle": "2024-04-17T07:31:27.726721Z", "shell.execute_reply": "2024-04-17T07:31:27.726507Z" } }, "outputs": [], "source": [ "p = ggplot(data, aes(x='dose', color='supp'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Error-bars with lines and points." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:31:27.727975Z", "iopub.status.busy": "2024-04-17T07:31:27.727808Z", "iopub.status.idle": "2024-04-17T07:31:27.759567Z", "shell.execute_reply": "2024-04-17T07:31:27.759316Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + geom_errorbar(aes(ymin='len_min', ymax='len_max'), width=.1) \\\n", "+ geom_line(aes(y='length')) \\\n", "+ geom_point(aes(y='length'))" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:31:27.760768Z", "iopub.status.busy": "2024-04-17T07:31:27.760683Z", "iopub.status.idle": "2024-04-17T07:31:27.763883Z", "shell.execute_reply": "2024-04-17T07:31:27.763694Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# The errorbars overlapped, so use position_dodge to move them horizontally\n", "pd = position_dodge(0.1) # move them .05 to the left and right\n", "p + geom_errorbar(aes(ymin='len_min', ymax='len_max'), width=.1, position=pd) \\\n", "+ geom_line(aes(y='length'), position=pd) \\\n", "+ geom_point(aes(y='length'), position=pd)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:31:27.765083Z", "iopub.status.busy": "2024-04-17T07:31:27.764952Z", "iopub.status.idle": "2024-04-17T07:31:27.768046Z", "shell.execute_reply": "2024-04-17T07:31:27.767834Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Black errorbars - notice the mapping of 'group=supp'\n", "# Without it, the errorbars won't be dodged!\n", "p + geom_errorbar(aes(ymin='len_min', ymax='len_max', group='supp'), color='black', width=.1, position=pd) \\\n", "+ geom_line(aes(y='length'), position=pd) \\\n", "+ geom_point(aes(y='length'), position=pd, size=5)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:31:27.769263Z", "iopub.status.busy": "2024-04-17T07:31:27.769119Z", "iopub.status.idle": "2024-04-17T07:31:27.772952Z", "shell.execute_reply": "2024-04-17T07:31:27.772738Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# The finished graph:\n", "# - fixed size\n", "# - point shape # 21 is filled circle \n", "# - position legend in the bottom right\n", "p1 = p \\\n", "+ xlab(\"Dose (mg)\") \\\n", "+ ylab(\"Tooth length (mm)\") \\\n", "+ scale_color_manual(['orange', 'dark_green'], na_value='gray') \\\n", "+ ggsize(700, 400)\n", "p1 + geom_errorbar(aes(ymin='len_min', ymax='len_max', group='supp'), color='black', width=.1, position=pd) \\\n", "+ geom_line(aes(y='length'), position=pd) \\\n", "+ geom_point(aes(y='length'), position=pd, size=5, shape=21, fill=\"white\") \\\n", "+ theme(legend_justification=[1,0], legend_position=[1,0]) \\\n", "+ ggtitle(\"The Effect of Vitamin C on Tooth Growth in Guinea Pigs\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Error-bars on bar plot." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:31:27.774045Z", "iopub.status.busy": "2024-04-17T07:31:27.773925Z", "iopub.status.idle": "2024-04-17T07:31:27.777073Z", "shell.execute_reply": "2024-04-17T07:31:27.776883Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Plot error ranges on Bar plot\n", "p1 \\\n", "+ geom_bar(aes(y='length', fill='supp'), stat='identity', position='dodge', color='black') \\\n", "+ geom_errorbar(aes(ymin='len_min', ymax='len_max', group='supp'), color='black', width=.1, position=position_dodge(0.9)) \\\n", "+ theme(legend_justification=[0,1], legend_position=[0,1])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Crossbars." ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:31:27.778157Z", "iopub.status.busy": "2024-04-17T07:31:27.778034Z", "iopub.status.idle": "2024-04-17T07:31:27.780414Z", "shell.execute_reply": "2024-04-17T07:31:27.780222Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Thickness of the horizontal mid-line can be adjusted using `fatten` parameter.\n", "p1 + geom_crossbar(aes(ymin='len_min', ymax='len_max', y='length', color='supp'), fatten=5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Line-range." ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:31:27.781414Z", "iopub.status.busy": "2024-04-17T07:31:27.781291Z", "iopub.status.idle": "2024-04-17T07:31:27.783854Z", "shell.execute_reply": "2024-04-17T07:31:27.783665Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p1 \\\n", "+ geom_linerange(aes(ymin='len_min', ymax='len_max', color='supp'), position=pd) \\\n", "+ geom_line(aes(y='length'), position=pd)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Point-range" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:31:27.784858Z", "iopub.status.busy": "2024-04-17T07:31:27.784738Z", "iopub.status.idle": "2024-04-17T07:31:27.787397Z", "shell.execute_reply": "2024-04-17T07:31:27.787202Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Point-range is the same as line-range but with an added mid-point.\n", "p1 \\\n", "+ geom_pointrange(aes(y='length', ymin='len_min', ymax='len_max', color='supp'), position=pd) \\\n", "+ geom_line(aes(y='length'), position=pd)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:31:27.788369Z", "iopub.status.busy": "2024-04-17T07:31:27.788250Z", "iopub.status.idle": "2024-04-17T07:31:27.791173Z", "shell.execute_reply": "2024-04-17T07:31:27.790984Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Size of the mid-point can be adjuasted using `fatten` parameter - multiplication factor relative to the line size.\n", "p1 \\\n", "+ geom_line(aes(y='length'), position=pd) \\\n", "+ geom_pointrange(aes(y='length', ymin='len_min', ymax='len_max', fill='supp'), position=pd, color='rgb(230, 230, 230)', size=5, shape=23, fatten=1) \\\n", "+ scale_fill_manual(['orange', 'dark_green'], na_value='gray')" ] } ], "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": 2 }