{ "cells": [ { "cell_type": "markdown", "id": "4fc94f03", "metadata": {}, "source": [ "# New variables computed by `'count'` and `'count2d'` statistics\n", "\n", "`geomBar()` uses `'count'` statistical transformation. In addition to `'..count..'` variable, the statistics now provide additional variables related to sum (`'..sum..'`), `'..prop..'` (proportion) and `'..proppct..'` (proportion in percent).\n", "\n", "`geomPie()` uses `'count2d'` statistical transformation. It allows to make a slice sizes proportional to the number of cases in each group (or if the weight aesthetic is supplied, the sum of the weights). Also `'count2d'` provides variables for proportion (`'..prop..'`) and proportion in percent (`'..proppct..'`)." ] }, { "cell_type": "code", "execution_count": 1, "id": "4b7169bc", "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": "1595f4ce", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Lets-Plot Kotlin API v.4.4.1. Frontend: Notebook with dynamically loaded JS. Lets-Plot JS v.3.2.0." ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "LetsPlot.getInfo()" ] }, { "cell_type": "markdown", "id": "8b3ba4a8", "metadata": {}, "source": [ "### 1. Data preparation" ] }, { "cell_type": "code", "execution_count": 3, "id": "417bc215", "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 mpgDf = DataFrame.readCSV(\"https://raw.githubusercontent.com/JetBrains/lets-plot-kotlin/master/docs/examples/data/mpg.csv\")\n", "mpgDf.head()\n" ] }, { "cell_type": "code", "execution_count": 4, "id": "362a49d7", "metadata": {}, "outputs": [], "source": [ "val mpgData = mpgDf.toMap()" ] }, { "cell_type": "code", "execution_count": 5, "id": "c0ca3a3e", "metadata": {}, "outputs": [], "source": [ "val blankTheme = theme(line = elementBlank(), axis = elementBlank())" ] }, { "cell_type": "markdown", "id": "ec0e815f", "metadata": {}, "source": [ "Prepare the tooltips content to show the variables calculated by the statistics" ] }, { "cell_type": "code", "execution_count": 6, "id": "4769236b", "metadata": {}, "outputs": [], "source": [ "val tooltipContent = layerTooltips()\n", " .title(\"^fill\")\n", " .line(\"count of records in group|@{..count..} (@{..prop..})\")\n", " .line(\"total count of records|@{..sum..}\")\n", " .format(\"..prop..\", \".0%\")\n", " .format(\"..count..\", \".1f\")\n", " .format(\"..sum..\", \".1f\")" ] }, { "cell_type": "markdown", "id": "b741fe7f", "metadata": {}, "source": [ "### 2. Default presentation" ] }, { "cell_type": "markdown", "id": "91e9ad66", "metadata": {}, "source": [ "Let's show the variables computed by the in the tooltips." ] }, { "cell_type": "code", "execution_count": 7, "id": "c707c39a", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(mpgData) +\n", " geomBar(tooltips = tooltipContent) { x = \"manufacturer\"; fill = \"class\" }" ] }, { "cell_type": "markdown", "id": "c525cb5b", "metadata": {}, "source": [ "Let's build a pie chart where the sector will correspond to the car class.\n", "All statistical information is shown in tooltips." ] }, { "cell_type": "code", "execution_count": 8, "id": "4a2856c9", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(mpgData) +\n", " geomPie(size=24, hole=0.2, stroke=1.0, tooltips = tooltipContent) { fill = \"class\" } +\n", " blankTheme" ] }, { "cell_type": "markdown", "id": "664e6b9f", "metadata": {}, "source": [ "### 3. Compute weighted sum" ] }, { "cell_type": "markdown", "id": "b10fc659", "metadata": {}, "source": [ "Compute weighted sum instead of simple count: let's use total engine displacement of each class." ] }, { "cell_type": "code", "execution_count": 9, "id": "21c837af", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(mpgData) +\n", " geomPie(size=24, hole=0.2, stroke=1.0, tooltips = tooltipContent) { \n", " fill = \"class\"\n", " weight = \"displ\"\n", " } +\n", " blankTheme" ] }, { "cell_type": "markdown", "id": "ce251ba0", "metadata": {}, "source": [ "Then let's order sectors by `'..count..'`:" ] }, { "cell_type": "code", "execution_count": 10, "id": "17be9143", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(mpgData) +\n", " geomPie(size=24, hole=0.2, stroke=1.0, tooltips = tooltipContent) { \n", " fill = asDiscrete(\"class\", orderBy = \"..count..\")\n", " weight = \"displ\"\n", " } +\n", " blankTheme" ] }, { "cell_type": "markdown", "id": "224e352f", "metadata": {}, "source": [ "Map `size` to computed variable `'..sum..'`:" ] }, { "cell_type": "code", "execution_count": 11, "id": "b384e38d", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(mpgData) +\n", " geomPie(tooltips = tooltipContent) { fill = \"class\"; size = \"..sum..\" } +\n", " facetWrap(facets = \"manufacturer\", ncol = 5, order = 1) +\n", " scaleSize(range = 4 to 10) +\n", " guides(size = \"none\") +\n", " blankTheme" ] } ], "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 }