{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"a8uVag\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"library\">\n",
       "       if(!window.letsPlotCallQueue) {\n",
       "           window.letsPlotCallQueue = [];\n",
       "       }; \n",
       "       window.letsPlotCall = function(f) {\n",
       "           window.letsPlotCallQueue.push(f);\n",
       "       };\n",
       "       (function() {\n",
       "           var script = document.createElement(\"script\");\n",
       "           script.type = \"text/javascript\";\n",
       "           script.src = \"https://cdn.jsdelivr.net/gh/JetBrains/lets-plot@v4.0.0/js-package/distr/lets-plot.min.js\";\n",
       "           script.onload = function() {\n",
       "               window.letsPlotCall = function(f) {f();};\n",
       "               window.letsPlotCallQueue.forEach(function(f) {f();});\n",
       "               window.letsPlotCallQueue = [];\n",
       "               \n",
       "               \n",
       "           };\n",
       "           script.onerror = function(event) {\n",
       "               window.letsPlotCall = function(f) {};\n",
       "               window.letsPlotCallQueue = [];\n",
       "               var div = document.createElement(\"div\");\n",
       "               div.style.color = 'darkred';\n",
       "               div.textContent = 'Error loading Lets-Plot JS';\n",
       "               document.getElementById(\"a8uVag\").appendChild(div);\n",
       "           };\n",
       "           var e = document.getElementById(\"a8uVag\");\n",
       "           e.appendChild(script);\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%useLatestDescriptors\n",
    "%use lets-plot"
   ]
  },
  {
   "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",
    "- `geomErrorBar()`\n",
    "- `geomCrossbar()`\n",
    "- `geomLineRange()`\n",
    "- `geomPointRange()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "// This example was found at: www.cookbook-r.com/Graphs/Plotting_means_and_error_bars_(ggplot2)\n",
    "val data = mapOf(\n",
    "    \"supp\" to listOf(\"OJ\", \"OJ\", \"OJ\", \"VC\", \"VC\", \"VC\"),\n",
    "    \"dose\" to listOf(0.5, 1.0, 2.0, 0.5, 1.0, 2.0),\n",
    "    \"length\" to listOf(13.23, 22.70, 26.06, 7.98, 16.77, 26.14),\n",
    "    \"len_min\" to listOf(11.83, 21.2, 24.50, 4.24, 15.26, 23.35),\n",
    "    \"len_max\" to listOf(15.63, 24.9, 27.11, 10.72, 19.28, 28.93)\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "val p = letsPlot(data) {x=\"dose\"; color=\"supp\"}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Error-bars with lines and points."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"hQRTej\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "\"x\":\"dose\",\n",
       "\"color\":\"supp\"\n",
       "},\n",
       "\"data\":{\n",
       "\"dose\":[0.5,1.0,2.0,0.5,1.0,2.0],\n",
       "\"supp\":[\"OJ\",\"OJ\",\"OJ\",\"VC\",\"VC\",\"VC\"],\n",
       "\"length\":[13.23,22.7,26.06,7.98,16.77,26.14],\n",
       "\"len_min\":[11.83,21.2,24.5,4.24,15.26,23.35],\n",
       "\"len_max\":[15.63,24.9,27.11,10.72,19.28,28.93]\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "\"ymin\":\"len_min\",\n",
       "\"ymax\":\"len_max\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"width\":0.1,\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"errorbar\",\n",
       "\"data\":{\n",
       "}\n",
       "},{\n",
       "\"mapping\":{\n",
       "\"y\":\"length\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"line\",\n",
       "\"data\":{\n",
       "}\n",
       "},{\n",
       "\"mapping\":{\n",
       "\"y\":\"length\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"point\",\n",
       "\"data\":{\n",
       "}\n",
       "}]\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"hQRTej\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();    \n",
       "   </script>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p + geomErrorBar(width = .1) {ymin = \"len_min\"; ymax = \"len_max\"} +\n",
    "    geomLine {y = \"length\"} +\n",
    "    geomPoint {y = \"length\"}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"aElEDv\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "\"x\":\"dose\",\n",
       "\"color\":\"supp\"\n",
       "},\n",
       "\"data\":{\n",
       "\"dose\":[0.5,1.0,2.0,0.5,1.0,2.0],\n",
       "\"supp\":[\"OJ\",\"OJ\",\"OJ\",\"VC\",\"VC\",\"VC\"],\n",
       "\"length\":[13.23,22.7,26.06,7.98,16.77,26.14],\n",
       "\"len_min\":[11.83,21.2,24.5,4.24,15.26,23.35],\n",
       "\"len_max\":[15.63,24.9,27.11,10.72,19.28,28.93]\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "\"ymin\":\"len_min\",\n",
       "\"ymax\":\"len_max\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"width\":0.1,\n",
       "\"position\":{\n",
       "\"name\":\"dodge\",\n",
       "\"width\":0.1\n",
       "},\n",
       "\"geom\":\"errorbar\",\n",
       "\"data\":{\n",
       "}\n",
       "},{\n",
       "\"mapping\":{\n",
       "\"y\":\"length\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"position\":{\n",
       "\"name\":\"dodge\",\n",
       "\"width\":0.1\n",
       "},\n",
       "\"geom\":\"line\",\n",
       "\"data\":{\n",
       "}\n",
       "},{\n",
       "\"mapping\":{\n",
       "\"y\":\"length\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"position\":{\n",
       "\"name\":\"dodge\",\n",
       "\"width\":0.1\n",
       "},\n",
       "\"geom\":\"point\",\n",
       "\"data\":{\n",
       "}\n",
       "}]\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"aElEDv\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();    \n",
       "   </script>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "// The errorbars overlapped, so use position_dodge to move them horizontally\n",
    "val pd = positionDodge(0.1) // move them .05 to the left and right\n",
    "p + geomErrorBar(width=.1, position=pd) {ymin=\"len_min\"; ymax=\"len_max\"} +\n",
    "    geomLine(position=pd) {y=\"length\"} +\n",
    "    geomPoint(position=pd) {y=\"length\"}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"xlmfLl\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "\"x\":\"dose\",\n",
       "\"color\":\"supp\"\n",
       "},\n",
       "\"data\":{\n",
       "\"dose\":[0.5,1.0,2.0,0.5,1.0,2.0],\n",
       "\"supp\":[\"OJ\",\"OJ\",\"OJ\",\"VC\",\"VC\",\"VC\"],\n",
       "\"length\":[13.23,22.7,26.06,7.98,16.77,26.14],\n",
       "\"len_min\":[11.83,21.2,24.5,4.24,15.26,23.35],\n",
       "\"len_max\":[15.63,24.9,27.11,10.72,19.28,28.93]\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "\"ymin\":\"len_min\",\n",
       "\"ymax\":\"len_max\",\n",
       "\"group\":\"supp\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"color\":\"pen\",\n",
       "\"width\":0.1,\n",
       "\"position\":{\n",
       "\"name\":\"dodge\",\n",
       "\"width\":0.1\n",
       "},\n",
       "\"geom\":\"errorbar\",\n",
       "\"data\":{\n",
       "}\n",
       "},{\n",
       "\"mapping\":{\n",
       "\"y\":\"length\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"position\":{\n",
       "\"name\":\"dodge\",\n",
       "\"width\":0.1\n",
       "},\n",
       "\"geom\":\"line\",\n",
       "\"data\":{\n",
       "}\n",
       "},{\n",
       "\"mapping\":{\n",
       "\"y\":\"length\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"size\":5.0,\n",
       "\"position\":{\n",
       "\"name\":\"dodge\",\n",
       "\"width\":0.1\n",
       "},\n",
       "\"geom\":\"point\",\n",
       "\"data\":{\n",
       "}\n",
       "}]\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"xlmfLl\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();    \n",
       "   </script>"
      ]
     },
     "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 + geomErrorBar(\n",
    "    color = \"pen\", \n",
    "    width = .1, \n",
    "    position = pd) {ymin = \"len_min\"; ymax = \"len_max\"; group = \"supp\"} +\n",
    "    geomLine(position = pd) {y = \"length\"} +\n",
    "    geomPoint(position = pd, size = 5) {y = \"length\"}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"UFCLWN\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"ggtitle\":{\n",
       "\"text\":\"The Effect of Vitamin C on Tooth Growth in Guinea Pigs\"\n",
       "},\n",
       "\"mapping\":{\n",
       "\"x\":\"dose\",\n",
       "\"color\":\"supp\"\n",
       "},\n",
       "\"data\":{\n",
       "\"dose\":[0.5,1.0,2.0,0.5,1.0,2.0],\n",
       "\"supp\":[\"OJ\",\"OJ\",\"OJ\",\"VC\",\"VC\",\"VC\"],\n",
       "\"length\":[13.23,22.7,26.06,7.98,16.77,26.14],\n",
       "\"len_min\":[11.83,21.2,24.5,4.24,15.26,23.35],\n",
       "\"len_max\":[15.63,24.9,27.11,10.72,19.28,28.93]\n",
       "},\n",
       "\"ggsize\":{\n",
       "\"width\":700.0,\n",
       "\"height\":400.0\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[{\n",
       "\"aesthetic\":\"x\",\n",
       "\"name\":\"Dose (mg)\"\n",
       "},{\n",
       "\"aesthetic\":\"y\",\n",
       "\"name\":\"Tooth length (mm)\"\n",
       "},{\n",
       "\"aesthetic\":\"color\",\n",
       "\"na_value\":\"gray\",\n",
       "\"values\":[\"orange\",\"dark_green\"]\n",
       "}],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "\"ymin\":\"len_min\",\n",
       "\"ymax\":\"len_max\",\n",
       "\"group\":\"supp\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"color\":\"pen\",\n",
       "\"width\":0.1,\n",
       "\"position\":{\n",
       "\"name\":\"dodge\",\n",
       "\"width\":0.1\n",
       "},\n",
       "\"geom\":\"errorbar\",\n",
       "\"data\":{\n",
       "}\n",
       "},{\n",
       "\"mapping\":{\n",
       "\"y\":\"length\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"position\":{\n",
       "\"name\":\"dodge\",\n",
       "\"width\":0.1\n",
       "},\n",
       "\"geom\":\"line\",\n",
       "\"data\":{\n",
       "}\n",
       "},{\n",
       "\"mapping\":{\n",
       "\"y\":\"length\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"shape\":21.0,\n",
       "\"size\":5.0,\n",
       "\"position\":{\n",
       "\"name\":\"dodge\",\n",
       "\"width\":0.1\n",
       "},\n",
       "\"geom\":\"point\",\n",
       "\"data\":{\n",
       "}\n",
       "}],\n",
       "\"theme\":{\n",
       "\"legend_justification\":[1.0,0.0],\n",
       "\"legend_position\":[1.0,0.0]\n",
       "}\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"UFCLWN\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();    \n",
       "   </script>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "// Finished graph\n",
    "// - fixed size\n",
    "// - point shape # 21 is filled circle \n",
    "// - position legend in bottom right\n",
    "val p1 = p +\n",
    "    xlab(\"Dose (mg)\") +\n",
    "    ylab(\"Tooth length (mm)\") +\n",
    "    scaleColorManual(listOf(\"orange\", \"dark_green\"), naValue = \"gray\") +\n",
    "    ggsize(700, 400)\n",
    "p1 + geomErrorBar(color = \"pen\", \n",
    "                  width = .1, \n",
    "                  position = pd) {ymin = \"len_min\"; ymax = \"len_max\"; group = \"supp\"} +\n",
    "    geomLine(position = pd) {y = \"length\"} +\n",
    "    geomPoint(position = pd, size = 5, shape = 21) {y = \"length\"} +\n",
    "    theme().legendJustification(1,0).legendPosition(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": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"Mmyl0h\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "\"x\":\"dose\",\n",
       "\"color\":\"supp\"\n",
       "},\n",
       "\"data\":{\n",
       "\"dose\":[0.5,1.0,2.0,0.5,1.0,2.0],\n",
       "\"supp\":[\"OJ\",\"OJ\",\"OJ\",\"VC\",\"VC\",\"VC\"],\n",
       "\"length\":[13.23,22.7,26.06,7.98,16.77,26.14],\n",
       "\"len_min\":[11.83,21.2,24.5,4.24,15.26,23.35],\n",
       "\"len_max\":[15.63,24.9,27.11,10.72,19.28,28.93]\n",
       "},\n",
       "\"ggsize\":{\n",
       "\"width\":700.0,\n",
       "\"height\":400.0\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[{\n",
       "\"aesthetic\":\"x\",\n",
       "\"name\":\"Dose (mg)\"\n",
       "},{\n",
       "\"aesthetic\":\"y\",\n",
       "\"name\":\"Tooth length (mm)\"\n",
       "},{\n",
       "\"aesthetic\":\"color\",\n",
       "\"na_value\":\"gray\",\n",
       "\"values\":[\"orange\",\"dark_green\"]\n",
       "}],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "\"y\":\"length\",\n",
       "\"fill\":\"supp\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"color\":\"pen\",\n",
       "\"position\":\"dodge\",\n",
       "\"geom\":\"bar\",\n",
       "\"data\":{\n",
       "}\n",
       "},{\n",
       "\"mapping\":{\n",
       "\"ymin\":\"len_min\",\n",
       "\"ymax\":\"len_max\",\n",
       "\"group\":\"supp\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"color\":\"pen\",\n",
       "\"width\":0.1,\n",
       "\"position\":{\n",
       "\"name\":\"dodge\",\n",
       "\"width\":0.9\n",
       "},\n",
       "\"geom\":\"errorbar\",\n",
       "\"data\":{\n",
       "}\n",
       "}],\n",
       "\"theme\":{\n",
       "\"legend_justification\":[0.0,1.0],\n",
       "\"legend_position\":[0.0,1.0]\n",
       "}\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"Mmyl0h\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();    \n",
       "   </script>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "// Plot error ranges on Bar plot\n",
    "p1 + \n",
    "    geomBar(stat = Stat.identity, \n",
    "            position = positionDodge(), \n",
    "            color = \"pen\") {y = \"length\"; fill = \"supp\"} +\n",
    "    geomErrorBar(width = .1, \n",
    "                 position = positionDodge(0.9),\n",
    "                 color = \"pen\") {\n",
    "        ymin = \"len_min\"\n",
    "        ymax = \"len_max\" \n",
    "        group = \"supp\"} +\n",
    "    theme().legendJustification(0,1).legendPosition(0,1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Crossbars."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"DI02MA\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "\"x\":\"dose\",\n",
       "\"color\":\"supp\"\n",
       "},\n",
       "\"data\":{\n",
       "\"dose\":[0.5,1.0,2.0,0.5,1.0,2.0],\n",
       "\"supp\":[\"OJ\",\"OJ\",\"OJ\",\"VC\",\"VC\",\"VC\"],\n",
       "\"length\":[13.23,22.7,26.06,7.98,16.77,26.14],\n",
       "\"len_min\":[11.83,21.2,24.5,4.24,15.26,23.35],\n",
       "\"len_max\":[15.63,24.9,27.11,10.72,19.28,28.93]\n",
       "},\n",
       "\"ggsize\":{\n",
       "\"width\":700.0,\n",
       "\"height\":400.0\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[{\n",
       "\"aesthetic\":\"x\",\n",
       "\"name\":\"Dose (mg)\"\n",
       "},{\n",
       "\"aesthetic\":\"y\",\n",
       "\"name\":\"Tooth length (mm)\"\n",
       "},{\n",
       "\"aesthetic\":\"color\",\n",
       "\"na_value\":\"gray\",\n",
       "\"values\":[\"orange\",\"dark_green\"]\n",
       "}],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "\"ymin\":\"len_min\",\n",
       "\"ymax\":\"len_max\",\n",
       "\"y\":\"length\",\n",
       "\"color\":\"supp\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"position\":\"dodge\",\n",
       "\"geom\":\"crossbar\",\n",
       "\"fatten\":5.0,\n",
       "\"data\":{\n",
       "}\n",
       "}]\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"DI02MA\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();    \n",
       "   </script>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "// Thickness of the horizontal mid-line can be adjusted using `fatten` parameter.\n",
    "p1 + geomCrossbar(fatten = 5.0) {\n",
    "    ymin=\"len_min\"\n",
    "    ymax=\"len_max\"\n",
    "    y=\"length\"\n",
    "    color=\"supp\"\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Line-range."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"RIZFjN\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "\"x\":\"dose\",\n",
       "\"color\":\"supp\"\n",
       "},\n",
       "\"data\":{\n",
       "\"dose\":[0.5,1.0,2.0,0.5,1.0,2.0],\n",
       "\"supp\":[\"OJ\",\"OJ\",\"OJ\",\"VC\",\"VC\",\"VC\"],\n",
       "\"length\":[13.23,22.7,26.06,7.98,16.77,26.14],\n",
       "\"len_min\":[11.83,21.2,24.5,4.24,15.26,23.35],\n",
       "\"len_max\":[15.63,24.9,27.11,10.72,19.28,28.93]\n",
       "},\n",
       "\"ggsize\":{\n",
       "\"width\":700.0,\n",
       "\"height\":400.0\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[{\n",
       "\"aesthetic\":\"x\",\n",
       "\"name\":\"Dose (mg)\"\n",
       "},{\n",
       "\"aesthetic\":\"y\",\n",
       "\"name\":\"Tooth length (mm)\"\n",
       "},{\n",
       "\"aesthetic\":\"color\",\n",
       "\"na_value\":\"gray\",\n",
       "\"values\":[\"orange\",\"dark_green\"]\n",
       "}],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "\"ymin\":\"len_min\",\n",
       "\"ymax\":\"len_max\",\n",
       "\"color\":\"supp\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"position\":{\n",
       "\"name\":\"dodge\",\n",
       "\"width\":0.1\n",
       "},\n",
       "\"geom\":\"linerange\",\n",
       "\"data\":{\n",
       "}\n",
       "},{\n",
       "\"mapping\":{\n",
       "\"y\":\"length\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"position\":{\n",
       "\"name\":\"dodge\",\n",
       "\"width\":0.1\n",
       "},\n",
       "\"geom\":\"line\",\n",
       "\"data\":{\n",
       "}\n",
       "}]\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"RIZFjN\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();    \n",
       "   </script>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p1 + geomLineRange(position=pd) {ymin=\"len_min\"; ymax=\"len_max\"; color=\"supp\"} +\n",
    "     geomLine(position=pd) {y=\"length\"}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Point-range."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"z2MSuO\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "\"x\":\"dose\",\n",
       "\"color\":\"supp\"\n",
       "},\n",
       "\"data\":{\n",
       "\"dose\":[0.5,1.0,2.0,0.5,1.0,2.0],\n",
       "\"supp\":[\"OJ\",\"OJ\",\"OJ\",\"VC\",\"VC\",\"VC\"],\n",
       "\"length\":[13.23,22.7,26.06,7.98,16.77,26.14],\n",
       "\"len_min\":[11.83,21.2,24.5,4.24,15.26,23.35],\n",
       "\"len_max\":[15.63,24.9,27.11,10.72,19.28,28.93]\n",
       "},\n",
       "\"ggsize\":{\n",
       "\"width\":700.0,\n",
       "\"height\":400.0\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[{\n",
       "\"aesthetic\":\"x\",\n",
       "\"name\":\"Dose (mg)\"\n",
       "},{\n",
       "\"aesthetic\":\"y\",\n",
       "\"name\":\"Tooth length (mm)\"\n",
       "},{\n",
       "\"aesthetic\":\"color\",\n",
       "\"na_value\":\"gray\",\n",
       "\"values\":[\"orange\",\"dark_green\"]\n",
       "}],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "\"y\":\"length\",\n",
       "\"ymin\":\"len_min\",\n",
       "\"ymax\":\"len_max\",\n",
       "\"color\":\"supp\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"position\":{\n",
       "\"name\":\"dodge\",\n",
       "\"width\":0.1\n",
       "},\n",
       "\"geom\":\"pointrange\",\n",
       "\"data\":{\n",
       "}\n",
       "},{\n",
       "\"mapping\":{\n",
       "\"y\":\"length\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"position\":{\n",
       "\"name\":\"dodge\",\n",
       "\"width\":0.1\n",
       "},\n",
       "\"geom\":\"line\",\n",
       "\"data\":{\n",
       "}\n",
       "}]\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"z2MSuO\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();    \n",
       "   </script>"
      ]
     },
     "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 + geomPointRange(position=pd) {y=\"length\"; ymin=\"len_min\"; ymax=\"len_max\"; color=\"supp\"} +\n",
    "     geomLine(position=pd) {y=\"length\"}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"kxhwsr\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "\"x\":\"dose\",\n",
       "\"color\":\"supp\"\n",
       "},\n",
       "\"data\":{\n",
       "\"dose\":[0.5,1.0,2.0,0.5,1.0,2.0],\n",
       "\"supp\":[\"OJ\",\"OJ\",\"OJ\",\"VC\",\"VC\",\"VC\"],\n",
       "\"length\":[13.23,22.7,26.06,7.98,16.77,26.14],\n",
       "\"len_min\":[11.83,21.2,24.5,4.24,15.26,23.35],\n",
       "\"len_max\":[15.63,24.9,27.11,10.72,19.28,28.93]\n",
       "},\n",
       "\"ggsize\":{\n",
       "\"width\":700.0,\n",
       "\"height\":400.0\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[{\n",
       "\"aesthetic\":\"x\",\n",
       "\"name\":\"Dose (mg)\"\n",
       "},{\n",
       "\"aesthetic\":\"y\",\n",
       "\"name\":\"Tooth length (mm)\"\n",
       "},{\n",
       "\"aesthetic\":\"color\",\n",
       "\"na_value\":\"gray\",\n",
       "\"values\":[\"orange\",\"dark_green\"]\n",
       "},{\n",
       "\"aesthetic\":\"fill\",\n",
       "\"na_value\":\"gray\",\n",
       "\"values\":[\"orange\",\"dark_green\"]\n",
       "}],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "\"y\":\"length\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"position\":{\n",
       "\"name\":\"dodge\",\n",
       "\"width\":0.1\n",
       "},\n",
       "\"geom\":\"line\",\n",
       "\"data\":{\n",
       "}\n",
       "},{\n",
       "\"mapping\":{\n",
       "\"y\":\"length\",\n",
       "\"ymin\":\"len_min\",\n",
       "\"ymax\":\"len_max\",\n",
       "\"fill\":\"supp\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"color\":\"rgb(230, 230, 230)\",\n",
       "\"shape\":23.0,\n",
       "\"size\":5.0,\n",
       "\"position\":{\n",
       "\"name\":\"dodge\",\n",
       "\"width\":0.1\n",
       "},\n",
       "\"geom\":\"pointrange\",\n",
       "\"linewidth\":5.0,\n",
       "\"fatten\":1.0,\n",
       "\"data\":{\n",
       "}\n",
       "}]\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"kxhwsr\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();    \n",
       "   </script>"
      ]
     },
     "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 + geomLine(position = pd) {y = \"length\"} +\n",
    "     geomPointRange(position = pd, \n",
    "                    color = \"rgb(230, 230, 230)\", \n",
    "                    size = 5, \n",
    "                    linewidth = 5, \n",
    "                    shape = 23, fatten = 1.0) {\n",
    "         y = \"length\"\n",
    "         ymin = \"len_min\"\n",
    "         ymax = \"len_max\"\n",
    "         fill = \"supp\"\n",
    "     } +\n",
    "     scaleFillManual(listOf(\"orange\", \"dark_green\"), naValue=\"gray\")"
   ]
  }
 ],
 "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": 4
}