{ "cells": [ { "cell_type": "markdown", "id": "872680a6", "metadata": {}, "source": [ "# Lines in Lets-Plot" ] }, { "cell_type": "code", "execution_count": 1, "id": "9c24c1d2", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%useLatestDescriptors\n", "// %use krangl\n", "%use kotlin-statistics\n", "%use lets-plot" ] }, { "cell_type": "code", "execution_count": 2, "id": "encouraging-limitation", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Lets-Plot Kotlin API v.4.1.1. Frontend: Notebook with dynamically loaded JS. Lets-Plot JS v.2.5.1." ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "LetsPlot.getInfo() // This prevents Krangl from loading an obsolete version of Lets-Plot classes." ] }, { "cell_type": "code", "execution_count": 3, "id": "random-foster", "metadata": {}, "outputs": [], "source": [ "%use krangl" ] }, { "cell_type": "markdown", "id": "91a3bca2", "metadata": {}, "source": [ "## Vertical, horizontal and oblique lines" ] }, { "cell_type": "code", "execution_count": 4, "id": "b62278c4", "metadata": {}, "outputs": [ { "data": { "text/html": [ "| manufacturer | model | displ | year | cyl | trans | drv | cty | hwy | fl | class | |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | audi | a4 | 1.8 | 1999 | 4 | auto(l5) | f | 18 | 29 | p | compact |
| 2 | audi | a4 | 1.8 | 1999 | 4 | manual(m5) | f | 21 | 29 | p | compact |
| 3 | audi | a4 | 2.0 | 2008 | 4 | manual(m6) | f | 20 | 31 | p | compact |
| 4 | audi | a4 | 2.0 | 2008 | 4 | auto(av) | f | 21 | 30 | p | compact |
| 5 | audi | a4 | 2.8 | 1999 | 6 | auto(l5) | f | 16 | 26 | p | compact |
Shape: 5 x 12. \n", "
" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val mpgDf = DataFrame.readCSV(\"https://raw.githubusercontent.com/JetBrains/lets-plot-kotlin/master/docs/examples/data/mpg.csv\")\n", "mpgDf.head()\n" ] }, { "cell_type": "code", "execution_count": 5, "id": "f395c360", "metadata": {}, "outputs": [], "source": [ "val mpgMap = mpgDf.toMap()" ] }, { "cell_type": "code", "execution_count": 6, "id": "781fdc93", "metadata": {}, "outputs": [], "source": [ "val regModel = (mpgMap[\"cty\"]!! zip mpgMap[\"hwy\"]!!).simpleRegression(\n", " xSelector = { it.first as Number },\n", " ySelector = { it.second as Number }\n", ")\n", "val ctyMedian = mpgDf.get(\"cty\").median()\n", "val hwyMedian = mpgDf.get(\"hwy\").median()" ] }, { "cell_type": "code", "execution_count": 7, "id": "ac17c9ed", "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "letsPlot(mpgMap) { x = \"cty\"; y = \"hwy\" } +\n", " geomPoint() +\n", " geomVLine(xintercept = ctyMedian, color = \"#756bb1\", linetype = \"dashed\") +\n", " geomHLine(yintercept = hwyMedian, color = \"#756bb1\", linetype = \"dashed\") +\n", " geomABLine(slope = regModel.slope, intercept = regModel.intercept, color = \"#de2d26\") +\n", " themeMinimal()" ] }, { "cell_type": "markdown", "id": "4211165f", "metadata": {}, "source": [ "## Broken lines" ] }, { "cell_type": "code", "execution_count": 8, "id": "d1c840c9", "metadata": {}, "outputs": [], "source": [ "fun generateParabolicDataMap(n: Int = 25, a: Double = 1.0): Map