{ "cells": [ { "cell_type": "markdown", "id": "f78cfe2b", "metadata": {}, "source": [ "# Stackable Position Adjustments" ] }, { "cell_type": "markdown", "id": "3ab13984", "metadata": {}, "source": [ "## Table of Contents\n", "\n", "1. [Default Presentation of `'stack'` and `'fill'`](#default)\n", "\n", "2. [Stacking Order](#order)\n", "\n", "3. [Other Geometries](#other-geoms)\n", "\n", "4. [Parameter `vjust`](#vjust)\n", "\n", "5. [Parameter `mode`](#mode)\n", "\n", "6. [Negative Values](#negative)" ] }, { "cell_type": "code", "execution_count": 1, "id": "1572041f", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:01:00.413903Z", "iopub.status.busy": "2024-04-26T12:01:00.413903Z", "iopub.status.idle": "2024-04-26T12:01:01.374235Z", "shell.execute_reply": "2024-04-26T12:01:01.374235Z" } }, "outputs": [], "source": [ "import pandas as pd\n", "\n", "from lets_plot import *\n", "from lets_plot.mapping import *" ] }, { "cell_type": "code", "execution_count": 2, "id": "24920b1e", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:01:01.374235Z", "iopub.status.busy": "2024-04-26T12:01:01.374235Z", "iopub.status.idle": "2024-04-26T12:01:01.390977Z", "shell.execute_reply": "2024-04-26T12:01:01.390046Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 3, "id": "c30f9d65", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:01:01.390977Z", "iopub.status.busy": "2024-04-26T12:01:01.390977Z", "iopub.status.idle": "2024-04-26T12:01:01.531310Z", "shell.execute_reply": "2024-04-26T12:01:01.531310Z" } }, "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": 3, "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": "markdown", "id": "45c88372", "metadata": {}, "source": [ "\n", "\n", "#### 1. Default Presentation of `'stack'` and `'fill'`\n", "\n", "Stacking is the default behaviour for most area plots.\n", "Fill makes it easier to compare proportions." ] }, { "cell_type": "code", "execution_count": 4, "id": "aa2c468e", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:01:01.531310Z", "iopub.status.busy": "2024-04-26T12:01:01.531310Z", "iopub.status.idle": "2024-04-26T12:01:01.672117Z", "shell.execute_reply": "2024-04-26T12:01:01.672117Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(df, aes(x=\"drv\", fill=as_discrete(\"year\"))) + \\\n", " geom_bar() + \\\n", " ggtitle(\"bar: position='stack' (default)\")" ] }, { "cell_type": "code", "execution_count": 5, "id": "02960166", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:01:01.672117Z", "iopub.status.busy": "2024-04-26T12:01:01.672117Z", "iopub.status.idle": "2024-04-26T12:01:01.703579Z", "shell.execute_reply": "2024-04-26T12:01:01.703579Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(df, aes(x=\"drv\", fill=as_discrete(\"year\"))) + \\\n", " geom_bar(position='fill') + \\\n", " ggtitle(\"bar: position='fill'\")" ] }, { "cell_type": "code", "execution_count": 6, "id": "2769dac6", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:01:01.703579Z", "iopub.status.busy": "2024-04-26T12:01:01.703579Z", "iopub.status.idle": "2024-04-26T12:01:01.719137Z", "shell.execute_reply": "2024-04-26T12:01:01.719137Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(df, aes(x=\"hwy\", fill=as_discrete(\"year\"))) + \\\n", " geom_histogram() + \\\n", " ggtitle(\"histogram: position='stack' (default)\")" ] }, { "cell_type": "code", "execution_count": 7, "id": "7a0cd8f3", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:01:01.719137Z", "iopub.status.busy": "2024-04-26T12:01:01.719137Z", "iopub.status.idle": "2024-04-26T12:01:01.734819Z", "shell.execute_reply": "2024-04-26T12:01:01.734819Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(df, aes(x=\"hwy\", fill=as_discrete(\"year\"))) + \\\n", " geom_histogram(position='fill') + \\\n", " ggtitle(\"histogram: position='fill'\")" ] }, { "cell_type": "code", "execution_count": 8, "id": "abe5c551", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:01:01.734819Z", "iopub.status.busy": "2024-04-26T12:01:01.734819Z", "iopub.status.idle": "2024-04-26T12:01:01.797604Z", "shell.execute_reply": "2024-04-26T12:01:01.797604Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(df, aes(x=\"hwy\", fill=as_discrete(\"year\"))) + \\\n", " geom_area(stat='density', color=\"black\") + \\\n", " ggtitle(\"area: position='stack' (default)\")" ] }, { "cell_type": "code", "execution_count": 9, "id": "959c55a8", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:01:01.797604Z", "iopub.status.busy": "2024-04-26T12:01:01.797604Z", "iopub.status.idle": "2024-04-26T12:01:01.860091Z", "shell.execute_reply": "2024-04-26T12:01:01.860091Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(df, aes(x=\"hwy\", fill=as_discrete(\"year\"))) + \\\n", " geom_area(stat='density', position='fill', color=\"black\") + \\\n", " ggtitle(\"area: position='fill'\")" ] }, { "cell_type": "markdown", "id": "d12ed7c6", "metadata": {}, "source": [ "\n", "\n", "#### 2. Stacking Order\n", "\n", "Control the stacking order by changing the `order` parameter of the `as_discrete()` function." ] }, { "cell_type": "code", "execution_count": 10, "id": "b9022666", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:01:01.860091Z", "iopub.status.busy": "2024-04-26T12:01:01.860091Z", "iopub.status.idle": "2024-04-26T12:01:01.938581Z", "shell.execute_reply": "2024-04-26T12:01:01.938581Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(df, aes(x=\"hwy\")) + \\\n", " geom_area(aes(fill=as_discrete(\"drv\")), stat='density', color=\"black\") + \\\n", " ggtitle(\"Default order of drive types\")" ] }, { "cell_type": "code", "execution_count": 11, "id": "f2ac8383", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:01:01.938581Z", "iopub.status.busy": "2024-04-26T12:01:01.938581Z", "iopub.status.idle": "2024-04-26T12:01:02.016913Z", "shell.execute_reply": "2024-04-26T12:01:02.016913Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(df, aes(x=\"hwy\")) + \\\n", " geom_area(aes(fill=as_discrete(\"drv\", order=-1)), stat='density', color=\"black\") + \\\n", " ggtitle(\"Backward order of drive types\")" ] }, { "cell_type": "markdown", "id": "d1be5ff4", "metadata": {}, "source": [ "\n", "\n", "#### 3. Other Geometries\n", "\n", "Let's have a look at the geometries, for which the default position adjustment differs from `'stack'` and `'fill'`.\n", "\n", "When stacking across multiple layers it's a good idea to set the `group` aesthetic - this ensures that all layers are stacked in the same way." ] }, { "cell_type": "code", "execution_count": 12, "id": "9269044d", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:01:02.016913Z", "iopub.status.busy": "2024-04-26T12:01:02.016913Z", "iopub.status.idle": "2024-04-26T12:01:02.048740Z", "shell.execute_reply": "2024-04-26T12:01:02.048740Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(df, aes(x=\"drv\", color=as_discrete(\"year\"))) + \\\n", " geom_line(stat='count', position='stack') + \\\n", " geom_point(stat='count', position='stack') + \\\n", " ggtitle(\"Line and point\")" ] }, { "cell_type": "code", "execution_count": 13, "id": "6fea6dd8", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:01:02.048740Z", "iopub.status.busy": "2024-04-26T12:01:02.048740Z", "iopub.status.idle": "2024-04-26T12:01:02.081587Z", "shell.execute_reply": "2024-04-26T12:01:02.080327Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(df, aes(x=\"drv\")) + \\\n", " geom_area(aes(fill=as_discrete(\"year\")), stat='count', position='stack', color=\"black\") + \\\n", " geom_line(aes(group=\"year\"), stat='count', position='stack', color=\"black\") + \\\n", " ggtitle(\"Area and line\")" ] }, { "cell_type": "markdown", "id": "83f4120e", "metadata": {}, "source": [ "\n", "\n", "#### 4. Parameter `vjust`\n", "\n", "The `vjust` argument of `position_stack()` is used to move the location of plot elements vertically (vjust stands for vertical adjustment)." ] }, { "cell_type": "code", "execution_count": 14, "id": "56c79857", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:01:02.081587Z", "iopub.status.busy": "2024-04-26T12:01:02.081587Z", "iopub.status.idle": "2024-04-26T12:01:02.113535Z", "shell.execute_reply": "2024-04-26T12:01:02.112094Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(df, aes(x=\"drv\")) + \\\n", " geom_area(aes(fill=as_discrete(\"year\")), stat='count', \\\n", " position='stack', color=\"black\") + \\\n", " geom_label(aes(label=\"..count..\", group=\"year\"), stat='count', \\\n", " position='stack', color=\"black\") + \\\n", " ggtitle(\"vjust=1 (default)\")" ] }, { "cell_type": "code", "execution_count": 15, "id": "689596f8", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:01:02.113535Z", "iopub.status.busy": "2024-04-26T12:01:02.113535Z", "iopub.status.idle": "2024-04-26T12:01:02.144004Z", "shell.execute_reply": "2024-04-26T12:01:02.144004Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(df, aes(x=\"drv\")) + \\\n", " geom_area(aes(fill=as_discrete(\"year\")), stat='count', \\\n", " position='stack', color=\"black\") + \\\n", " geom_label(aes(label=\"..count..\", group=\"year\"), stat='count', \\\n", " position=position_stack(vjust=.5), color=\"black\") + \\\n", " ggtitle(\"vjust=0.5\")" ] }, { "cell_type": "markdown", "id": "fba93796", "metadata": {}, "source": [ "\n", "\n", "#### 5. Parameter `mode`\n", "\n", "By default only objects from different groups are stacked over each other, and objects inside one group are positioned as in `position='identity'`.\n", "\n", "This behaviour could be changed by switching `mode` parameter.\n", "\n", "`mode='all'` means, that each object will be shifted." ] }, { "cell_type": "code", "execution_count": 16, "id": "4d1e5c20", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:01:02.144004Z", "iopub.status.busy": "2024-04-26T12:01:02.144004Z", "iopub.status.idle": "2024-04-26T12:01:02.175494Z", "shell.execute_reply": "2024-04-26T12:01:02.175494Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(df, aes(x=\"hwy\", color=\"class\")) + \\\n", " geom_point(y=1, position=position_stack()) + \\\n", " coord_fixed() + ylim(1, 35) + \\\n", " ggtitle(\"mode='groups' (default)\")" ] }, { "cell_type": "code", "execution_count": 17, "id": "de17d673", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:01:02.175494Z", "iopub.status.busy": "2024-04-26T12:01:02.175494Z", "iopub.status.idle": "2024-04-26T12:01:02.206987Z", "shell.execute_reply": "2024-04-26T12:01:02.206987Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(df, aes(x=\"hwy\", color=\"class\")) + \\\n", " geom_point(y=1, position=position_stack(mode='all')) + \\\n", " coord_fixed() + ylim(1, 35) + \\\n", " ggtitle(\"mode='all'\")" ] }, { "cell_type": "markdown", "id": "ba6cd973", "metadata": {}, "source": [ "\n", "\n", "#### 6. Negative Values\n", "\n", "Stacking supports positive and negative shifts of the objects separately." ] }, { "cell_type": "code", "execution_count": 18, "id": "270a9a1e", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:01:02.206987Z", "iopub.status.busy": "2024-04-26T12:01:02.206987Z", "iopub.status.idle": "2024-04-26T12:01:02.222576Z", "shell.execute_reply": "2024-04-26T12:01:02.222576Z" } }, "outputs": [], "source": [ "data = {\n", " \"x\": [0, 1, 2, 0, 1, 2],\n", " \"y\": [4, 2, 3, 0, 1, -1],\n", " \"g\": [\"a\", \"a\", \"a\", \"b\", \"b\", \"b\"],\n", "}" ] }, { "cell_type": "code", "execution_count": 19, "id": "b3588ea3", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:01:02.222576Z", "iopub.status.busy": "2024-04-26T12:01:02.222576Z", "iopub.status.idle": "2024-04-26T12:01:02.238223Z", "shell.execute_reply": "2024-04-26T12:01:02.238223Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(data, aes(\"x\", \"y\", fill=\"g\")) + \\\n", " geom_bar(stat='identity') + \\\n", " geom_hline(yintercept=0, color=\"black\")" ] }, { "cell_type": "code", "execution_count": 20, "id": "5e4750bb", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:01:02.238223Z", "iopub.status.busy": "2024-04-26T12:01:02.238223Z", "iopub.status.idle": "2024-04-26T12:01:02.253843Z", "shell.execute_reply": "2024-04-26T12:01:02.253843Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(data, aes(\"x\", \"y\")) + \\\n", " geom_bar(aes(fill=\"g\"), stat='identity', position='fill') + \\\n", " geom_label(aes(label=\"g\", group=\"g\"), stat='identity', \\\n", " position=position_fill(vjust=.5)) + \\\n", " geom_hline(yintercept=0, color=\"black\")" ] } ], "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.10.13" } }, "nbformat": 4, "nbformat_minor": 5 }