{ "cells": [ { "cell_type": "markdown", "id": "9bf88576-8343-4a6d-b2ae-eaa4a1c2cda7", "metadata": {}, "source": [ "# New Variables `..sumprop..`, `..sumpct..`
in the `count` and `count2d` Statistics\n", "\n", "Computed variables `..sumprop..` and `..sumpct..` take the value of the share of observations at a given location relative to the total number of observations.\n", "\n", "This is in contrast to computed variables `..prop..` and `..proppct..` which take the value of the share of observations belonging to a given group relative to the number of observations at a given location." ] }, { "cell_type": "code", "execution_count": 1, "id": "84496fc2-1ee7-4346-8636-85e546852ae6", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "\n", "from lets_plot import *\n", "from lets_plot.mapping import as_discrete" ] }, { "cell_type": "code", "execution_count": 2, "id": "a7c6c370-8540-4635-bfd4-56cd73894b66", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 3, "id": "03627ff5-011c-4e25-9911-7834fdfdcf8d", "metadata": {}, "outputs": [], "source": [ "data = {\n", " 'x': ['a', 'a', 'a', 'a', 'b', 'b'],\n", " 'group': ['A', 'A', 'A', 'B', 'A', 'B'],\n", "}" ] }, { "cell_type": "code", "execution_count": 4, "id": "6299b559-be59-4a4b-bec2-6ac451a2ae0a", "metadata": {}, "outputs": [], "source": [ "tooltip_options = layer_tooltips([\"..sumprop..\", \"..sumpct..\", \"..prop..\", \"..proppct..\"])" ] }, { "cell_type": "markdown", "id": "9d3b3b6a-973a-4a25-a219-5b651e173ea7", "metadata": {}, "source": [ "#### 1. Use New `..sumprop..` and `..sumpct..` for Plots without Grouping \n", "\n", "> Note: compare vaues shown in the tooltip." ] }, { "cell_type": "code", "execution_count": 5, "id": "0395117c-1e14-4e41-90d3-665f9a767e4b", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(data, aes('x')) + \\\n", " geom_bar(tooltips=tooltip_options, labels=layer_labels().line('@..sumprop.. (@..sumpct..)'))" ] }, { "cell_type": "markdown", "id": "4ebb42b5-ddea-4cde-b94d-a5f2dd391e29", "metadata": {}, "source": [ "#### 2. However \"..prop..\", \"..proppct..\" Better Suit for Grouped Plots \n", "\n", "> Note: again, compare vaues shown in the tooltip." ] }, { "cell_type": "code", "execution_count": 6, "id": "7eafc2c6-068d-4808-8b18-002f1c57f83d", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(data, aes('x', fill='group')) + \\\n", " geom_bar(tooltips=tooltip_options, labels=layer_labels().line('@..prop.. (@..proppct..)'))" ] }, { "cell_type": "markdown", "id": "390bd049-9626-4713-96ef-0312bb0613db", "metadata": {}, "source": [ "#### 3. The `count2d` Stat Works Similarly" ] }, { "cell_type": "code", "execution_count": 7, "id": "7a6abbf0-fb03-49b9-8c13-69d492bf0e12", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(234, 12)\n" ] }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Unnamed: 0manufacturermodeldisplyearcyltransdrvctyhwyflclass
01audia41.819994auto(l5)f1829pcompact
12audia41.819994manual(m5)f2129pcompact
23audia42.020084manual(m6)f2031pcompact
34audia42.020084auto(av)f2130pcompact
45audia42.819996auto(l5)f1626pcompact
\n", "
" ], "text/plain": [ " Unnamed: 0 manufacturer model displ year cyl trans drv cty hwy \\\n", "0 1 audi a4 1.8 1999 4 auto(l5) f 18 29 \n", "1 2 audi a4 1.8 1999 4 manual(m5) f 21 29 \n", "2 3 audi a4 2.0 2008 4 manual(m6) f 20 31 \n", "3 4 audi a4 2.0 2008 4 auto(av) f 21 30 \n", "4 5 audi a4 2.8 1999 6 auto(l5) f 16 26 \n", "\n", " fl class \n", "0 p compact \n", "1 p compact \n", "2 p compact \n", "3 p compact \n", "4 p compact " ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.read_csv(\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv\")\n", "print(df.shape)\n", "df.head()" ] }, { "cell_type": "code", "execution_count": 8, "id": "9e68462d-6f37-49e7-b744-8a5902d382ed", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(df, aes(\"drv\", as_discrete(\"year\"))) + \\\n", " geom_pie(aes(fill=\"class\", size='..sum..'), tooltips=tooltip_options) + \\\n", " scale_size(guide='none')" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.18" } }, "nbformat": 4, "nbformat_minor": 5 }