{ "cells": [ { "cell_type": "markdown", "id": "reserved-institution", "metadata": {}, "source": [ "### Dotplot Geometry" ] }, { "cell_type": "code", "execution_count": 1, "id": "engaging-filter", "metadata": { "execution": { "iopub.execute_input": "2024-08-23T11:04:13.305184Z", "iopub.status.busy": "2024-08-23T11:04:13.303531Z", "iopub.status.idle": "2024-08-23T11:04:15.874119Z", "shell.execute_reply": "2024-08-23T11:04:15.873787Z" } }, "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": { "execution": { "iopub.execute_input": "2024-08-23T11:04:15.876613Z", "iopub.status.busy": "2024-08-23T11:04:15.876070Z", "iopub.status.idle": "2024-08-23T11:04:15.913470Z", "shell.execute_reply": "2024-08-23T11:04:15.912961Z" } }, "outputs": [ { "data": { "text/plain": [ "Lets-Plot Kotlin API v.4.7.2. Frontend: Notebook with dynamically loaded JS. Lets-Plot JS v.4.3.2." ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "LetsPlot.getInfo()" ] }, { "cell_type": "code", "execution_count": 3, "id": "serial-matthew", "metadata": { "execution": { "iopub.execute_input": "2024-08-23T11:04:15.915668Z", "iopub.status.busy": "2024-08-23T11:04:15.915191Z", "iopub.status.idle": "2024-08-23T11:04:16.469310Z", "shell.execute_reply": "2024-08-23T11:04:16.469049Z" } }, "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", "

DataFrame: rowsCount = 5, columnsCount = 12

\n", "
untitledmanufacturermodeldisplyearcyltransdrvctyhwyflclass
1audia41.80000019994auto(l5)f1829pcompact
2audia41.80000019994manual(m5)f2129pcompact
3audia42.00000020084manual(m6)f2031pcompact
4audia42.00000020084auto(av)f2130pcompact
5audia42.80000019996auto(l5)f1626pcompact
\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": { "execution": { "iopub.execute_input": "2024-08-23T11:04:16.471887Z", "iopub.status.busy": "2024-08-23T11:04:16.471400Z", "iopub.status.idle": "2024-08-23T11:04:16.505373Z", "shell.execute_reply": "2024-08-23T11:04:16.505061Z" } }, "outputs": [], "source": [ "val mpg32 = mpg.shuffle().take(32)" ] }, { "cell_type": "code", "execution_count": 5, "id": "juvenile-continuity", "metadata": { "execution": { "iopub.execute_input": "2024-08-23T11:04:16.507680Z", "iopub.status.busy": "2024-08-23T11:04:16.507221Z", "iopub.status.idle": "2024-08-23T11:04:16.631299Z", "shell.execute_reply": "2024-08-23T11:04:16.631471Z" } }, "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": { "execution": { "iopub.execute_input": "2024-08-23T11:04:16.633391Z", "iopub.status.busy": "2024-08-23T11:04:16.633132Z", "iopub.status.idle": "2024-08-23T11:04:16.650659Z", "shell.execute_reply": "2024-08-23T11:04:16.650394Z" } }, "outputs": [], "source": [ "val PACIFIC_BLUE = \"#118ed8\"" ] }, { "cell_type": "code", "execution_count": 7, "id": "signed-child", "metadata": { "execution": { "iopub.execute_input": "2024-08-23T11:04:16.652479Z", "iopub.status.busy": "2024-08-23T11:04:16.652215Z", "iopub.status.idle": "2024-08-23T11:04:16.724635Z", "shell.execute_reply": "2024-08-23T11:04:16.724807Z" } }, "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": { "execution": { "iopub.execute_input": "2024-08-23T11:04:16.726606Z", "iopub.status.busy": "2024-08-23T11:04:16.726345Z", "iopub.status.idle": "2024-08-23T11:04:16.787736Z", "shell.execute_reply": "2024-08-23T11:04:16.787908Z" } }, "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": { "execution": { "iopub.execute_input": "2024-08-23T11:04:16.789862Z", "iopub.status.busy": "2024-08-23T11:04:16.789598Z", "iopub.status.idle": "2024-08-23T11:04:16.879651Z", "shell.execute_reply": "2024-08-23T11:04:16.879438Z" } }, "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": { "execution": { "iopub.execute_input": "2024-08-23T11:04:16.880999Z", "iopub.status.busy": "2024-08-23T11:04:16.880842Z", "iopub.status.idle": "2024-08-23T11:04:16.943918Z", "shell.execute_reply": "2024-08-23T11:04:16.944034Z" } }, "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": { "execution": { "iopub.execute_input": "2024-08-23T11:04:16.945257Z", "iopub.status.busy": "2024-08-23T11:04:16.945026Z", "iopub.status.idle": "2024-08-23T11:04:17.009831Z", "shell.execute_reply": "2024-08-23T11:04:17.009939Z" } }, "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": { "execution": { "iopub.execute_input": "2024-08-23T11:04:17.010963Z", "iopub.status.busy": "2024-08-23T11:04:17.010813Z", "iopub.status.idle": "2024-08-23T11:04:17.066644Z", "shell.execute_reply": "2024-08-23T11:04:17.066750Z" } }, "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": { "execution": { "iopub.execute_input": "2024-08-23T11:04:17.067845Z", "iopub.status.busy": "2024-08-23T11:04:17.067698Z", "iopub.status.idle": "2024-08-23T11:04:17.121911Z", "shell.execute_reply": "2024-08-23T11:04:17.122106Z" } }, "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": { "execution": { "iopub.execute_input": "2024-08-23T11:04:17.123832Z", "iopub.status.busy": "2024-08-23T11:04:17.123583Z", "iopub.status.idle": "2024-08-23T11:04:17.176614Z", "shell.execute_reply": "2024-08-23T11:04:17.176799Z" } }, "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": { "execution": { "iopub.execute_input": "2024-08-23T11:04:17.178185Z", "iopub.status.busy": "2024-08-23T11:04:17.177959Z", "iopub.status.idle": "2024-08-23T11:04:17.235175Z", "shell.execute_reply": "2024-08-23T11:04:17.235282Z" } }, "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": { "execution": { "iopub.execute_input": "2024-08-23T11:04:17.236345Z", "iopub.status.busy": "2024-08-23T11:04:17.236207Z", "iopub.status.idle": "2024-08-23T11:04:17.309739Z", "shell.execute_reply": "2024-08-23T11:04:17.309845Z" } }, "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": { "execution": { "iopub.execute_input": "2024-08-23T11:04:17.311031Z", "iopub.status.busy": "2024-08-23T11:04:17.310884Z", "iopub.status.idle": "2024-08-23T11:04:17.375376Z", "shell.execute_reply": "2024-08-23T11:04:17.375487Z" } }, "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": { "execution": { "iopub.execute_input": "2024-08-23T11:04:17.376689Z", "iopub.status.busy": "2024-08-23T11:04:17.376552Z", "iopub.status.idle": "2024-08-23T11:04:17.405405Z", "shell.execute_reply": "2024-08-23T11:04:17.405219Z" } }, "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": { "execution": { "iopub.execute_input": "2024-08-23T11:04:17.406717Z", "iopub.status.busy": "2024-08-23T11:04:17.406553Z", "iopub.status.idle": "2024-08-23T11:04:17.468117Z", "shell.execute_reply": "2024-08-23T11:04:17.468293Z" } }, "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": { "execution": { "iopub.execute_input": "2024-08-23T11:04:17.469816Z", "iopub.status.busy": "2024-08-23T11:04:17.469574Z", "iopub.status.idle": "2024-08-23T11:04:17.525008Z", "shell.execute_reply": "2024-08-23T11:04:17.524794Z" } }, "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.9.23" } }, "nbformat": 4, "nbformat_minor": 5 }