{ "cells": [ { "cell_type": "markdown", "id": "06050c71", "metadata": {}, "source": [ "# Coordinate Systems\n", "\n", "The coordinate system determines how the x and y aesthetics combine to position elements in the plot. " ] }, { "cell_type": "code", "execution_count": 1, "id": "ded7e809", "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:19.591164Z", "iopub.status.busy": "2025-11-05T13:40:19.591065Z", "iopub.status.idle": "2025-11-05T13:40:19.619918Z", "shell.execute_reply": "2025-11-05T13:40:19.619702Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The geodata is provided by © OpenStreetMap contributors and is made available here under the Open Database License (ODbL).\n" ] } ], "source": [ "import pandas as pd\n", "\n", "from lets_plot import *\n", "from lets_plot.geo_data import *" ] }, { "cell_type": "code", "execution_count": 2, "id": "c65f3cbd-9028-4a42-8c09-daa980e4dc36", "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:19.620868Z", "iopub.status.busy": "2025-11-05T13:40:19.620772Z", "iopub.status.idle": "2025-11-05T13:40:19.622737Z", "shell.execute_reply": "2025-11-05T13:40:19.622568Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 3, "id": "f45fec18", "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:19.623356Z", "iopub.status.busy": "2025-11-05T13:40:19.623286Z", "iopub.status.idle": "2025-11-05T13:40:19.707294Z", "shell.execute_reply": "2025-11-05T13:40:19.707001Z" } }, "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": "code", "execution_count": 4, "id": "7ec3a720", "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:19.708055Z", "iopub.status.busy": "2025-11-05T13:40:19.707981Z", "iopub.status.idle": "2025-11-05T13:40:20.401552Z", "shell.execute_reply": "2025-11-05T13:40:20.401213Z" } }, "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", "
statefound namegeometry
0WalesWalesMULTIPOLYGON (((-5.35304 51.86591, -5.33064 51...
1ScotlandScotlandMULTIPOLYGON (((-8.61762 57.82752, -8.56173 57...
2EnglandEnglandMULTIPOLYGON (((-6.33758 49.94591, -6.34859 49...
3Northern IrelandNorthern IrelandMULTIPOLYGON (((-8.00601 54.54594, -7.85066 54...
\n", "
" ], "text/plain": [ " state found name \\\n", "0 Wales Wales \n", "1 Scotland Scotland \n", "2 England England \n", "3 Northern Ireland Northern Ireland \n", "\n", " geometry \n", "0 MULTIPOLYGON (((-5.35304 51.86591, -5.33064 51... \n", "1 MULTIPOLYGON (((-8.61762 57.82752, -8.56173 57... \n", "2 MULTIPOLYGON (((-6.33758 49.94591, -6.34859 49... \n", "3 MULTIPOLYGON (((-8.00601 54.54594, -7.85066 54... " ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "states_gdf = geocode_states().scope('UK').get_boundaries(6)\n", "states_gdf.head()" ] }, { "cell_type": "markdown", "id": "c521143d", "metadata": {}, "source": [ "## 1. Cartesian Coordinates\n", "\n", "The Cartesian coordinate system is the most familiar and common type of coordinate system. \n", "Setting limits on the coordinate system will zoom the plot like you’re looking at it with a magnifying glass. \n", "It does not change the underlying data as setting limits on a scale does.\n", "\n", "Typical use case: changing limits of axes." ] }, { "cell_type": "code", "execution_count": 5, "id": "a2bd521e", "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:20.402779Z", "iopub.status.busy": "2025-11-05T13:40:20.402696Z", "iopub.status.idle": "2025-11-05T13:40:20.413236Z", "shell.execute_reply": "2025-11-05T13:40:20.413046Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " ggplot(df, aes('cty', 'hwy')) + geom_point() + ggtitle('Default'),\n", " ggplot(df, aes('cty', 'hwy')) + geom_point() + \\\n", " ggtitle('With coord_cartesian()') + \\\n", " coord_cartesian(xlim=[10, 25], ylim=[10, 35]) \n", "])" ] }, { "cell_type": "markdown", "id": "92948a7e", "metadata": {}, "source": [ "## 2. Fixed Coordinates\n", "\n", "A fixed scale coordinate system forces a specified ratio between the physical representations of data units on the axes.\n", "\n", "Typical use case: stretching a plot along one of the axes." ] }, { "cell_type": "code", "execution_count": 6, "id": "84cb1010", "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:20.414330Z", "iopub.status.busy": "2025-11-05T13:40:20.414255Z", "iopub.status.idle": "2025-11-05T13:40:20.421606Z", "shell.execute_reply": "2025-11-05T13:40:20.421429Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " ggplot(df, aes('cty', 'hwy')) + geom_point() + ggtitle('Default'),\n", " ggplot(df, aes('cty', 'hwy')) + geom_point() + \\\n", " ggtitle('With coord_fixed()') + \\\n", " coord_fixed(ratio=1)\n", "])" ] }, { "cell_type": "markdown", "id": "81b559c3", "metadata": {}, "source": [ "Fixing coordinates could be combined with changing limits as for `coord_cartesian()`." ] }, { "cell_type": "code", "execution_count": 7, "id": "2851857d", "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:20.422507Z", "iopub.status.busy": "2025-11-05T13:40:20.422436Z", "iopub.status.idle": "2025-11-05T13:40:20.430166Z", "shell.execute_reply": "2025-11-05T13:40:20.429996Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " ggplot(df, aes('cty', 'hwy')) + geom_point() + \\\n", " coord_fixed(ratio=1) + ggtitle('Stretching only'),\n", " ggplot(df, aes('cty', 'hwy')) + geom_point() + \\\n", " ggtitle('With changing limits') + \\\n", " coord_fixed(ratio=1, xlim=[10, 30], ylim=[10, 40])\n", "])" ] }, { "cell_type": "markdown", "id": "cf991252", "metadata": {}, "source": [ "## 3. Flipping Coordinates\n", "\n", "You can flip axis of default coordinate system so that horizontal axis becomes vertical and vice versa." ] }, { "cell_type": "code", "execution_count": 8, "id": "25b84153", "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:20.431063Z", "iopub.status.busy": "2025-11-05T13:40:20.430994Z", "iopub.status.idle": "2025-11-05T13:40:20.438270Z", "shell.execute_reply": "2025-11-05T13:40:20.438081Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " ggplot(df, aes(x='fl')) + geom_bar() + ggtitle('Default'),\n", " ggplot(df, aes(x='fl')) + geom_bar() + \\\n", " ggtitle('With coord_flip()') + coord_flip()\n", "])" ] }, { "cell_type": "markdown", "id": "ad0fe8d9", "metadata": {}, "source": [ "Flipping could also be combined with changing limits." ] }, { "cell_type": "code", "execution_count": 9, "id": "05a7d75d", "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:20.439160Z", "iopub.status.busy": "2025-11-05T13:40:20.439091Z", "iopub.status.idle": "2025-11-05T13:40:20.447199Z", "shell.execute_reply": "2025-11-05T13:40:20.446837Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " ggplot(df, aes(x='fl')) + geom_bar() + \\\n", " coord_flip() + ggtitle('Flipping only'),\n", " ggplot(df, aes(x='fl')) + geom_bar() + \\\n", " coord_flip(ylim=[0, 300]) + ggtitle('With changing limits') + \\\n", " theme(plot_margin=[0, 5])\n", "])" ] }, { "cell_type": "markdown", "id": "b7415cdb", "metadata": {}, "source": [ "Also, you can add flip as an option for coordinate system." ] }, { "cell_type": "code", "execution_count": 10, "id": "be2a6e7b", "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:20.447981Z", "iopub.status.busy": "2025-11-05T13:40:20.447903Z", "iopub.status.idle": "2025-11-05T13:40:20.455020Z", "shell.execute_reply": "2025-11-05T13:40:20.454775Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " ggplot(df, aes(x='fl')) + geom_bar() + \\\n", " coord_flip() + ggtitle('Flipping via coord_flip()'),\n", " ggplot(df, aes(x='fl')) + geom_bar() + \\\n", " coord_cartesian(flip=True) + ggtitle('Flipping via flip=True')\n", "])" ] }, { "cell_type": "markdown", "id": "51be9b04-ceb5-443f-9d26-eeeb2a3539df", "metadata": {}, "source": [ "Tooltips after `coord_flip()` are flipped too." ] }, { "cell_type": "code", "execution_count": 11, "id": "7fbb53d8-e3c3-416e-855a-6d81726eb587", "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:20.455696Z", "iopub.status.busy": "2025-11-05T13:40:20.455623Z", "iopub.status.idle": "2025-11-05T13:40:20.465110Z", "shell.execute_reply": "2025-11-05T13:40:20.464855Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " ggplot(df) + geom_boxplot(aes(as_discrete('class',order=1, order_by='..middle..'), 'hwy')) + \\\n", " theme(axis_title_x='blank') + ggtitle('Default'),\n", " ggplot(df) + geom_boxplot(aes(as_discrete('class',order=1, order_by='..middle..'), 'hwy')) + \\\n", " theme(axis_title_y='blank') + coord_flip() + ggtitle('Flipped')\n", "])" ] }, { "cell_type": "markdown", "id": "82294706", "metadata": {}, "source": [ "## 4. Map Coordinates\n", "\n", "`coord_map()` projects a portion of the earth, which is approximately spherical, onto a flat 2D plane. Map projections generally do not preserve straight lines, so this requires considerable computation.\n", "\n", "Typical use case: geospatial data." ] }, { "cell_type": "code", "execution_count": 12, "id": "46cef4c3", "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:20.465803Z", "iopub.status.busy": "2025-11-05T13:40:20.465728Z", "iopub.status.idle": "2025-11-05T13:40:20.511314Z", "shell.execute_reply": "2025-11-05T13:40:20.511019Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " ggplot() + geom_polygon(aes(fill='state'), data=states_gdf) + \\\n", " ggtitle('Default'),\n", " ggplot() + geom_polygon(aes(fill='state'), data=states_gdf, tooltips='none') + \\\n", " coord_map() + ggtitle('With coord_map()')\n", "])" ] }, { "cell_type": "markdown", "id": "41a12861", "metadata": {}, "source": [ "Parameters `xlim`, `ylim`, `flip` are also available for `coord_map()`." ] }, { "cell_type": "markdown", "id": "956ab8ab-67df-4546-adcd-2fbb0d2ae1e6", "metadata": {}, "source": [ "## 5. Polar Coordinates\n", "\n", "Typical use case: pie charts and polar plots." ] }, { "cell_type": "markdown", "id": "4469d9e3-c688-4b23-8d06-5200bf0a6bcb", "metadata": {}, "source": [ "Let's set `theme_grey()` to improve plots readability." ] }, { "cell_type": "code", "execution_count": 13, "id": "8dac58d6-9e09-4738-a5cb-0e0d3a2654dd", "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:20.512116Z", "iopub.status.busy": "2025-11-05T13:40:20.512037Z", "iopub.status.idle": "2025-11-05T13:40:20.513410Z", "shell.execute_reply": "2025-11-05T13:40:20.513223Z" } }, "outputs": [], "source": [ "LetsPlot.set_theme(theme_grey())" ] }, { "cell_type": "markdown", "id": "fe5c7392-4f28-446b-bbb6-2adfa308c099", "metadata": {}, "source": [ "### 5.1. Pie Chart\n", "`geom_bar()` with `coord_polar()` is transformed to a pie chart. " ] }, { "cell_type": "code", "execution_count": 14, "id": "da028518-b14e-4da0-89a4-d11ec1c445e1", "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:20.514173Z", "iopub.status.busy": "2025-11-05T13:40:20.514103Z", "iopub.status.idle": "2025-11-05T13:40:20.522773Z", "shell.execute_reply": "2025-11-05T13:40:20.522592Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " ggplot(df) + geom_bar(aes(fill=as_discrete('cyl')), size=0) + ggtitle('From Bar Chart'),\n", " ggplot(df) + geom_bar(aes(fill=as_discrete('cyl')), size=0) + \\\n", " coord_polar(theta='y') + ggtitle('To Pie Chart')\n", "])" ] }, { "cell_type": "markdown", "id": "a4ae6a38-262a-4a67-96cc-815d39321de7", "metadata": {}, "source": [ "### 5.2. Various Bar Charts" ] }, { "cell_type": "code", "execution_count": 15, "id": "ad7ae97f-1f82-495e-85a2-3a2425d29fb1", "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:20.523418Z", "iopub.status.busy": "2025-11-05T13:40:20.523346Z", "iopub.status.idle": "2025-11-05T13:40:20.542024Z", "shell.execute_reply": "2025-11-05T13:40:20.541695Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bull_eye = ggplot(df) + \\\n", " geom_bar(aes(fill=as_discrete('cyl')), size=0, show_legend=False)\n", " \n", "radial_bar = ggplot(df) + \\\n", " geom_bar(aes(fill=as_discrete('cyl')), size=0, position='dodge', show_legend=False)\n", " \n", "coxcomb = ggplot(df) + \\\n", " geom_bar(aes(fill=as_discrete('cyl')), size=0, position='dodge', show_legend=False)\n", "\n", "gggrid([\n", " bull_eye + coord_polar() + ggtitle('Bulls eye'),\n", " radial_bar + coord_polar(theta='y') + ggtitle('Radial bar chart'),\n", " coxcomb + coord_polar() + ggtitle('Coxcomb plot'),\n", "\n", " bull_eye + ggtitle('was:'),\n", " radial_bar + ggtitle('was:'),\n", " coxcomb + ggtitle('was:'),\n", "], ncol=3)" ] }, { "cell_type": "markdown", "id": "58510e31-63be-49c5-b456-cd364d71ce16", "metadata": {}, "source": [ "### 5.3. Parameters" ] }, { "cell_type": "code", "execution_count": 16, "id": "c79c8808-2644-4c3b-825a-2a7b6f0797ce", "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:20.542783Z", "iopub.status.busy": "2025-11-05T13:40:20.542693Z", "iopub.status.idle": "2025-11-05T13:40:20.544803Z", "shell.execute_reply": "2025-11-05T13:40:20.544360Z" } }, "outputs": [], "source": [ "labels_df = {\n", " 'x': [0, 1, 2, 3, 4, 5, 6, 7, 8],\n", " 'y': [0, 45, 90, 135, 180, 225, 270, 315, 360],\n", " 'r_y': [360, 315, 270, 225, 180, 135, 90, 45, 0],\n", " 'l': ['l0', 'l45', 'l90', 'l135', 'l180', 'l225', 'l270', 'l315', 'l360'],\n", " 'g': ['g1', 'g1', 'g1', 'g2', 'g2', 'g2', 'g3', 'g3', 'g3']\n", "}" ] }, { "cell_type": "code", "execution_count": 17, "id": "bdb1a3d3-e224-47d0-a6d3-6fbfa46a1ff7", "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:20.545586Z", "iopub.status.busy": "2025-11-05T13:40:20.545465Z", "iopub.status.idle": "2025-11-05T13:40:20.549183Z", "shell.execute_reply": "2025-11-05T13:40:20.549010Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p1 = ggplot(labels_df, aes(x='x', y='y', color='y')) + \\\n", " geom_path(size=3, show_legend=False) + scale_color_brewer(palette='GnBu')\n", "\n", "p1 + coord_polar() + ggtitle('Default plot with coord_polar()')" ] }, { "cell_type": "markdown", "id": "cbc16b66-052a-47c4-9091-249865c0445e", "metadata": {}, "source": [ "#### `transform_bkgr`\n", "\n", "When using the `transform_bkgr` parameter, the panel is not transformed into a circle but remains a rectangle. " ] }, { "cell_type": "code", "execution_count": 18, "id": "5706ced0-58d3-4b2c-a149-428d0d649ba2", "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:20.549788Z", "iopub.status.busy": "2025-11-05T13:40:20.549717Z", "iopub.status.idle": "2025-11-05T13:40:20.552188Z", "shell.execute_reply": "2025-11-05T13:40:20.552020Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p1 + coord_polar(transform_bkgr=False) + ggtitle('coord_polar(transform_bkgr=False)') " ] }, { "cell_type": "markdown", "id": "2b2a0df5-590d-4f3c-bac2-9621ff8aa520", "metadata": {}, "source": [ "#### `direction`\n", "\n", "You can specify angle direction: 1 = clockwise, -1 = counter-clockwise." ] }, { "cell_type": "code", "execution_count": 19, "id": "78a74b28-814f-4e41-82ad-f98d6062f2e3", "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:20.552784Z", "iopub.status.busy": "2025-11-05T13:40:20.552713Z", "iopub.status.idle": "2025-11-05T13:40:20.555234Z", "shell.execute_reply": "2025-11-05T13:40:20.555062Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p1 + coord_polar(direction=-1) + ggtitle('coord_polar(direction=-1)')" ] }, { "cell_type": "markdown", "id": "a3c1ee0b-ce39-40c4-93f6-1e92d5e4f4cb", "metadata": {}, "source": [ "#### `start`\n", "\n", "This parameter specifies the offset relative to the starting angle (12 o'clock) in radians." ] }, { "cell_type": "code", "execution_count": 20, "id": "9e2223cc-ff70-4db5-8c0e-884c569862d9", "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:20.555851Z", "iopub.status.busy": "2025-11-05T13:40:20.555783Z", "iopub.status.idle": "2025-11-05T13:40:20.559092Z", "shell.execute_reply": "2025-11-05T13:40:20.558923Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " p1 + coord_polar(start=3.14 / 2) + ggtitle('start=PI/2'),\n", " p1 + coord_polar(start=-3.14 / 2) + ggtitle('start=-PI/2')\n", "])" ] }, { "cell_type": "markdown", "id": "613a3063-b9e7-4878-b81e-33c9cad8e951", "metadata": {}, "source": [ "#### `theta`\n", "\n", "The `theta` parameter allows to select aesthetic used to map angle.\n", "\n", "Note. Use the `flat` parameter in path-based geoms to construct a radar plot." ] }, { "cell_type": "code", "execution_count": 21, "id": "daa5ca81-7673-40d9-ad89-1bc8b1596170", "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:20.559712Z", "iopub.status.busy": "2025-11-05T13:40:20.559640Z", "iopub.status.idle": "2025-11-05T13:40:20.563521Z", "shell.execute_reply": "2025-11-05T13:40:20.563351Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data1 = {\n", " 'subj': ['progr', 'math', 'physic', 'chemistry', 'biology'],\n", " 'subj_id': [1, 2, 3, 4, 5],\n", " 'student': ['John'] * 5,\n", " 'score': [19, 15, 18, 12, 9]\n", "}\n", "\n", "p2 = ggplot(data1) + \\\n", " geom_path(aes(x='subj', y='score', color='student'), flat=True) + \\\n", " geom_point(aes(x='subj', y='score', color='student')) \n", "\n", "gggrid([\n", " p2 + coord_polar(theta='x'),\n", " p2 + coord_polar(theta='y')\n", "])" ] }, { "cell_type": "markdown", "id": "cea7242f-87f0-4a47-9b75-1e4c455c3bf8", "metadata": {}, "source": [ "#### `xlim` and `ylim`\n", "\n", "The `xlim` parameter can be used to fix overlapping between first and last value. \n", "The `ylim` parameter can be used to move data out of the center. \n", "\n", "To prevent `biology` and `progr` overlapping let's change the `xlim` to `[None, 5]` (`None` is to keep the default minimum limit as this value is not important to us). \n", "To show the full score range we change the `ylim` to `[1, 20]`." ] }, { "cell_type": "code", "execution_count": 22, "id": "d8192601-e9ed-4a60-b9ba-ada472d9514f", "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:20.564188Z", "iopub.status.busy": "2025-11-05T13:40:20.564119Z", "iopub.status.idle": "2025-11-05T13:40:20.567573Z", "shell.execute_reply": "2025-11-05T13:40:20.567393Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " p2 + coord_polar(),\n", " p2 + coord_polar(xlim=[None, 5], ylim=[1, 20])\n", "]) " ] }, { "cell_type": "markdown", "id": "14274f7a-2162-4993-93cb-8b24b30709e2", "metadata": {}, "source": [ "### 5.4. Theming\n", "\n", "The polar coordinate system supports axis theme options, incorporates specialized logic for the theme's `panel_inset` parameter, and offers its own `transform_bkgr` option to manage plot panel transformation:" ] }, { "cell_type": "code", "execution_count": 23, "id": "58c98aaa-6d50-472d-98f9-eca7e84aa443", "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:20.568252Z", "iopub.status.busy": "2025-11-05T13:40:20.568180Z", "iopub.status.idle": "2025-11-05T13:40:20.575106Z", "shell.execute_reply": "2025-11-05T13:40:20.574923Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(df) + \\\n", " geom_bar(aes(x='model', y='cty', fill='cty'), stat='identity', position='dodge') + \\\n", " scale_fill_gradient(low='red', high='white', limits=(5, 40)) + \\\n", " theme(axis_line_y=element_line(color='red', size=2),\n", " axis_line_x=element_line(color='blue', size=2),\n", " axis_ticks_length_y=5,\n", " axis_ticks_length_x=10,\n", " axis_ticks_y=element_line(size=5, color='red'),\n", " axis_ticks_x=element_line(size=3, color='blue'),\n", " axis_text_x=element_text(color='blue', angle=10, vjust=.7, hjust=.4),\n", " axis_text_y=element_text(color='red'),\n", " panel_inset=[20, 140, 30, 135]) + \\\n", " ggsize(900, 500) + \\\n", " coord_polar(transform_bkgr=False)" ] } ], "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 }