{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# The `orientation` Parameter\n", "\n", "Some geoms treat each axis differently and, thus, can have two orientations.\n", "\n", "The `orientation` parameter specifies the axis that the layer's stat and geom should run along (x-axis by default). " ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:11:00.092438Z", "iopub.status.busy": "2024-04-26T12:11:00.092438Z", "iopub.status.idle": "2024-04-26T12:11:00.974770Z", "shell.execute_reply": "2024-04-26T12:11:00.974770Z" } }, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "\n", "from lets_plot import *\n", "from lets_plot.mapping import as_discrete" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:11:00.974770Z", "iopub.status.busy": "2024-04-26T12:11:00.974770Z", "iopub.status.idle": "2024-04-26T12:11:00.990340Z", "shell.execute_reply": "2024-04-26T12:11:00.990340Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## One Axis Is Discrete, the Other Continuous" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:11:01.006112Z", "iopub.status.busy": "2024-04-26T12:11:01.006112Z", "iopub.status.idle": "2024-04-26T12:11:01.132537Z", "shell.execute_reply": "2024-04-26T12:11:01.132537Z" } }, "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", "
Unnamed: 0manufacturermodeldisplyearcyltransdrvctyhwyflclass
01audia41.819994auto(l5)f1829pcompact
12audia41.819994manual(m5)f2129pcompact
23audia42.020084manual(m6)f2031pcompact
\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", "\n", " fl class \n", "0 p compact \n", "1 p compact \n", "2 p compact " ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mpg_df = pd.read_csv(\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv\")\n", "print(mpg_df.shape)\n", "mpg_df.head(3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `geom_bar()`" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:11:01.132537Z", "iopub.status.busy": "2024-04-26T12:11:01.132537Z", "iopub.status.idle": "2024-04-26T12:11:01.148183Z", "shell.execute_reply": "2024-04-26T12:11:01.148183Z" } }, "outputs": [], "source": [ "base = ggplot(mpg_df) + ggsize(800, 300)\n", "manufacturer_mapping = as_discrete('manufacturer', order_by='..count..')\n", "hide_x_label = theme(axis_title_x='blank')\n", "hide_y_label = theme(axis_title_y='blank')" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:11:01.148183Z", "iopub.status.busy": "2024-04-26T12:11:01.148183Z", "iopub.status.idle": "2024-04-26T12:11:01.243493Z", "shell.execute_reply": "2024-04-26T12:11:01.243493Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Orientation \"x\" (default) : \"manufacturer\" is mapped to the x-axis.\n", "orientation_x = base + geom_bar(aes(x=manufacturer_mapping, fill='class'), color='white')\n", "orientation_x + hide_x_label" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:11:01.243493Z", "iopub.status.busy": "2024-04-26T12:11:01.243493Z", "iopub.status.idle": "2024-04-26T12:11:01.259111Z", "shell.execute_reply": "2024-04-26T12:11:01.259111Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Flip coordinates.\n", "orientation_x + coord_flip() + hide_x_label" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:11:01.259111Z", "iopub.status.busy": "2024-04-26T12:11:01.259111Z", "iopub.status.idle": "2024-04-26T12:11:01.274829Z", "shell.execute_reply": "2024-04-26T12:11:01.274829Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Orientation \"y\" : \"manufacturer\" is mapped to the y-axis.\n", "base_y = base + hide_y_label\n", "base_y + geom_bar(aes(y=manufacturer_mapping, fill='class'), color='white', orientation=\"y\")" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:11:01.274829Z", "iopub.status.busy": "2024-04-26T12:11:01.274829Z", "iopub.status.idle": "2024-04-26T12:11:01.290357Z", "shell.execute_reply": "2024-04-26T12:11:01.290357Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# The \"y\" orientation is automatically selected if x is a continuous axis and y is a discrete axis.\n", "base_y + geom_bar(aes(y=manufacturer_mapping, fill='class'), color='white')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `geom_boxplot()`" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:11:01.290357Z", "iopub.status.busy": "2024-04-26T12:11:01.290357Z", "iopub.status.idle": "2024-04-26T12:11:01.306062Z", "shell.execute_reply": "2024-04-26T12:11:01.306062Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Implicit change of orientation.\n", "base_y + geom_boxplot(aes(y='manufacturer', x='cty'), width=0.7)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `geom_violin()`" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:11:01.306062Z", "iopub.status.busy": "2024-04-26T12:11:01.306062Z", "iopub.status.idle": "2024-04-26T12:11:01.384584Z", "shell.execute_reply": "2024-04-26T12:11:01.384584Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Implicit change of orientation.\n", "ggplot(mpg_df) + \\\n", " geom_violin(aes(y=\"drv\", x='cty', fill=\"drv\"), trim=False) + \\\n", " geom_boxplot(aes(y=\"drv\", x='cty'), fill=\"white\", alpha=0.5, width=0.3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `geom_lollipop()`" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:11:01.384584Z", "iopub.status.busy": "2024-04-26T12:11:01.384584Z", "iopub.status.idle": "2024-04-26T12:11:01.400354Z", "shell.execute_reply": "2024-04-26T12:11:01.400354Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Implicit change of orientation.\n", "base_y + geom_lollipop(aes(y='manufacturer'), stat='count') + scale_y_continuous(expand=[.05, 0])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `stat_summary()`" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:11:01.400354Z", "iopub.status.busy": "2024-04-26T12:11:01.400354Z", "iopub.status.idle": "2024-04-26T12:11:01.416131Z", "shell.execute_reply": "2024-04-26T12:11:01.416131Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Implicit change of orientation.\n", "base_y + stat_summary(aes(y='manufacturer', x='cty'), width=0.7)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `as_discrete()`\n", "\n", "Sometimes an explicit use of `as_discrete()` is required - when the discrete data on the y-axis is represented by numbers." ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:11:01.416131Z", "iopub.status.busy": "2024-04-26T12:11:01.416131Z", "iopub.status.idle": "2024-04-26T12:11:01.447595Z", "shell.execute_reply": "2024-04-26T12:11:01.447595Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Explicit change of orientation through as_discrete().\n", "gggrid([\n", " ggplot(mpg_df) + geom_bar(aes('cty', 'cyl', fill='cyl'), stat='sum', size=0) + scale_fill_discrete(),\n", " ggplot(mpg_df) + geom_bar(aes('cty', as_discrete('cyl'), fill='cyl'), stat='sum', size=0) + scale_fill_discrete(),\n", "])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Continuous Variables\n", "\n", "In the case where both variables are continuous, the orientation should always be specified explicitly." ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:11:01.447595Z", "iopub.status.busy": "2024-04-26T12:11:01.447595Z", "iopub.status.idle": "2024-04-26T12:11:01.463051Z", "shell.execute_reply": "2024-04-26T12:11:01.463051Z" } }, "outputs": [], "source": [ "#\n", "# Density estimates.\n", "#\n", "\n", "np.random.seed(0)\n", "\n", "cov0 = [[1, -.8], \n", " [-.8, 1]] \n", "cov1 = [[ 10, .1],\n", " [.1, .1]]\n", "\n", "x0, y0 = np.random.multivariate_normal(mean=[-2,0], cov=cov0, size=200).T\n", "x1, y1 = np.random.multivariate_normal(mean=[0,1], cov=cov1, size=200).T\n", "\n", "data = dict(\n", " x = np.concatenate((x0, x1)),\n", " y = np.concatenate((y0, y1)),\n", " c = [\"A\"]*200 + [\"B\"]*200\n", ")" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:11:01.463051Z", "iopub.status.busy": "2024-04-26T12:11:01.463051Z", "iopub.status.idle": "2024-04-26T12:11:01.478862Z", "shell.execute_reply": "2024-04-26T12:11:01.478862Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = ggplot(data, aes(\"x\", \"y\", color=\"c\")) + geom_point()\n", "p" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `geom_density()`" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:11:01.480325Z", "iopub.status.busy": "2024-04-26T12:11:01.480325Z", "iopub.status.idle": "2024-04-26T12:11:01.604776Z", "shell.execute_reply": "2024-04-26T12:11:01.604776Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " p + geom_density(size=2),\n", " p + geom_density(size=2, orientation=\"y\"),\n", "])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `geom_histogram()`" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:11:01.604776Z", "iopub.status.busy": "2024-04-26T12:11:01.604776Z", "iopub.status.idle": "2024-04-26T12:11:01.636260Z", "shell.execute_reply": "2024-04-26T12:11:01.636260Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " p + geom_histogram(fill=\"rgba(0,0,0,0)\"),\n", " p + geom_histogram(alpha=0, orientation=\"y\", show_legend=False),\n", "])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `geom_freqpoly()`" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:11:01.636260Z", "iopub.status.busy": "2024-04-26T12:11:01.636260Z", "iopub.status.idle": "2024-04-26T12:11:01.667519Z", "shell.execute_reply": "2024-04-26T12:11:01.667519Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " p + geom_freqpoly(size=2),\n", " p + geom_freqpoly(size=2, orientation=\"y\"),\n", "])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `geom_smooth()`" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:11:01.668740Z", "iopub.status.busy": "2024-04-26T12:11:01.668740Z", "iopub.status.idle": "2024-04-26T12:11:01.714796Z", "shell.execute_reply": "2024-04-26T12:11:01.714796Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " p + geom_smooth(),\n", " p + geom_smooth() + coord_flip(),\n", " p + geom_smooth(orientation=\"y\"),\n", "])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `geom_boxplot()`" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:11:01.714796Z", "iopub.status.busy": "2024-04-26T12:11:01.714796Z", "iopub.status.idle": "2024-04-26T12:11:01.746172Z", "shell.execute_reply": "2024-04-26T12:11:01.746172Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + \\\n", " geom_boxplot(x=-10, color=\"black\") + \\\n", " geom_boxplot(y=-3, color=\"black\", orientation=\"y\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `geom_violin()`" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:11:01.746172Z", "iopub.status.busy": "2024-04-26T12:11:01.746172Z", "iopub.status.idle": "2024-04-26T12:11:01.840197Z", "shell.execute_reply": "2024-04-26T12:11:01.840197Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + \\\n", " geom_violin(x=-10, color=\"black\", alpha=0) + \\\n", " geom_violin(y=-3, color=\"black\", alpha=0, orientation=\"y\")" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:11:01.840197Z", "iopub.status.busy": "2024-04-26T12:11:01.840197Z", "iopub.status.idle": "2024-04-26T12:11:01.918970Z", "shell.execute_reply": "2024-04-26T12:11:01.918970Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + geom_violin(aes(fill=\"c\"), y=-2, color=\"black\", \n", " alpha=0.3, position=\"identity\", width=3, show_legend=False,\n", " orientation=\"y\")" ] } ], "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": 4 }