{ "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" }, { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%useLatestDescriptors\n", "%use lets-plot\n", "%use dataframe" ] }, { "cell_type": "code", "execution_count": 2, "id": "trying-liberty", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Lets-Plot Kotlin API v.4.4.2. Frontend: Notebook with dynamically loaded JS. Lets-Plot JS v.4.0.0." ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "LetsPlot.getInfo()" ] }, { "cell_type": "code", "execution_count": 3, "id": "serial-matthew", "metadata": {}, "outputs": [ { "data": { "application/kotlindataframe+json": "{\"nrow\":5,\"ncol\":12,\"columns\":[\"untitled\",\"manufacturer\",\"model\",\"displ\",\"year\",\"cyl\",\"trans\",\"drv\",\"cty\",\"hwy\",\"fl\",\"class\"],\"kotlin_dataframe\":[{\"untitled\":1,\"manufacturer\":\"audi\",\"model\":\"a4\",\"displ\":1.8,\"year\":1999,\"cyl\":4,\"trans\":\"auto(l5)\",\"drv\":\"f\",\"cty\":18,\"hwy\":29,\"fl\":\"p\",\"class\":\"compact\"},{\"untitled\":2,\"manufacturer\":\"audi\",\"model\":\"a4\",\"displ\":1.8,\"year\":1999,\"cyl\":4,\"trans\":\"manual(m5)\",\"drv\":\"f\",\"cty\":21,\"hwy\":29,\"fl\":\"p\",\"class\":\"compact\"},{\"untitled\":3,\"manufacturer\":\"audi\",\"model\":\"a4\",\"displ\":2.0,\"year\":2008,\"cyl\":4,\"trans\":\"manual(m6)\",\"drv\":\"f\",\"cty\":20,\"hwy\":31,\"fl\":\"p\",\"class\":\"compact\"},{\"untitled\":4,\"manufacturer\":\"audi\",\"model\":\"a4\",\"displ\":2.0,\"year\":2008,\"cyl\":4,\"trans\":\"auto(av)\",\"drv\":\"f\",\"cty\":21,\"hwy\":30,\"fl\":\"p\",\"class\":\"compact\"},{\"untitled\":5,\"manufacturer\":\"audi\",\"model\":\"a4\",\"displ\":2.8,\"year\":1999,\"cyl\":6,\"trans\":\"auto(l5)\",\"drv\":\"f\",\"cty\":16,\"hwy\":26,\"fl\":\"p\",\"class\":\"compact\"}]}", "text/html": [ " \n", " \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()\n" ] }, { "cell_type": "code", "execution_count": 4, "id": "latter-manual", "metadata": {}, "outputs": [], "source": [ "val mpg32 = mpg.shuffle().take(32)" ] }, { "cell_type": "code", "execution_count": 5, "id": "juvenile-continuity", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 5, "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": 6, "id": "informed-protection", "metadata": {}, "outputs": [], "source": [ "val PACIFIC_BLUE = \"#118ed8\"" ] }, { "cell_type": "code", "execution_count": 7, "id": "signed-child", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 7, "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": 8, "id": "international-passing", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 8, "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": 9, "id": "psychological-kernel", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 9, "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": 10, "id": "peaceful-sentence", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 10, "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": 11, "id": "geographic-delivery", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 11, "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": 12, "id": "resident-carpet", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 12, "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": 13, "id": "compliant-transcription", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 13, "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": 14, "id": "scenic-treasure", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 14, "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": 15, "id": "streaming-promotion", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 15, "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": 16, "id": "quick-discretion", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 16, "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": 17, "id": "standing-gender", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 17, "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": 18, "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": 19, "id": "magnetic-keeping", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 19, "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": [ "### `coordFlip()`" ] }, { "cell_type": "code", "execution_count": 20, "id": "straight-acrylic", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "letsPlot(mpg32.toMap()) {x = \"hwy\"} +\n", " geomDotplot() + coordFlip()" ] } ], "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.20" } }, "nbformat": 4, "nbformat_minor": 5 }