{ "cells": [ { "cell_type": "markdown", "id": "2a51cb88-6706-4266-a2c0-794dad6ef810", "metadata": {}, "source": [ "# Customizing `geom_pie()` with `start` and `direction` Parameters\n", "Two new parameters have been added to the `geom_pie()` function:\n", "\n", "- `start`: specifies the starting angle of the first slice in degrees (0-360°)\n", "- `direction`: controls sector layout direction (1 for clockwise or -1 for counterclockwise)\n", "\n", "Previously, pie charts were limited by fixed positioning where the second slice always started at 0° and all slices were arranged clockwise. \n", "These new parameters provide precise control over slice positioning and orientation. " ] }, { "cell_type": "code", "execution_count": 1, "id": "83168fbc-3b0d-4899-998b-5339d4edc47f", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "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", "
CountryGDP_2025_Trillion_USD
0United States30.34
1China19.53
2Germany4.92
\n", "
" ], "text/plain": [ " Country GDP_2025_Trillion_USD\n", "0 United States 30.34\n", "1 China 19.53\n", "2 Germany 4.92" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from lets_plot import *\n", "import pandas as pd\n", "\n", "LetsPlot.setup_html()\n", "\n", "data = pd.read_csv(\"https://raw.githubusercontent.com/JetBrains/lets-plot/refs/heads/master/docs/f-25a/data/gdp_forecast_2025_trillion_usd.csv\", encoding ='utf-8')\n", "data.head(3)\n" ] }, { "cell_type": "code", "execution_count": 2, "id": "9005245d-7b76-4c98-a560-1cc124b3d75e", "metadata": {}, "outputs": [], "source": [ "p = ggplot(data, aes(fill='Country', slice='GDP_2025_Trillion_USD')) \\\n", " + ggtitle('GDP forecast 2025 (trillion US$) by country', \n", " subtitle='Source: Wikipedia') \\\n", " + scale_fill_gradient(low=\"blue\", high=\"yellow\") \\\n", " + theme_void() + theme(plot_title=element_text(hjust=0.5), plot_subtitle=element_text(hjust=0.5))\n" ] }, { "cell_type": "markdown", "id": "a7e003fa-b531-498a-abdc-f80aaf1d85ca", "metadata": {}, "source": [ "#### 1. Auto-layout\n", "\n", "By default, the first sector is positioned counterclockwise from the start point (12 o’clock), while the remaining sectors are arranged clockwise." ] }, { "cell_type": "code", "execution_count": 3, "id": "d97a17aa-aaa6-4c00-b52e-3d728f12d7b3", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + geom_pie(size=.6, size_unit='x', stat='identity')\n" ] }, { "cell_type": "markdown", "id": "4382ac18-b018-4ed5-97ea-32fa2c7c0a37", "metadata": {}, "source": [ "#### 2. `direction`\n", "Use `1` for clockwise (default) or `-1` for counterclockwise." ] }, { "cell_type": "code", "execution_count": 4, "id": "10042f4d-0ff8-44e6-a287-baa77c8c3088", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + geom_pie(size=.6, size_unit='x', stat='identity',\n", " direction=-1) # <-- counterclockwise\n" ] }, { "cell_type": "markdown", "id": "e59fde8d-9125-4026-aaca-cd883ece4f22", "metadata": {}, "source": [ "#### 2. `start`\n", "Specifies the starting angle of the first slice in degrees." ] }, { "cell_type": "code", "execution_count": 5, "id": "2070d067-21a6-4d22-87d4-26fff9c700ec", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " p + geom_pie(size=.9, size_unit='x', stat='identity') + ggtitle('Auto'),\n", " p + geom_pie(size=.9, size_unit='x', start=0, stat='identity') + ggtitle('start=0'),\n", " p + geom_pie(size=.9, size_unit='x', start=180, stat='identity') + ggtitle('start=180')\n", "])" ] } ], "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.9.21" } }, "nbformat": 4, "nbformat_minor": 5 }