{ "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": "2025-11-05T13:47:59.521241Z", "iopub.status.busy": "2025-11-05T13:47:59.521137Z", "iopub.status.idle": "2025-11-05T13:47:59.524415Z", "shell.execute_reply": "2025-11-05T13:47:59.524227Z" } }, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "\n", "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:47:59.525428Z", "iopub.status.busy": "2025-11-05T13:47:59.525357Z", "iopub.status.idle": "2025-11-05T13:47:59.527061Z", "shell.execute_reply": "2025-11-05T13:47:59.526895Z" } }, "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": "2025-11-05T13:47:59.540083Z", "iopub.status.busy": "2025-11-05T13:47:59.540003Z", "iopub.status.idle": "2025-11-05T13:47:59.637194Z", "shell.execute_reply": "2025-11-05T13:47:59.636904Z" } }, "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": "2025-11-05T13:47:59.638247Z", "iopub.status.busy": "2025-11-05T13:47:59.638175Z", "iopub.status.idle": "2025-11-05T13:47:59.640310Z", "shell.execute_reply": "2025-11-05T13:47:59.640130Z" } }, "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": "2025-11-05T13:47:59.641258Z", "iopub.status.busy": "2025-11-05T13:47:59.641187Z", "iopub.status.idle": "2025-11-05T13:47:59.672594Z", "shell.execute_reply": "2025-11-05T13:47:59.672213Z" } }, "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": "2025-11-05T13:47:59.673378Z", "iopub.status.busy": "2025-11-05T13:47:59.673289Z", "iopub.status.idle": "2025-11-05T13:47:59.678030Z", "shell.execute_reply": "2025-11-05T13:47:59.677843Z" } }, "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": "2025-11-05T13:47:59.678668Z", "iopub.status.busy": "2025-11-05T13:47:59.678587Z", "iopub.status.idle": "2025-11-05T13:47:59.683544Z", "shell.execute_reply": "2025-11-05T13:47:59.683372Z" } }, "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": "2025-11-05T13:47:59.684175Z", "iopub.status.busy": "2025-11-05T13:47:59.684103Z", "iopub.status.idle": "2025-11-05T13:47:59.688906Z", "shell.execute_reply": "2025-11-05T13:47:59.688739Z" } }, "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": "2025-11-05T13:47:59.689537Z", "iopub.status.busy": "2025-11-05T13:47:59.689466Z", "iopub.status.idle": "2025-11-05T13:47:59.694177Z", "shell.execute_reply": "2025-11-05T13:47:59.694007Z" } }, "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": "2025-11-05T13:47:59.694806Z", "iopub.status.busy": "2025-11-05T13:47:59.694732Z", "iopub.status.idle": "2025-11-05T13:47:59.718209Z", "shell.execute_reply": "2025-11-05T13:47:59.718013Z" } }, "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": "2025-11-05T13:47:59.718925Z", "iopub.status.busy": "2025-11-05T13:47:59.718843Z", "iopub.status.idle": "2025-11-05T13:47:59.723014Z", "shell.execute_reply": "2025-11-05T13:47:59.722837Z" } }, "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": "2025-11-05T13:47:59.723646Z", "iopub.status.busy": "2025-11-05T13:47:59.723569Z", "iopub.status.idle": "2025-11-05T13:47:59.727526Z", "shell.execute_reply": "2025-11-05T13:47:59.727360Z" } }, "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": "2025-11-05T13:47:59.728235Z", "iopub.status.busy": "2025-11-05T13:47:59.728164Z", "iopub.status.idle": "2025-11-05T13:47:59.736800Z", "shell.execute_reply": "2025-11-05T13:47:59.736626Z" } }, "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": "2025-11-05T13:47:59.737480Z", "iopub.status.busy": "2025-11-05T13:47:59.737408Z", "iopub.status.idle": "2025-11-05T13:47:59.739656Z", "shell.execute_reply": "2025-11-05T13:47:59.739475Z" } }, "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": "2025-11-05T13:47:59.740249Z", "iopub.status.busy": "2025-11-05T13:47:59.740175Z", "iopub.status.idle": "2025-11-05T13:47:59.744278Z", "shell.execute_reply": "2025-11-05T13:47:59.744109Z" } }, "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": "2025-11-05T13:47:59.744917Z", "iopub.status.busy": "2025-11-05T13:47:59.744845Z", "iopub.status.idle": "2025-11-05T13:47:59.785383Z", "shell.execute_reply": "2025-11-05T13:47:59.785186Z" } }, "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": "2025-11-05T13:47:59.786148Z", "iopub.status.busy": "2025-11-05T13:47:59.786067Z", "iopub.status.idle": "2025-11-05T13:47:59.795376Z", "shell.execute_reply": "2025-11-05T13:47:59.795198Z" } }, "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": "2025-11-05T13:47:59.796059Z", "iopub.status.busy": "2025-11-05T13:47:59.795984Z", "iopub.status.idle": "2025-11-05T13:47:59.804027Z", "shell.execute_reply": "2025-11-05T13:47:59.803849Z" } }, "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": "2025-11-05T13:47:59.804682Z", "iopub.status.busy": "2025-11-05T13:47:59.804609Z", "iopub.status.idle": "2025-11-05T13:47:59.818534Z", "shell.execute_reply": "2025-11-05T13:47:59.818357Z" } }, "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": "2025-11-05T13:47:59.819196Z", "iopub.status.busy": "2025-11-05T13:47:59.819124Z", "iopub.status.idle": "2025-11-05T13:47:59.831713Z", "shell.execute_reply": "2025-11-05T13:47:59.831520Z" } }, "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": "2025-11-05T13:47:59.832385Z", "iopub.status.busy": "2025-11-05T13:47:59.832310Z", "iopub.status.idle": "2025-11-05T13:47:59.864209Z", "shell.execute_reply": "2025-11-05T13:47:59.864021Z" } }, "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": "2025-11-05T13:47:59.864984Z", "iopub.status.busy": "2025-11-05T13:47:59.864900Z", "iopub.status.idle": "2025-11-05T13:47:59.887322Z", "shell.execute_reply": "2025-11-05T13:47:59.887123Z" } }, "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 }