{ "cells": [ { "cell_type": "markdown", "id": "reserved-institution", "metadata": {}, "source": [ "### Dotplot Geometry" ] }, { "cell_type": "code", "execution_count": 1, "id": "engaging-filter", "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": "trying-liberty", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Lets-Plot Kotlin API v.4.0.0. Frontend: Notebook with dynamically loaded JS. Lets-Plot JS v.2.4.0." ] }, "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": "flexible-referral", "metadata": {}, "outputs": [], "source": [ "%use krangl" ] }, { "cell_type": "code", "execution_count": 4, "id": "serial-matthew", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
manufacturermodeldisplyearcyltransdrvctyhwyflclass
1audia41.819994auto(l5)f1829pcompact
2audia41.819994manual(m5)f2129pcompact
3audia42.020084manual(m6)f2031pcompact
4audia42.020084auto(av)f2130pcompact
5audia42.819996auto(l5)f1626pcompact

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": "latter-manual", "metadata": {}, "outputs": [], "source": [ "_rand = java.util.Random(32)\n", "val mpg32 = mpg.sampleN(32)" ] }, { "cell_type": "code", "execution_count": 6, "id": "juvenile-continuity", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "letsPlot(mpg32.toMap()) + geomDotplot {x = \"hwy\"}" ] }, { "cell_type": "markdown", "id": "solved-skirt", "metadata": {}, "source": [ "### `geomDotplot()` and `geomDensity()`" ] }, { "cell_type": "code", "execution_count": 7, "id": "informed-protection", "metadata": {}, "outputs": [], "source": [ "val PACIFIC_BLUE = \"#118ed8\"" ] }, { "cell_type": "code", "execution_count": 8, "id": "signed-child", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "letsPlot(mpg32.toMap()) {x = \"hwy\"} +\n", " geomDensity(fill = PACIFIC_BLUE) + \n", " geomDotplot(binWidth = 1.5, fill = \"white\", color = PACIFIC_BLUE)\n", " " ] }, { "cell_type": "markdown", "id": "broad-single", "metadata": {}, "source": [ "### `geomDotplot()` and `geomHistogram()`" ] }, { "cell_type": "code", "execution_count": 9, "id": "international-passing", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "letsPlot(mpg32.toMap()) {x = \"hwy\"} +\n", " geomHistogram(binWidth = 1.5, color = \"white\") + \n", " geomDotplot(method = \"histodot\", binWidth = 1.5, fill = \"white\", color = PACIFIC_BLUE)\n" ] }, { "cell_type": "markdown", "id": "engaging-cycling", "metadata": {}, "source": [ "### Parameter `stackDir`" ] }, { "cell_type": "code", "execution_count": 10, "id": "psychological-kernel", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val plotBase = letsPlot(mpg32.toMap()) {x = \"hwy\"}\n", "val plots = listOf(\n", " plotBase + geomDotplot(binWidth = 1.5, stackDir = \"up\") + ggtitle(\"stackDir = \\\"up\\\"\"),\n", " plotBase + geomDotplot(binWidth = 1.5, stackDir = \"down\") + ggtitle(\"stackDir = \\\"down\\\"\"),\n", " plotBase + geomDotplot(binWidth = 1.5, stackDir = \"center\") + ggtitle(\"stackDir = \\\"center\\\"\"),\n", " plotBase + geomDotplot(binWidth = 1.5, stackDir = \"centerwhole\") + ggtitle(\"stackDir = \\\"centerwhole\\\"\"),\n", ")\n", "\n", "gggrid(plots, 2, 400, 310, fit = true)" ] }, { "cell_type": "markdown", "id": "documented-guatemala", "metadata": {}, "source": [ "### Parameter `stackRatio`" ] }, { "cell_type": "code", "execution_count": 11, "id": "peaceful-sentence", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val plots1 = listOf(\n", " plotBase + geomDotplot(binWidth = 1.5, stackRatio = 1) + ggtitle(\"stackRatio = 1\"),\n", " plotBase + geomDotplot(binWidth = 1.5, stackRatio = 0.5) + ggtitle(\"stackRatio = 0.5\"),\n", " plotBase + geomDotplot(binWidth = 1.5, stackRatio = 1.5) + ggtitle(\"stackRatio = 1.5\"),\n", ")\n", "\n", "gggrid(plots1, 2, 400, 310, fit = true)" ] }, { "cell_type": "markdown", "id": "refined-queensland", "metadata": {}, "source": [ "### Parameter `dotSize`" ] }, { "cell_type": "code", "execution_count": 12, "id": "geographic-delivery", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val plots2 = listOf(\n", " plotBase + geomDotplot(binWidth = 1.5, dotSize = 1) + ggtitle(\"dotSize = 1\"),\n", " plotBase + geomDotplot(binWidth = 1.5, dotSize = 0.5) + ggtitle(\"dotSize = 0.5\"),\n", " plotBase + geomDotplot(binWidth = 1.5, dotSize = 1.5) + ggtitle(\"dotSize = 1.5\"),\n", ")\n", "\n", "gggrid(plots2, 2, 400, 310, fit = true)" ] }, { "cell_type": "markdown", "id": "laughing-occupation", "metadata": {}, "source": [ "### Parameter `center`" ] }, { "cell_type": "code", "execution_count": 13, "id": "resident-carpet", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val plots3 = listOf(\n", " plotBase + geomDotplot(binWidth = 1.5, method = \"histodot\") + ggtitle(\"Default\"),\n", " plotBase + geomDotplot(binWidth = 1.5, method = \"histodot\", center = 11) + ggtitle(\"center = 11\"),\n", ")\n", "\n", "gggrid(plots3, 2, 400, 310, fit = true)" ] }, { "cell_type": "markdown", "id": "agricultural-roots", "metadata": {}, "source": [ "### Parameter `boundary`" ] }, { "cell_type": "code", "execution_count": 14, "id": "compliant-transcription", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val plots4 = listOf(\n", " plotBase + geomDotplot(binWidth = 1.5, method = \"histodot\") + ggtitle(\"Default\"),\n", " plotBase + geomDotplot(binWidth = 1.5, method = \"histodot\", boundary = 12) + ggtitle(\"boundary = 12\"),\n", ")\n", "\n", "gggrid(plots4, 2, 400, 310, fit = true)" ] }, { "cell_type": "markdown", "id": "downtown-rebate", "metadata": {}, "source": [ "### Parameter `bins`" ] }, { "cell_type": "code", "execution_count": 15, "id": "scenic-treasure", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val plots5 = listOf(\n", " plotBase + geomDotplot(method = \"histodot\") + ggtitle(\"Default (30)\"),\n", " plotBase + geomDotplot(method = \"histodot\", bins = 15) + ggtitle(\"bins = 15\"),\n", ")\n", "\n", "gggrid(plots5, 2, 400, 310, fit = true)" ] }, { "cell_type": "markdown", "id": "banner-assault", "metadata": {}, "source": [ "### Grouping" ] }, { "cell_type": "code", "execution_count": 16, "id": "streaming-promotion", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plotBase +\n", " geomDotplot(binWidth = 2, color=\"black\") {fill = asDiscrete(\"cyl\")} +\n", " ggtitle(\"method='dotdensity'\")" ] }, { "cell_type": "code", "execution_count": 17, "id": "quick-discretion", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val plots6 = listOf(\n", " plotBase + geomDotplot(method = \"histodot\", binWidth = 2, color = \"black\") {\n", " fill = asDiscrete(\"cyl\")\n", " } + ggtitle(\"method='histodot'\"),\n", " plotBase + geomDotplot(method = \"histodot\", stackGroups = true, binWidth = 2, color = \"black\") {\n", " fill = asDiscrete(\"cyl\")\n", " } + ggtitle(\"method='histodot', stackGroups = true\"),\n", ")\n", "\n", "gggrid(plots6, 2, 400, 310, fit = true)" ] }, { "cell_type": "markdown", "id": "rural-manchester", "metadata": {}, "source": [ "### Tooltips" ] }, { "cell_type": "code", "execution_count": 18, "id": "standing-gender", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "letsPlot(mpg32.toMap()) {x = \"hwy\"} + \n", " geomDotplot(tooltips = layerTooltips()\n", " .line(\"stack center|^x\")\n", " .line(\"Number of dots in stack|@..count..\")\n", " .line(\"Dot diameter|@..binwidth..\"),\n", " bins = 15)" ] }, { "cell_type": "markdown", "id": "least-sodium", "metadata": {}, "source": [ "### Facets" ] }, { "cell_type": "code", "execution_count": 19, "id": "healthy-master", "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": 20, "id": "magnetic-keeping", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plotBase2 + geomDotplot(binWidth = 2) {\n", " color = asDiscrete(\"cyl\")\n", " fill = asDiscrete(\"cyl\")\n", " } + facetGrid(x = \"cyl\") + ggtitle(\"facetGrid\")\n" ] }, { "cell_type": "markdown", "id": "defensive-sight", "metadata": {}, "source": [ "### `coord_flip()`" ] }, { "cell_type": "code", "execution_count": 21, "id": "straight-acrylic", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "letsPlot(mpg32.toMap()) {x = \"hwy\"} +\n", " geomDotplot() + coordFlip()" ] }, { "cell_type": "markdown", "id": "happy-isolation", "metadata": {}, "source": [ "### \"identity\" statistic" ] }, { "cell_type": "code", "execution_count": 22, "id": "french-mount", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val data = mapOf(\n", " \"x\" to listOf(0.5, 1.5, 2.5, null),\n", " \"count\" to listOf(0, 3, null, 0),\n", " \"binwidth\" to listOf(.5, .5, .5, .5),\n", ")\n", "\n", "letsPlot(data) + geomDotplot(stat = Stat.identity) {\n", " x = \"x\"\n", " stackSize = \"count\"\n", " binWidth = \"binwidth\"\n", "} + ggtitle(\"stat = Stat.identity\")" ] }, { "cell_type": "markdown", "id": "purple-buffer", "metadata": {}, "source": [ "### Dotplot" ] }, { "cell_type": "code", "execution_count": 23, "id": "collect-prague", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "letsPlot(mpg32.toMap()) {x = \"hwy\"} +\n", " geomDotplot(\n", " method = \"histodot\",\n", " bins = 9, \n", " stackDir = \"center\",\n", " stackRatio = 0.8, \n", " dotSize = 0.8,\n", " color = \"black\", \n", " alpha = .5, size = 1\n", " ) {\n", " fill = asDiscrete(\"cyl\")\n", " } + \n", " scaleFillBrewer(palette = \"Set1\") +\n", " themeGrey()" ] } ], "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 }