{ "cells": [ { "cell_type": "markdown", "id": "normal-allowance", "metadata": {}, "source": [ "### Violin Geometry" ] }, { "cell_type": "code", "execution_count": 1, "id": "optical-digit", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%useLatestDescriptors\n", "%use lets-plot\n", "// %use krangl" ] }, { "cell_type": "code", "execution_count": 2, "id": "adopted-england", "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": "athletic-working", "metadata": {}, "outputs": [], "source": [ "%use krangl" ] }, { "cell_type": "code", "execution_count": 4, "id": "painful-nitrogen", "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": [ "var mpg = DataFrame.readCSV(\"https://raw.githubusercontent.com/JetBrains/lets-plot-kotlin/master/docs/examples/data/mpg.csv\")\n", "mpg.head()\n" ] }, { "cell_type": "code", "execution_count": 5, "id": "variable-delta", "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "letsPlot(mpg.toMap()) + geomViolin {y = \"hwy\"}" ] }, { "cell_type": "markdown", "id": "prepared-jacksonville", "metadata": {}, "source": [ "### `geomViolin()` and `geomDesity()`" ] }, { "cell_type": "code", "execution_count": 6, "id": "compliant-bridges", "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val densityPlot = letsPlot(mpg.toMap()) + \n", " geomDensity(color=\"black\", alpha=.5) {x=\"hwy\"; fill=\"drv\"} + \n", " facetGrid(x=\"drv\") + \n", " coordFlip() + scaleYContinuous(breaks = emptyList())\n", " ggtitle(\"geomDensity()\")\n", "\n", "val violinPlot = letsPlot(mpg.toMap()) +\n", " geomViolin(alpha=0.5) {\n", " x = asDiscrete(\"drv\", order=1)\n", " y = \"hwy\"\n", " fill = \"drv\"\n", " } +\n", " ggtitle(\"geomViolin()\")\n", " \n", "gggrid(listOf(densityPlot, violinPlot), 2, 400, 300) " ] }, { "cell_type": "markdown", "id": "surgical-hormone", "metadata": {}, "source": [ "### Parameter `drawQuantiles`" ] }, { "cell_type": "code", "execution_count": 7, "id": "residential-rings", "metadata": {}, "outputs": [], "source": [ "val DRAW_QUANTILES = listOf(.25, .5, .75)" ] }, { "cell_type": "code", "execution_count": 8, "id": "excellent-namibia", "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "letsPlot(mpg.toMap()) { x = \"drv\"; y = \"hwy\" } +\n", " geomViolin(drawQuantiles=DRAW_QUANTILES) + \n", " ggtitle(\"drawQuantiles=$DRAW_QUANTILES\")" ] }, { "cell_type": "markdown", "id": "committed-davis", "metadata": {}, "source": [ "### Parameter `scale`" ] }, { "cell_type": "code", "execution_count": 9, "id": "personal-disaster", "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val options = listOf(null, \"area\", \"count\", \"width\")\n", "\n", "val plots = List(options.size) { i -> \n", " letsPlot(mpg.toMap()) { x = \"drv\"; y = \"hwy\" } +\n", " geomViolin(scale = options[i], drawQuantiles=DRAW_QUANTILES) +\n", " ggtitle(\"scale = ${options[i]} \")\n", "}\n", "\n", "gggrid(plots, 2, 400, 250)" ] }, { "cell_type": "markdown", "id": "nuclear-flight", "metadata": {}, "source": [ "### Parameters used by `ydensity` statistic" ] }, { "cell_type": "code", "execution_count": 10, "id": "forty-synthetic", "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val plotBase = letsPlot(mpg.toMap()) { x = \"drv\"; y = \"hwy\" }\n", "val plots1 = listOf(\n", " plotBase + geomViolin(drawQuantiles=DRAW_QUANTILES) + ggtitle(\"Default\"),\n", " plotBase + geomViolin(drawQuantiles=DRAW_QUANTILES, kernel=\"epanechikov\") + ggtitle(\"kernel='epanechikov'\"),\n", " plotBase + geomViolin(drawQuantiles=DRAW_QUANTILES, bw=.1) + ggtitle(\"bw=0.1\"),\n", " plotBase + geomViolin(drawQuantiles=DRAW_QUANTILES, adjust=2) + ggtitle(\"adjust=2\"), \n", ")\n", "\n", "gggrid(plots1, 2, 400, 250)" ] }, { "cell_type": "markdown", "id": "israeli-impossible", "metadata": {}, "source": [ "### Grouping and tooltips" ] }, { "cell_type": "code", "execution_count": 11, "id": "intended-criticism", "metadata": {}, "outputs": [], "source": [ "val plotBase2 = plotBase + \n", " scaleColorBrewer(type=\"qual\", palette=\"Set1\") + \n", " scaleFillBrewer(type=\"qual\", palette=\"Set1\") +\n", " themeGrey()" ] }, { "cell_type": "code", "execution_count": 12, "id": "recorded-sally", "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plotBase2 + \n", " geomViolin(drawQuantiles = DRAW_QUANTILES, alpha = 0.5, size = 2,\n", " tooltips = layerTooltips().title(\"^x\")\n", " .line(\"year|@year\")\n", " .line(\"hwy|@hwy\")\n", " .line(\"violinwidth|@..violinwidth..\")\n", " .line(\"density|@..density..\").format(\"@..density..\", \".3f\")\n", " .line(\"count|@..count..\").format(\"@..count..\", \".3f\")\n", " .line(\"scaled|@..scaled..\").format(\"@..scaled..\", \".3f\")\n", " ) { \n", " color = asDiscrete(\"year\")\n", " fill = asDiscrete(\"year\")\n", " }\n" ] }, { "cell_type": "markdown", "id": "preceding-radar", "metadata": {}, "source": [ "### Facets" ] }, { "cell_type": "code", "execution_count": 13, "id": "least-commodity", "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plotBase2 + \n", " geomViolin(drawQuantiles = DRAW_QUANTILES, alpha = 0.5, size = 2) { \n", " color = asDiscrete(\"year\")\n", " fill = asDiscrete(\"year\")\n", " } + \n", " facetGrid(y = \"year\")\n" ] }, { "cell_type": "markdown", "id": "parental-classroom", "metadata": {}, "source": [ "### `coord_flip()`" ] }, { "cell_type": "code", "execution_count": 14, "id": "regular-person", "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plotBase + geomViolin(drawQuantiles = DRAW_QUANTILES) + coordFlip()" ] }, { "cell_type": "markdown", "id": "liable-telling", "metadata": {}, "source": [ "### `geomViolin()` and `geomBoxplot()`" ] }, { "cell_type": "code", "execution_count": 15, "id": "floppy-strength", "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " " ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plotBase2 + \n", " geomViolin(drawQuantiles = DRAW_QUANTILES, alpha = 0.5, size = 1, trim = false) + \n", " geomBoxplot(width=.2, position = positionDodge(.2), outlierShape = 21, outlierFill = \"white\", outlierSize = 5) {\n", " color = asDiscrete(\"year\")\n", " fill = asDiscrete(\"year\")\n", " }\n" ] } ], "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.20-dev-1299" } }, "nbformat": 4, "nbformat_minor": 5 }