{ "cells": [ { "cell_type": "markdown", "id": "e00d3551", "metadata": {}, "source": [ "# Automatically Choose `orientation=\"y\"` When Aes Y Is Discrete" ] }, { "cell_type": "code", "execution_count": 1, "id": "ff95f57a", "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": "eae93dff", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 3, "id": "26cdc00e", "metadata": {}, "outputs": [ { "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", "
countrycontinentyearlifeExppopgdpPercap
0AfghanistanAsia195228.8018425333779.445314
1AfghanistanAsia195730.3329240934820.853030
2AfghanistanAsia196231.99710267083853.100710
\n", "
" ], "text/plain": [ " country continent year lifeExp pop gdpPercap\n", "0 Afghanistan Asia 1952 28.801 8425333 779.445314\n", "1 Afghanistan Asia 1957 30.332 9240934 820.853030\n", "2 Afghanistan Asia 1962 31.997 10267083 853.100710" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.read_csv('https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/gapminder.csv')\n", "albania_df = df.loc[df['country'] == 'Albania']\n", "df.head(3)" ] }, { "cell_type": "markdown", "id": "624bf284", "metadata": {}, "source": [ "#### 1. How Does Automatic Setting `orientation=\"y\"` Work" ] }, { "cell_type": "code", "execution_count": 4, "id": "f1acbed4", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " ggplot(df, aes('continent', 'pop', fill='continent')) + geom_bar() + ggtitle('Default'),\n", " ggplot(df, aes('pop', 'continent', fill='continent')) + geom_bar() + ggtitle('Automatic setting orientation=\"y\"')\n", "], ncol=2)" ] }, { "cell_type": "markdown", "id": "91589cbd", "metadata": {}, "source": [ "#### 2. Marking by `as_discrete()`" ] }, { "cell_type": "markdown", "id": "6fa6b5f2", "metadata": {}, "source": [ "If you're using continuous data for the Y-axis, the plot may be incorrect. Try marking the Y-axis data with the `as_discrete()` function, this will rotate your geom." ] }, { "cell_type": "code", "execution_count": 5, "id": "c28918fd", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " ggplot(albania_df) + geom_bar(aes('lifeExp', 'year'), stat='sum', size=0),\n", " ggplot(albania_df) + geom_bar(aes('lifeExp', as_discrete('year')), stat='sum', size=0)\n", "], ncol=2)" ] }, { "cell_type": "markdown", "id": "47a2e97a", "metadata": {}, "source": [ "#### 3. Other Kinds of Geoms Support Automatic Choosing of Orientation" ] }, { "cell_type": "markdown", "id": "bbd7db96", "metadata": {}, "source": [ "Such manipulations with automatic rotation can be done for geoms `geom_boxplot()`, `geom_violin()`, `geom_bar()` and `geom_lollipop()`." ] }, { "cell_type": "code", "execution_count": 6, "id": "593c52c6", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " ggplot(df, aes('continent', 'lifeExp', fill='continent')) + geom_boxplot(),\n", " ggplot(df, aes('lifeExp', 'continent', fill='continent')) + geom_boxplot(),\n", " ggplot(df, aes('continent', 'lifeExp', fill='continent')) + geom_violin(),\n", " ggplot(df, aes('lifeExp', 'continent', fill='continent')) + geom_violin(),\n", " ggplot(albania_df, aes(as_discrete('year'), 'lifeExp')) + geom_lollipop(stat='sum', size=2),\n", " ggplot(albania_df, aes('lifeExp', as_discrete('year'))) + geom_lollipop(stat='sum', size=2),\n", "], ncol=2)" ] }, { "cell_type": "markdown", "id": "8867feb1", "metadata": {}, "source": [ "#### 4. `stat='boxplot'`, `stat='boxplot_outlier'`, `stat_summary()`" ] }, { "cell_type": "code", "execution_count": 7, "id": "13f2b318", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " ggplot(df, aes('continent', 'lifeExp')) + geom_pointrange(stat='boxplot'),\n", " ggplot(df, aes('lifeExp', 'continent')) + geom_pointrange(stat='boxplot'),\n", " ggplot(df, aes('continent', 'lifeExp')) + geom_pointrange(stat='boxplot_outlier'),\n", " ggplot(df, aes('lifeExp', 'continent')) + geom_pointrange(stat='boxplot_outlier'),\n", " ggplot(df, aes('continent', 'lifeExp')) + stat_summary(),\n", " ggplot(df, aes('lifeExp', 'continent')) + stat_summary()\n", "], ncol=2)" ] } ], "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.19" } }, "nbformat": 4, "nbformat_minor": 5 }