{ "cells": [ { "cell_type": "markdown", "id": "unique-glasgow", "metadata": {}, "source": [ "### Y-Dotplot Geometry" ] }, { "cell_type": "code", "execution_count": 1, "id": "liberal-determination", "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": "radical-omega", "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": "conservative-obligation", "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": "executed-thirty", "metadata": {}, "outputs": [], "source": [ "val mpg100 = mpg.shuffle().take(100)" ] }, { "cell_type": "code", "execution_count": 5, "id": "experimental-license", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "letsPlot(mpg100.toMap()) + geomYDotplot {y = \"hwy\"}" ] }, { "cell_type": "markdown", "id": "dominant-voluntary", "metadata": {}, "source": [ "### `geomDotplot()` and `geomViolin()`" ] }, { "cell_type": "code", "execution_count": 6, "id": "appointed-warren", "metadata": {}, "outputs": [], "source": [ "val PACIFIC_BLUE = \"#118ed8\"" ] }, { "cell_type": "code", "execution_count": 7, "id": "ordinary-compromise", "metadata": {}, "outputs": [], "source": [ "val plotBase = letsPlot(mpg100.toMap()) {x = \"drv\"; y = \"hwy\"}" ] }, { "cell_type": "code", "execution_count": 8, "id": "altered-bangkok", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val plots = listOf(\n", " plotBase +\n", " geomViolin(fill = PACIFIC_BLUE, size = 0) + \n", " geomYDotplot(binWidth = 1.6, stackRatio=.5, fill = \"white\", color = PACIFIC_BLUE) + \n", " ggtitle(\"violin + ydotplot (method='dotdensity')\"),\n", " plotBase +\n", " geomViolin(fill = PACIFIC_BLUE, size = 0) + \n", " geomYDotplot(binWidth = 1.6, stackRatio=.5, method = \"histodot\", fill = \"white\", color = PACIFIC_BLUE) + \n", " ggtitle(\"violin + ydotplot (method='histodot')\"),\n", ")\n", "\n", "gggrid(plots, 2, 450, 300)" ] }, { "cell_type": "markdown", "id": "spectacular-breach", "metadata": {}, "source": [ "### Parameter `stackDir`" ] }, { "cell_type": "code", "execution_count": 9, "id": "forbidden-correction", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val plots1 = listOf(\n", " plotBase + geomYDotplot(binWidth = 1.5, stackDir = \"left\") + ggtitle(\"stackDir = \\\"left\\\"\"),\n", " plotBase + geomYDotplot(binWidth = 1.5, stackDir = \"right\") + ggtitle(\"stackDir = \\\"right\\\"\"),\n", " plotBase + geomYDotplot(binWidth = 1.5, stackDir = \"center\") + ggtitle(\"stackDir = \\\"center\\\"\"),\n", " plotBase + geomYDotplot(binWidth = 1.5, stackDir = \"centerwhole\") + ggtitle(\"stackDir = \\\"centerwhole\\\"\"),\n", ")\n", "\n", "gggrid(plots1, 2, 450, 300)" ] }, { "cell_type": "markdown", "id": "subject-restriction", "metadata": {}, "source": [ "### Parameter `stackRatio`" ] }, { "cell_type": "code", "execution_count": 10, "id": "developed-nevada", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val plots2 = listOf(\n", " plotBase + geomYDotplot(binWidth = 1.5, stackRatio = 1) + ggtitle(\"stackRatio = 1\"),\n", " plotBase + geomYDotplot(binWidth = 1.5, stackRatio = 0.5) + ggtitle(\"stackRatio = 0.5\"),\n", " plotBase + geomYDotplot(binWidth = 1.5, stackRatio = 1.5) + ggtitle(\"stackRatio = 1.5\"),\n", ")\n", "\n", "gggrid(plots2, 2, 450, 300)" ] }, { "cell_type": "markdown", "id": "structural-national", "metadata": {}, "source": [ "### Parameter `dotSize`" ] }, { "cell_type": "code", "execution_count": 11, "id": "heated-shower", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val plots3 = listOf(\n", " plotBase + geomYDotplot(binWidth = 1.5, dotSize = 1) + ggtitle(\"dotSize = 1\"),\n", " plotBase + geomYDotplot(binWidth = 1.5, dotSize = 0.5) + ggtitle(\"dotSize = 0.5\"),\n", " plotBase + geomYDotplot(binWidth = 1.5, dotSize = 1.5) + ggtitle(\"dotSize = 1.5\"),\n", ")\n", "\n", "gggrid(plots3, 2, 450, 300)" ] }, { "cell_type": "markdown", "id": "enhanced-granny", "metadata": {}, "source": [ "### Parameter `center`" ] }, { "cell_type": "code", "execution_count": 12, "id": "cardiac-defensive", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val plots4 = listOf(\n", " plotBase + geomYDotplot(binWidth = 1.5, method = \"histodot\") + ggtitle(\"Default\"),\n", " plotBase + geomYDotplot(binWidth = 1.5, method = \"histodot\", center = 11) + ggtitle(\"center = 11\"),\n", ")\n", "\n", "gggrid(plots4, 2, 450, 300)" ] }, { "cell_type": "markdown", "id": "competent-frequency", "metadata": {}, "source": [ "### Parameter `boundary`" ] }, { "cell_type": "code", "execution_count": 13, "id": "driving-deputy", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val plots5 = listOf(\n", " plotBase + geomYDotplot(binWidth = 1.5, method = \"histodot\") + ggtitle(\"Default\"),\n", " plotBase + geomYDotplot(binWidth = 1.5, method = \"histodot\", boundary = 12) + ggtitle(\"boundary = 12\"),\n", ")\n", "\n", "gggrid(plots5, 2, 450, 300)" ] }, { "cell_type": "markdown", "id": "worthy-summer", "metadata": {}, "source": [ "### Parameter `bins`" ] }, { "cell_type": "code", "execution_count": 14, "id": "continued-exclusion", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val plots6 = listOf(\n", " plotBase + geomYDotplot(method = \"histodot\") + ggtitle(\"Default (30)\"),\n", " plotBase + geomYDotplot(method = \"histodot\", bins = 15) + ggtitle(\"bins = 15\"),\n", ")\n", "\n", "gggrid(plots6, 2, 450, 300)" ] }, { "cell_type": "markdown", "id": "comic-republican", "metadata": {}, "source": [ "### Grouping" ] }, { "cell_type": "code", "execution_count": 15, "id": "commercial-thirty", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val plots7 = listOf(\n", " plotBase + geomYDotplot(binWidth = 1.6, stackRatio = .75, color = \"white\") {\n", " fill = asDiscrete(\"year\")\n", " } + ggtitle(\"method = \\\"dotdensity\\\", stackGroups = false (default)\"),\n", " plotBase + geomYDotplot(binWidth = 1.6, stackRatio = .75, color = \"white\", \n", " stackGroups = true) {\n", " fill = asDiscrete(\"year\")\n", " } + ggtitle(\"method = \\\"dotdensity\\\", stackGroups = true\"),\n", " plotBase + geomYDotplot(binWidth = 1.6, stackRatio = .75, color = \"white\", \n", " stackGroups = true, method = \"histodot\") {\n", " fill = asDiscrete(\"year\")\n", " } + ggtitle(\"method = \\\"dotdensity\\\", stackGroups = true\"),\n", ")\n", "\n", "gggrid(plots7, 1, 800, 300, fit = true)\n" ] }, { "cell_type": "markdown", "id": "appropriate-nashville", "metadata": {}, "source": [ "### Tooltips" ] }, { "cell_type": "code", "execution_count": 16, "id": "sublime-forest", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "letsPlot(mpg100.toMap()) {x = \"drv\"; y = \"hwy\"} + \n", " geomYDotplot(tooltips = layerTooltips()\n", " .title(\"Drivetrain: ^x\")\n", " .line(\"stack center|^y\")\n", " .line(\"Number of dots in stack|@..count..\")\n", " .line(\"Dot diameter|@..binwidth..\")\n", " )" ] }, { "cell_type": "markdown", "id": "heated-questionnaire", "metadata": {}, "source": [ "### Facets" ] }, { "cell_type": "code", "execution_count": 17, "id": "female-strength", "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": 18, "id": "excessive-camcorder", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plotBase2 + geomYDotplot(binWidth = 1.5) {\n", " color = asDiscrete(\"drv\")\n", " fill = asDiscrete(\"drv\")\n", " } + facetGrid(x = \"year\") + ggtitle(\"facetGrid\")\n" ] }, { "cell_type": "markdown", "id": "indoor-congo", "metadata": {}, "source": [ "### `coordFlip()`" ] }, { "cell_type": "code", "execution_count": 19, "id": "intelligent-bedroom", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "letsPlot(mpg100.toMap()) + geomYDotplot(binWidth = 1.5, stackRatio = .3, dotSize = .8, color = \"white\") {\n", " x = \"drv\"; y = \"hwy\"; fill = \"drv\"\n", "} + 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 }