{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"hbNudh\"></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@v2.3.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(\"hbNudh\").appendChild(div);\n",
       "           };\n",
       "           var e = document.getElementById(\"hbNudh\");\n",
       "           e.appendChild(script);\n",
       "       })();\n",
       "   </script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%useLatestDescriptors\n",
    "%use lets-plot\n",
    "import java.util.Random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "// This example was found at: \n",
    "// www.cookbook-r.com/Graphs/Scatterplots_(ggplot2)\n",
    "\n",
    "val rand = java.util.Random(123)\n",
    "val n = 20\n",
    "val data = mapOf<String, List<*>>(\n",
    "    \"cond\" to List(n / 2) { \"A\" } + List(n / 2) { \"B\" },\n",
    "    \"xvar\" to List(n) { i:Int-> i }, \n",
    "    \"yvar\" to List(n) { i:Int-> i + rand.nextGaussian() * 3 }\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Basic scatter plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"IxALrQ\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "\"x\":\"xvar\",\n",
       "\"y\":\"yvar\"\n",
       "},\n",
       "\"data\":{\n",
       "\"yvar\":[-4.31414792742272,2.9025852255330413,2.678186038496493,3.8323801422104644,4.552957466631817,3.9043586677544218,10.056090493036294,8.077708681007955,7.384161947842241,12.052485955548157,14.114693281686685,5.329404481450428,10.598634211929852,10.98677740976943,8.96180814139117,14.194166026725489,15.099984576207884,12.734927210327559,18.2859058065098,17.832111113004313],\n",
       "\"xvar\":[0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0,13.0,14.0,15.0,16.0,17.0,18.0,19.0]\n",
       "},\n",
       "\"ggsize\":{\n",
       "\"width\":300.0,\n",
       "\"height\":250.0\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"shape\":1.0,\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"point\",\n",
       "\"data\":{\n",
       "}\n",
       "}]\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"IxALrQ\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();    \n",
       "   </script>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val p = letsPlot(data) { x = \"xvar\"; y = \"yvar\" } + ggsize(300, 250)\n",
    "p + geomPoint(shape = 1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Add regression line"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"rybpJ4\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "\"x\":\"xvar\",\n",
       "\"y\":\"yvar\"\n",
       "},\n",
       "\"data\":{\n",
       "\"yvar\":[-4.31414792742272,2.9025852255330413,2.678186038496493,3.8323801422104644,4.552957466631817,3.9043586677544218,10.056090493036294,8.077708681007955,7.384161947842241,12.052485955548157,14.114693281686685,5.329404481450428,10.598634211929852,10.98677740976943,8.96180814139117,14.194166026725489,15.099984576207884,12.734927210327559,18.2859058065098,17.832111113004313],\n",
       "\"xvar\":[0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0,13.0,14.0,15.0,16.0,17.0,18.0,19.0]\n",
       "},\n",
       "\"ggsize\":{\n",
       "\"width\":300.0,\n",
       "\"height\":250.0\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"shape\":1.0,\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"point\",\n",
       "\"data\":{\n",
       "}\n",
       "},{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"smooth\",\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"smooth\",\n",
       "\"data\":{\n",
       "\"yvar\":[0.776022824006203,0.983294624600528,1.190566425194853,1.3978382257891777,1.6051100263835028,1.8123818269778276,2.0196536275721524,2.2269254281664774,2.4341972287608025,2.641469029355127,2.848740829949452,3.0560126305437767,3.263284431138102,3.470556231732427,3.677828032326752,3.885099832921077,4.092371633515402,4.299643434109727,4.506915234704051,4.714187035298377,4.921458835892701,5.128730636487027,5.33600243708135,5.543274237675676,5.750546038270001,5.957817838864326,6.165089639458651,6.372361440052975,6.579633240647301,6.786905041241625,6.994176841835951,7.201448642430275,7.4087204430246,7.615992243618925,7.823264044213251,8.030535844807575,8.237807645401901,8.445079445996225,8.65235124659055,8.859623047184874,9.0668948477792,9.274166648373525,9.48143844896785,9.688710249562174,9.895982050156498,10.103253850750825,10.310525651345149,10.517797451939474,10.7250692525338,10.932341053128123,11.139612853722449,11.346884654316773,11.5541564549111,11.761428255505423,11.968700056099747,12.175971856694073,12.383243657288398,12.590515457882724,12.797787258477047,13.005059059071373,13.212330859665698,13.419602660260022,13.626874460854348,13.834146261448673,14.041418062042997,14.248689862637322,14.455961663231648,14.663233463825971,14.870505264420299,15.077777065014622,15.285048865608948,15.492320666203272,15.699592466797597,15.90686426739192,16.114136067986244,16.32140786858057,16.5286796691749,16.73595146976922,16.943223270363546,17.150495070957874],\n",
       "\"..ymax..\":[3.258485241523943,3.4199897415052227,3.581825353762838,3.7440113154214836,3.9065682167322344,4.069518103253915,4.232884584745278,4.3966929507935495,4.560970293066146,4.725745633890145,4.891050060629045,5.056916865027747,5.223381686322768,5.390482656452605,5.558260545139937,5.726758901940309,5.896024191550464,6.066105917736772,6.2370567301797815,6.408932507344822,6.5817924072049925,6.755698876306443,6.930717606346796,7.106917426238628,7.28437011669069,7.463150133839956,7.643334228625101,7.825000949653668,8.008230019537606,8.193101578290534,8.379695292565025,8.56808933630808,8.758359256694645,8.950576748592788,9.144808370680487,9.341114245769882,9.539546795796424,9.740149567117301,9.942956203159767,10.147989618301676,10.355261418896001,10.564771604942742,10.776508570088927,10.990449399956697,11.206560451118804,11.424798177218062,11.645110156319012,11.86743626560952,12.091709946411605,12.317859503857196,12.545809390771357,12.775481433207078,13.006795964511792,13.239672844671873,13.474032351075376,13.70979593511476,13.946886845851349,14.18523062714817,14.424755498296465,14.665392630383664,14.907076331712144,15.149744155735752,15.393336944481392,15.637798819483736,15.88307713106223,16.129122375450507,16.375888087951825,16.62333071901064,16.87140949890427,17.120086295694215,17.369325470143966,17.619093730508617,17.86935998942467,18.12009522456505,18.37127234426233,18.622866058929304,18.874852758807204,19.127210398337205,19.37991838726824,19.632957488475615],\n",
       "\"xvar\":[0.0,0.24050632911392406,0.4810126582278481,0.7215189873417722,0.9620253164556962,1.2025316455696202,1.4430379746835444,1.6835443037974684,1.9240506329113924,2.1645569620253164,2.4050632911392404,2.6455696202531644,2.886075949367089,3.126582278481013,3.367088607594937,3.607594936708861,3.848101265822785,4.088607594936709,4.329113924050633,4.569620253164557,4.810126582278481,5.050632911392405,5.291139240506329,5.531645569620253,5.772151898734178,6.012658227848101,6.253164556962026,6.493670886075949,6.734177215189874,6.974683544303797,7.215189873417722,7.455696202531645,7.69620253164557,7.936708860759494,8.177215189873419,8.417721518987342,8.658227848101266,8.89873417721519,9.139240506329115,9.379746835443038,9.620253164556962,9.860759493670887,10.10126582278481,10.341772151898734,10.582278481012658,10.822784810126583,11.063291139240507,11.30379746835443,11.544303797468356,11.784810126582279,12.025316455696203,12.265822784810126,12.506329113924052,12.746835443037975,12.987341772151899,13.227848101265822,13.468354430379748,13.708860759493671,13.949367088607595,14.18987341772152,14.430379746835444,14.670886075949367,14.91139240506329,15.151898734177216,15.39240506329114,15.632911392405063,15.873417721518988,16.11392405063291,16.354430379746837,16.59493670886076,16.835443037974684,17.075949367088608,17.31645569620253,17.556962025316455,17.79746835443038,18.037974683544306,18.27848101265823,18.518987341772153,18.759493670886076,19.0],\n",
       "\"..ymin..\":[-1.7064395935115368,-1.4534004923041666,-1.200692503373132,-0.9483348638431282,-0.6963481639652289,-0.44475444929826025,-0.1935773296009735,0.05715790553940536,0.30742416445545917,0.5571924248201094,0.8064315992698594,1.055108396059806,1.3031871759534364,1.5506298070122495,1.797395519513566,2.0434407639018444,2.2887190754803397,2.5331809504826825,2.7767737392283207,3.019441563251931,3.26112526458041,3.50176239666761,3.741287267815904,3.979631049112724,4.216721959849313,4.452485543888696,4.686845050292201,4.919721930452282,5.151036461756995,5.380708504192716,5.608658391106877,5.834807948552471,6.059081629354555,6.281407738645062,6.501719717746015,6.719957443845269,6.936068495007378,7.150009324875148,7.361746290021333,7.571256476068072,7.7785282766623975,7.983561691804308,8.186368327846774,8.386971099167651,8.585403649194191,8.781709524283588,8.975941146371285,9.168158638269428,9.358428558655994,9.54682260239905,9.73341631667354,9.918287875426467,10.101516945310408,10.283183666338974,10.463367761124118,10.642147778273385,10.819600468725447,10.995800288617277,11.17081901865763,11.344725487759082,11.517585387619253,11.689461164784293,11.860411977227303,12.03049370341361,12.199758993023764,12.368257349824136,12.53603523851147,12.703136208641306,12.869601029936328,13.03546783433503,13.20077226107393,13.365547601897928,13.529824944170525,13.693633310218795,13.856999791710157,14.01994967823184,14.182506579542594,14.344692541201233,14.506528153458852,14.668032653440134]\n",
       "}\n",
       "}]\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"rybpJ4\");\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 + geomPoint(shape = 1) +\n",
    "    geomSmooth()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"90sC45\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "\"x\":\"xvar\",\n",
       "\"y\":\"yvar\"\n",
       "},\n",
       "\"data\":{\n",
       "\"yvar\":[-4.31414792742272,2.9025852255330413,2.678186038496493,3.8323801422104644,4.552957466631817,3.9043586677544218,10.056090493036294,8.077708681007955,7.384161947842241,12.052485955548157,14.114693281686685,5.329404481450428,10.598634211929852,10.98677740976943,8.96180814139117,14.194166026725489,15.099984576207884,12.734927210327559,18.2859058065098,17.832111113004313],\n",
       "\"xvar\":[0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0,13.0,14.0,15.0,16.0,17.0,18.0,19.0]\n",
       "},\n",
       "\"ggsize\":{\n",
       "\"width\":300.0,\n",
       "\"height\":250.0\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"shape\":1.0,\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"point\",\n",
       "\"data\":{\n",
       "}\n",
       "},{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"smooth\",\n",
       "\"se\":false,\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"smooth\",\n",
       "\"data\":{\n",
       "\"yvar\":[0.776022824006203,0.983294624600528,1.190566425194853,1.3978382257891777,1.6051100263835028,1.8123818269778276,2.0196536275721524,2.2269254281664774,2.4341972287608025,2.641469029355127,2.848740829949452,3.0560126305437767,3.263284431138102,3.470556231732427,3.677828032326752,3.885099832921077,4.092371633515402,4.299643434109727,4.506915234704051,4.714187035298377,4.921458835892701,5.128730636487027,5.33600243708135,5.543274237675676,5.750546038270001,5.957817838864326,6.165089639458651,6.372361440052975,6.579633240647301,6.786905041241625,6.994176841835951,7.201448642430275,7.4087204430246,7.615992243618925,7.823264044213251,8.030535844807575,8.237807645401901,8.445079445996225,8.65235124659055,8.859623047184874,9.0668948477792,9.274166648373525,9.48143844896785,9.688710249562174,9.895982050156498,10.103253850750825,10.310525651345149,10.517797451939474,10.7250692525338,10.932341053128123,11.139612853722449,11.346884654316773,11.5541564549111,11.761428255505423,11.968700056099747,12.175971856694073,12.383243657288398,12.590515457882724,12.797787258477047,13.005059059071373,13.212330859665698,13.419602660260022,13.626874460854348,13.834146261448673,14.041418062042997,14.248689862637322,14.455961663231648,14.663233463825971,14.870505264420299,15.077777065014622,15.285048865608948,15.492320666203272,15.699592466797597,15.90686426739192,16.114136067986244,16.32140786858057,16.5286796691749,16.73595146976922,16.943223270363546,17.150495070957874],\n",
       "\"xvar\":[0.0,0.24050632911392406,0.4810126582278481,0.7215189873417722,0.9620253164556962,1.2025316455696202,1.4430379746835444,1.6835443037974684,1.9240506329113924,2.1645569620253164,2.4050632911392404,2.6455696202531644,2.886075949367089,3.126582278481013,3.367088607594937,3.607594936708861,3.848101265822785,4.088607594936709,4.329113924050633,4.569620253164557,4.810126582278481,5.050632911392405,5.291139240506329,5.531645569620253,5.772151898734178,6.012658227848101,6.253164556962026,6.493670886075949,6.734177215189874,6.974683544303797,7.215189873417722,7.455696202531645,7.69620253164557,7.936708860759494,8.177215189873419,8.417721518987342,8.658227848101266,8.89873417721519,9.139240506329115,9.379746835443038,9.620253164556962,9.860759493670887,10.10126582278481,10.341772151898734,10.582278481012658,10.822784810126583,11.063291139240507,11.30379746835443,11.544303797468356,11.784810126582279,12.025316455696203,12.265822784810126,12.506329113924052,12.746835443037975,12.987341772151899,13.227848101265822,13.468354430379748,13.708860759493671,13.949367088607595,14.18987341772152,14.430379746835444,14.670886075949367,14.91139240506329,15.151898734177216,15.39240506329114,15.632911392405063,15.873417721518988,16.11392405063291,16.354430379746837,16.59493670886076,16.835443037974684,17.075949367088608,17.31645569620253,17.556962025316455,17.79746835443038,18.037974683544306,18.27848101265823,18.518987341772153,18.759493670886076,19.0]\n",
       "}\n",
       "}]\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"90sC45\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();    \n",
       "   </script>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "// Without standard error band.\n",
    "p + geomPoint(shape = 1) +\n",
    "    geomSmooth(se = false)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Split dataset by the `cond` variable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"iiNGvx\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "\"x\":\"xvar\",\n",
       "\"y\":\"yvar\",\n",
       "\"color\":\"cond\"\n",
       "},\n",
       "\"data\":{\n",
       "\"yvar\":[-4.31414792742272,2.9025852255330413,2.678186038496493,3.8323801422104644,4.552957466631817,3.9043586677544218,10.056090493036294,8.077708681007955,7.384161947842241,12.052485955548157,14.114693281686685,5.329404481450428,10.598634211929852,10.98677740976943,8.96180814139117,14.194166026725489,15.099984576207884,12.734927210327559,18.2859058065098,17.832111113004313],\n",
       "\"xvar\":[0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0,13.0,14.0,15.0,16.0,17.0,18.0,19.0],\n",
       "\"cond\":[\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\"]\n",
       "},\n",
       "\"ggsize\":{\n",
       "\"width\":500.0,\n",
       "\"height\":250.0\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"shape\":1.0,\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"point\",\n",
       "\"data\":{\n",
       "}\n",
       "},{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"smooth\",\n",
       "\"se\":false,\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"smooth\",\n",
       "\"data\":{\n",
       "\"yvar\":[-0.9879838982741758,-0.8335367953035937,-0.6790896923330116,-0.5246425893624296,-0.3701954863918474,-0.21574838342126545,-0.06130128045068328,0.09314582251989889,0.24759292549048095,0.4020400284610628,0.5564871314316449,0.7109342344022271,0.8653813373728092,1.019828440343391,1.1742755433139735,1.3287226462845556,1.4831697492551377,1.6376168522257193,1.7920639551963014,1.9465110581668839,2.1009581611374655,2.255405264108048,2.40985236707863,2.5642994700492117,2.718746573019794,2.8731936759903762,3.027640778960958,3.1820878819315404,3.336534984902123,3.4909820878727045,3.645429190843287,3.7998762938138686,3.954323396784451,4.108770499755033,4.263217602725614,4.417664705696197,4.5721118086667785,4.726558911637362,4.8810060146079435,5.035453117578525,5.189900220549107,5.344347323519689,5.498794426490272,5.653241529460853,5.807688632431436,5.9621357354020175,6.116582838372599,6.2710299413431825,6.425477044313764,6.579924147284346,6.734371250254928,6.88881835322551,7.0432654561960915,7.197712559166674,7.3521596621372565,7.506606765107839,7.6610538680784215,7.815500971049002,7.969948074019585,8.124395176990166,8.278842279960749,8.433289382931331,8.587736485901914,8.742183588872496,8.896630691843079,9.051077794813658,9.20552489778424,9.359972000754823,9.514419103725405,9.668866206695988,9.82331330966657,9.977760412637153,10.132207515607732,10.286654618578314,10.4411017215489,10.59554882451948,10.749995927490062,10.904443030460644,11.058890133431227,11.21333723640181,8.657321749520833,8.762550090694996,8.867778431869157,8.973006773043322,9.078235114217485,9.183463455391646,9.288691796565809,9.393920137739972,9.499148478914135,9.604376820088298,9.70960516126246,9.814833502436622,9.920061843610785,10.025290184784948,10.130518525959111,10.235746867133274,10.340975208307436,10.4462035494816,10.551431890655763,10.656660231829925,10.761888573004088,10.86711691417825,10.972345255352414,11.077573596526577,11.18280193770074,11.288030278874901,11.393258620049066,11.498486961223229,11.60371530239739,11.708943643571553,11.814171984745716,11.919400325919879,12.024628667094042,12.129857008268205,12.235085349442366,12.34031369061653,12.445542031790694,12.550770372964855,12.655998714139018,12.761227055313181,12.866455396487344,12.971683737661508,13.07691207883567,13.182140420009832,13.287368761183995,13.39259710235816,13.49782544353232,13.603053784706484,13.708282125880647,13.81351046705481,13.918738808228973,14.023967149403136,14.129195490577297,14.234423831751462,14.339652172925625,14.444880514099786,14.550108855273947,14.655337196448112,14.760565537622275,14.865793878796437,14.971022219970601,15.076250561144763,15.181478902318924,15.28670724349309,15.391935584667252,15.497163925841415,15.60239226701558,15.70762060818974,15.812848949363902,15.918077290538067,16.023305631712226,16.128533972886395,16.233762314060556,16.338990655234717,16.44421899640888,16.54944733758304,16.654675678757208,16.75990401993137,16.86513236110553,16.97036070227969],\n",
       "\"xvar\":[0.0,0.11392405063291139,0.22784810126582278,0.34177215189873417,0.45569620253164556,0.5696202531645569,0.6835443037974683,0.7974683544303798,0.9113924050632911,1.0253164556962024,1.1392405063291138,1.2531645569620253,1.3670886075949367,1.481012658227848,1.5949367088607596,1.7088607594936709,1.8227848101265822,1.9367088607594936,2.050632911392405,2.1645569620253164,2.2784810126582276,2.392405063291139,2.5063291139240507,2.620253164556962,2.7341772151898733,2.848101265822785,2.962025316455696,3.0759493670886076,3.189873417721519,3.3037974683544302,3.4177215189873418,3.531645569620253,3.6455696202531644,3.759493670886076,3.873417721518987,3.9873417721518987,4.10126582278481,4.215189873417722,4.329113924050633,4.443037974683544,4.556962025316455,4.670886075949367,4.784810126582278,4.898734177215189,5.012658227848101,5.1265822784810124,5.240506329113924,5.3544303797468356,5.468354430379747,5.582278481012658,5.69620253164557,5.810126582278481,5.924050632911392,6.037974683544304,6.151898734177215,6.265822784810126,6.379746835443038,6.493670886075949,6.6075949367088604,6.721518987341772,6.8354430379746836,6.949367088607595,7.063291139240506,7.177215189873418,7.291139240506329,7.40506329113924,7.518987341772152,7.632911392405063,7.746835443037974,7.860759493670886,7.974683544303797,8.08860759493671,8.20253164556962,8.316455696202532,8.430379746835444,8.544303797468354,8.658227848101266,8.772151898734178,8.886075949367088,9.0,10.0,10.113924050632912,10.227848101265822,10.341772151898734,10.455696202531646,10.569620253164556,10.683544303797468,10.79746835443038,10.91139240506329,11.025316455696203,11.139240506329113,11.253164556962025,11.367088607594937,11.481012658227847,11.594936708860759,11.708860759493671,11.822784810126581,11.936708860759493,12.050632911392405,12.164556962025316,12.278481012658228,12.39240506329114,12.50632911392405,12.620253164556962,12.734177215189874,12.848101265822784,12.962025316455696,13.075949367088608,13.189873417721518,13.30379746835443,13.417721518987342,13.531645569620252,13.645569620253164,13.759493670886076,13.873417721518987,13.987341772151899,14.10126582278481,14.215189873417721,14.329113924050633,14.443037974683545,14.556962025316455,14.670886075949367,14.784810126582279,14.89873417721519,15.012658227848101,15.126582278481013,15.240506329113924,15.354430379746836,15.468354430379748,15.582278481012658,15.69620253164557,15.810126582278482,15.924050632911392,16.037974683544306,16.151898734177216,16.265822784810126,16.379746835443036,16.49367088607595,16.60759493670886,16.72151898734177,16.835443037974684,16.949367088607595,17.063291139240505,17.17721518987342,17.29113924050633,17.40506329113924,17.518987341772153,17.632911392405063,17.746835443037973,17.860759493670887,17.974683544303797,18.08860759493671,18.20253164556962,18.31645569620253,18.430379746835442,18.544303797468352,18.658227848101266,18.77215189873418,18.88607594936709,19.0],\n",
       "\"cond\":[\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\"]\n",
       "}\n",
       "}]\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"iiNGvx\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();    \n",
       "   </script>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val p1 = letsPlot(data) { x = \"xvar\"; y = \"yvar\"; color = \"cond\" } + ggsize(500, 250)\n",
    "p1 + geomPoint(shape = 1) +\n",
    "     geomSmooth(se = false)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"3cEiGE\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "\"x\":\"xvar\",\n",
       "\"y\":\"yvar\",\n",
       "\"color\":\"cond\"\n",
       "},\n",
       "\"data\":{\n",
       "\"yvar\":[-4.31414792742272,2.9025852255330413,2.678186038496493,3.8323801422104644,4.552957466631817,3.9043586677544218,10.056090493036294,8.077708681007955,7.384161947842241,12.052485955548157,14.114693281686685,5.329404481450428,10.598634211929852,10.98677740976943,8.96180814139117,14.194166026725489,15.099984576207884,12.734927210327559,18.2859058065098,17.832111113004313],\n",
       "\"xvar\":[0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0,13.0,14.0,15.0,16.0,17.0,18.0,19.0],\n",
       "\"cond\":[\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\"]\n",
       "},\n",
       "\"ggsize\":{\n",
       "\"width\":500.0,\n",
       "\"height\":250.0\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "\"shape\":\"cond\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"size\":5.0,\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"point\",\n",
       "\"data\":{\n",
       "}\n",
       "}]\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"3cEiGE\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();    \n",
       "   </script>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "// Map `shape` to the `cond` variable.\n",
    "p1 + geomPoint(size = 5) { shape = \"cond\" }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"F1OQ22\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "\"x\":\"xvar\",\n",
       "\"y\":\"yvar\",\n",
       "\"color\":\"cond\"\n",
       "},\n",
       "\"data\":{\n",
       "\"yvar\":[-4.31414792742272,2.9025852255330413,2.678186038496493,3.8323801422104644,4.552957466631817,3.9043586677544218,10.056090493036294,8.077708681007955,7.384161947842241,12.052485955548157,14.114693281686685,5.329404481450428,10.598634211929852,10.98677740976943,8.96180814139117,14.194166026725489,15.099984576207884,12.734927210327559,18.2859058065098,17.832111113004313],\n",
       "\"xvar\":[0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0,13.0,14.0,15.0,16.0,17.0,18.0,19.0],\n",
       "\"cond\":[\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"A\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\",\"B\"]\n",
       "},\n",
       "\"ggsize\":{\n",
       "\"width\":500.0,\n",
       "\"height\":250.0\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[{\n",
       "\"aesthetic\":\"shape\",\n",
       "\"values\":[1.0,2.0]\n",
       "}],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "\"shape\":\"cond\"\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"size\":5.0,\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"point\",\n",
       "\"data\":{\n",
       "}\n",
       "}]\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"F1OQ22\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();    \n",
       "   </script>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "// Choose different shapes using `scale_shape_manual`:\n",
    "// 1 - hollow circle \n",
    "// 2 - hollow triangle\n",
    "p1 + geomPoint(size = 5) { shape = \"cond\" } + \n",
    "     scaleShapeManual(values = listOf(1,2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Handling overplotting"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "// Create data with overlapping points.\n",
    "val data1 = mapOf(\n",
    "        \"xvar\" to (data[\"xvar\"] as List<Double>).map { (it / 5).toInt() * 5 },\n",
    "        \"yvar\" to (data[\"yvar\"] as List<Double>).map { (it / 5).toInt() * 5 },\n",
    "    )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"jxB6D1\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "\"x\":\"xvar\",\n",
       "\"y\":\"yvar\"\n",
       "},\n",
       "\"data\":{\n",
       "\"yvar\":[0.0,0.0,0.0,0.0,0.0,0.0,10.0,5.0,5.0,10.0,10.0,5.0,10.0,10.0,5.0,10.0,15.0,10.0,15.0,15.0],\n",
       "\"xvar\":[0.0,0.0,0.0,0.0,0.0,5.0,5.0,5.0,5.0,5.0,10.0,10.0,10.0,10.0,10.0,15.0,15.0,15.0,15.0,15.0]\n",
       "},\n",
       "\"ggsize\":{\n",
       "\"width\":500.0,\n",
       "\"height\":250.0\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[{\n",
       "\"aesthetic\":\"x\",\n",
       "\"breaks\":[0.0,5.0,10.0,15.0]\n",
       "}],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"size\":7.0,\n",
       "\"alpha\":0.3,\n",
       "\"position\":\"identity\",\n",
       "\"geom\":\"point\",\n",
       "\"data\":{\n",
       "}\n",
       "}]\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"jxB6D1\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();    \n",
       "   </script>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val p2 = letsPlot(data1) { x = \"xvar\"; y = \"yvar\"} + ggsize(500, 250) +\n",
    "         scaleXContinuous(breaks = listOf(0, 5, 10, 15))\n",
    "// Use `alpha` to show overplotting.\n",
    "p2 + geomPoint(alpha = .3, size = 7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "   <div id=\"cccgym\"></div>\n",
       "   <script type=\"text/javascript\" data-lets-plot-script=\"plot\">\n",
       "       (function() {\n",
       "           var plotSpec={\n",
       "\"mapping\":{\n",
       "\"x\":\"xvar\",\n",
       "\"y\":\"yvar\"\n",
       "},\n",
       "\"data\":{\n",
       "\"yvar\":[0.0,0.0,0.0,0.0,0.0,0.0,10.0,5.0,5.0,10.0,10.0,5.0,10.0,10.0,5.0,10.0,15.0,10.0,15.0,15.0],\n",
       "\"xvar\":[0.0,0.0,0.0,0.0,0.0,5.0,5.0,5.0,5.0,5.0,10.0,10.0,10.0,10.0,10.0,15.0,15.0,15.0,15.0,15.0]\n",
       "},\n",
       "\"ggsize\":{\n",
       "\"width\":500.0,\n",
       "\"height\":250.0\n",
       "},\n",
       "\"kind\":\"plot\",\n",
       "\"scales\":[{\n",
       "\"aesthetic\":\"x\",\n",
       "\"breaks\":[0.0,5.0,10.0,15.0]\n",
       "}],\n",
       "\"layers\":[{\n",
       "\"mapping\":{\n",
       "},\n",
       "\"stat\":\"identity\",\n",
       "\"shape\":1.0,\n",
       "\"position\":{\n",
       "\"name\":\"jitter\",\n",
       "\"width\":0.1,\n",
       "\"kind\":\"pos\",\n",
       "\"height\":0.1\n",
       "},\n",
       "\"geom\":\"point\",\n",
       "\"data\":{\n",
       "}\n",
       "}]\n",
       "};\n",
       "           var plotContainer = document.getElementById(\"cccgym\");\n",
       "           window.letsPlotCall(function() {{\n",
       "               LetsPlot.buildPlotFromProcessedSpecs(plotSpec, -1, -1, plotContainer);\n",
       "           }});\n",
       "       })();    \n",
       "   </script>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "// `jitter` points to show overplotting in another way.\n",
    "p2 + geomPoint(shape = 1, position = positionJitter(width=.1, height=.1))"
   ]
  }
 ],
 "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.7.0-dev-1825"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}