{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### The layer `orientation`\n", "\n", "Some geoms treat each axis differently and, thus, can thus have two orientations.\n", "\n", "The `orientation` parameter specifies the axis that the layer' stat and geom should run along (x-axis by default). " ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import numpy as np\n", "import pandas as pd\n", "\n", "from lets_plot import *\n", "from lets_plot.mapping import as_discrete\n", "LetsPlot.setup_html()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### geom_bar()" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "mpg = pd.read_csv(\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv\")\n", "\n", "base = ggplot(mpg) + 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": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 3, "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": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "orientation_x + coord_flip() + hide_x_label" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Orientation \"y\".\n", "# The \"manufacturer\" is mapped to the y-axis.\n", "\n", "base_y = base + hide_y_label\n", "base_y + geom_bar(aes(y=manufacturer_mapping, fill='class'), color='white', orientation=\"y\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### geom_boxplot()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "base_y + geom_boxplot(aes(y='manufacturer', x='cty'), width=0.7, orientation=\"y\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `geom_violin()`" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(mpg) + geom_violin(aes(y=\"drv\", x='cty', fill=\"drv\"), trim=False, orientation=\"y\") \\\n", " + geom_boxplot(aes(y=\"drv\", x='cty'), fill=\"white\", alpha=0.5, width=0.3, orientation=\"y\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Continuous variables" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "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": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 9, "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": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + geom_density(size=2)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + geom_density(size=2, orientation=\"y\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `geom_histogram()`" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + geom_histogram(fill=\"rgba(0,0,0,0)\")" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + geom_histogram(alpha=0, orientation=\"y\", show_legend=False)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + geom_freqpoly(size=2)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + geom_freqpoly(size=2, orientation=\"y\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `geom_smooth()`" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + geom_smooth()" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + geom_smooth() + coord_flip()" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + geom_smooth(orientation=\"y\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `geom_boxplot()`" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + 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": 20, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + geom_violin(x=-10, color=\"black\", alpha=0) \\\n", " + geom_violin(y=-3, color=\"black\", alpha=0, orientation=\"y\")" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 21, "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.7.13" } }, "nbformat": 4, "nbformat_minor": 4 }