{ "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" }, { "data": { "text/html": [ " " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%useLatestDescriptors\n", "%use lets-plot\n", "%use dataframe" ] }, { "cell_type": "code", "execution_count": 2, "id": "adopted-england", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Lets-Plot Kotlin API v.4.6.0. Frontend: Notebook with dynamically loaded JS. Lets-Plot JS v.4.2.0." ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "LetsPlot.getInfo()" ] }, { "cell_type": "code", "execution_count": 3, "id": "painful-nitrogen", "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", " \n", " \n", " \n", " \n", "
\n", "\n", "

DataFrame: rowsCount = 5, columnsCount = 12

\n", " \n", " \n", " " ] }, "execution_count": 3, "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()" ] }, { "cell_type": "code", "execution_count": 4, "id": "variable-delta", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 4, "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": 5, "id": "compliant-bridges", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "org.jetbrains.letsPlot.intern.figure.SubPlotsFigure@2715644a" ] }, "execution_count": 5, "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)" ] }, { "cell_type": "markdown", "id": "surgical-hormone", "metadata": {}, "source": [ "### Parameter `drawQuantiles`" ] }, { "cell_type": "code", "execution_count": 6, "id": "residential-rings", "metadata": {}, "outputs": [], "source": [ "val quantiles = listOf(.25, .5, .75)" ] }, { "cell_type": "code", "execution_count": 7, "id": "excellent-namibia", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "letsPlot(mpg.toMap()) { x = \"drv\"; y = \"hwy\" } +\n", " geomViolin(quantiles = quantiles, quantileLines = true) + \n", " ggtitle(\"quantiles=$quantiles\")" ] }, { "cell_type": "markdown", "id": "committed-davis", "metadata": {}, "source": [ "### Parameter `scale`" ] }, { "cell_type": "code", "execution_count": 8, "id": "personal-disaster", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 8, "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], quantiles = quantiles, quantileLines = true) +\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": 9, "id": "forty-synthetic", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val plotBase = letsPlot(mpg.toMap()) { x = \"drv\"; y = \"hwy\" }\n", "val plots1 = listOf(\n", " plotBase + geomViolin(quantiles=quantiles, quantileLines=true) + ggtitle(\"Default\"),\n", " plotBase + geomViolin(quantiles=quantiles, quantileLines=true, kernel=\"epanechikov\") + \n", " ggtitle(\"kernel='epanechikov'\"),\n", " plotBase + geomViolin(quantiles=quantiles, quantileLines=true, bw=.1) + ggtitle(\"bw=0.1\"),\n", " plotBase + geomViolin(quantiles=quantiles, quantileLines=true, 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": 10, "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": 11, "id": "recorded-sally", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plotBase2 + \n", " geomViolin(quantiles = quantiles, quantileLines = true, 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", " }" ] }, { "cell_type": "markdown", "id": "preceding-radar", "metadata": {}, "source": [ "### Facets" ] }, { "cell_type": "code", "execution_count": 12, "id": "least-commodity", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plotBase2 + \n", " geomViolin(quantiles = quantiles, quantileLines = true, alpha = 0.5, size = 2) { \n", " color = asDiscrete(\"year\")\n", " fill = asDiscrete(\"year\")\n", " } + \n", " facetGrid(y = \"year\")" ] }, { "cell_type": "markdown", "id": "parental-classroom", "metadata": {}, "source": [ "### `coordFlip()`" ] }, { "cell_type": "code", "execution_count": 13, "id": "regular-person", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plotBase + geomViolin(quantiles = quantiles, quantileLines = true) + coordFlip()" ] }, { "cell_type": "markdown", "id": "liable-telling", "metadata": {}, "source": [ "### `geomViolin()` and `geomBoxplot()`" ] }, { "cell_type": "code", "execution_count": 14, "id": "floppy-strength", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plotBase2 + \n", " geomViolin(quantiles = quantiles, quantileLines = true, alpha = 0.5, size = 1, trim = false) + \n", " geomBoxplot(width=.2, position = positionDodge(.2), \n", " outlierShape = 21, outlierFill = \"white\", outlierSize = 2) {\n", " color = asDiscrete(\"year\")\n", " fill = asDiscrete(\"year\")\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.8.0-dev-3517" } }, "nbformat": 4, "nbformat_minor": 5 }