{ "cells": [ { "cell_type": "markdown", "id": "5404084f", "metadata": {}, "source": [ "# Horizontal error bars and vertical \"dodge\"\n", "\n", "`geomErrorBar()` 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 `positionDodgeV(height)` allows to set the dodge height.\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "510d7c07", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%useLatestDescriptors\n", "%use lets-plot" ] }, { "cell_type": "code", "execution_count": 2, "id": "066011c5", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Lets-Plot Kotlin API v.4.4.0. Frontend: Notebook with dynamically loaded JS. Lets-Plot JS v.3.2.0." ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "LetsPlot.getInfo()" ] }, { "cell_type": "code", "execution_count": 3, "id": "71f82eba", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%use dataframe" ] }, { "cell_type": "markdown", "id": "60d028be", "metadata": {}, "source": [ "#### 1. The \"Tooth Growth\" Dataset" ] }, { "cell_type": "markdown", "id": "5bf90046", "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": 4, "id": "cc81a6e2", "metadata": {}, "outputs": [ { "data": { "application/kotlindataframe+json": "{\"nrow\":3,\"ncol\":3,\"columns\":[\"len\",\"supp\",\"dose\"],\"kotlin_dataframe\":[{\"len\":4.2,\"supp\":\"VC\",\"dose\":0.5},{\"len\":11.5,\"supp\":\"VC\",\"dose\":0.5},{\"len\":7.3,\"supp\":\"VC\",\"dose\":0.5}]}", "text/html": [ " \n", " \n", " \n", " \n", " \n", " \n", "
\n", "\n", "

DataFrame: rowsCount = 3, columnsCount = 3

\n", " \n", " \n", " " ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val df = DataFrame.readCSV(\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/ToothGrowth.csv\")\n", "df.head(3)" ] }, { "cell_type": "markdown", "id": "df928797", "metadata": {}, "source": [ "* len : Tooth length\n", "* dose : Dose in milligrams (0.5, 1, 2)\n", "* supp : Supplement type (VC or OJ)" ] }, { "cell_type": "markdown", "id": "2f963817", "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": 5, "id": "5f2247f1", "metadata": {}, "outputs": [ { "data": { "application/kotlindataframe+json": "{\"nrow\":6,\"ncol\":5,\"columns\":[\"supp\",\"dose\",\"length\",\"len_min\",\"len_max\"],\"kotlin_dataframe\":[{\"supp\":\"OJ\",\"dose\":0.5,\"length\":13.23,\"len_min\":8.2,\"len_max\":21.5},{\"supp\":\"OJ\",\"dose\":1.0,\"length\":22.7,\"len_min\":14.5,\"len_max\":27.3},{\"supp\":\"OJ\",\"dose\":2.0,\"length\":26.060000000000002,\"len_min\":22.4,\"len_max\":30.9},{\"supp\":\"VC\",\"dose\":0.5,\"length\":7.980000000000001,\"len_min\":4.2,\"len_max\":11.5},{\"supp\":\"VC\",\"dose\":1.0,\"length\":16.77,\"len_min\":13.6,\"len_max\":22.5},{\"supp\":\"VC\",\"dose\":2.0,\"length\":26.139999999999997,\"len_min\":18.5,\"len_max\":33.9}]}", "text/html": [ " \n", " \n", " \n", " \n", " \n", " \n", "
\n", "\n", "

DataFrame: rowsCount = 6, columnsCount = 5

\n", " \n", " \n", " " ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val resultDf = df.groupBy { supp and dose }.aggregate {\n", " mean { len } into \"length\"\n", " min { len } into \"len_min\"\n", " max { len } into \"len_max\"\n", "}.sortBy { supp }\n", "resultDf" ] }, { "cell_type": "markdown", "id": "e2d3e48f", "metadata": {}, "source": [ "#### 2. Error Bars without a Position Adjustment" ] }, { "cell_type": "code", "execution_count": 6, "id": "c6e19e89", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "letsPlot(resultDf.toMap()) +\n", " geomErrorBar(height = 0.2, size = 1.2) { y = \"dose\"; xmin = \"len_min\"; xmax = \"len_max\"; color = \"supp\" } +\n", " scaleColorBrewer(palette = \"Set1\") +\n", " labs(x = \"Tooth length [mm]\")" ] }, { "cell_type": "markdown", "id": "ca506bc7", "metadata": {}, "source": [ "#### 3. Error Bars with position = 'dodgev'\n", "\n", "To fix errorbars overlapping, use `positionDodgeV(height)` - to move them vertically." ] }, { "cell_type": "code", "execution_count": 7, "id": "8e72eefb", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "letsPlot(resultDf.toMap()) +\n", " geomErrorBar(height = 0.2, size = 1.2, position = positionDodgeV(0.4)) {\n", " y = \"dose\"; xmin = \"len_min\"; xmax = \"len_max\"; color = \"supp\" \n", " } +\n", " scaleColorBrewer(palette = \"Set1\") +\n", " labs(x = \"Tooth length [mm]\")" ] }, { "cell_type": "markdown", "id": "59c06553", "metadata": {}, "source": [ "#### 4. Error Bars on Bar Plot" ] }, { "cell_type": "code", "execution_count": 8, "id": "605839ac", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "letsPlot(resultDf.toMap()) { y = \"dose\" } +\n", " geomBar(stat = Stat.identity, position = positionDodge(), orientation = \"y\") {\n", " x = \"length\"; fill = \"supp\"\n", " } +\n", " geomErrorBar(height = 0.2, size=1.2, position = positionDodgeV(0.9)) {\n", " xmin = \"len_min\"; xmax = \"len_max\"; group = \"supp\" \n", " } +\n", " scaleFillBrewer(palette = \"Paired\")" ] } ], "metadata": { "kernelspec": { "display_name": "Kotlin", "language": "kotlin", "name": "kotlin" }, "language_info": { "codemirror_mode": "text/x-kotlin", "file_extension": ".kt", "mimetype": "text/x-kotlin", "name": "kotlin", "nbconvert_exporter": "", "pygments_lexer": "kotlin", "version": "1.8.20" } }, "nbformat": 4, "nbformat_minor": 5 }