{ "cells": [ { "cell_type": "markdown", "id": "901b134b-7510-4d9e-ae95-a01a4c33274e", "metadata": {}, "source": [ "# Palmer Penguins\n", "\n", "This notebook is inspired by an example [Radar chart with ggradar](https://r-graph-gallery.com/web-radar-chart-with-R.html)." ] }, { "cell_type": "code", "execution_count": 1, "id": "09f8f3a0-478a-4f0a-945d-8354860919b6", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:19:42.160207Z", "iopub.status.busy": "2024-04-26T12:19:42.160207Z", "iopub.status.idle": "2024-04-26T12:19:43.055517Z", "shell.execute_reply": "2024-04-26T12:19:43.055517Z" } }, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "\n", "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "id": "f67a85a2-c3a9-4009-a5bf-0feea37e6658", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:19:43.055517Z", "iopub.status.busy": "2024-04-26T12:19:43.055517Z", "iopub.status.idle": "2024-04-26T12:19:43.071589Z", "shell.execute_reply": "2024-04-26T12:19:43.071589Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 3, "id": "93b26d2e-123d-40b5-af22-c48464271312", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:19:43.071589Z", "iopub.status.busy": "2024-04-26T12:19:43.071589Z", "iopub.status.idle": "2024-04-26T12:19:43.087260Z", "shell.execute_reply": "2024-04-26T12:19:43.087260Z" } }, "outputs": [], "source": [ "def rescale_in_group_df(df, value_col, group_col, rescaled_col=None):\n", " if rescaled_col is None:\n", " rescaled_col = \"rescaled_{0}\".format(value_col)\n", " def rescale_subdf(subdf):\n", " rescaler = lambda s: (s - s.min()) / (s.max() - s.min())\n", " return subdf.assign(**{rescaled_col: rescaler(subdf[value_col])})\n", " return pd.concat([rescale_subdf(df[df[group_col] == g]) for g in df[group_col].unique()])\n", "\n", "def get_data():\n", " df = pd.read_csv(\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/penguins.csv\")\n", "\n", " grouped_df = df.dropna().groupby(\"species\")[[\"bill_length_mm\", \"bill_depth_mm\", \"flipper_length_mm\", \"body_mass_g\"]].mean().reset_index()\n", " grouped_df.columns = [\"species\", \"avg. bill length\", \"avg. bill depth\", \"avg. flipper length\", \"avg. body mass\"]\n", "\n", " melted_df = pd.melt(grouped_df, id_vars=\"species\")\n", " avg_body_mass_filter = melted_df[\"variable\"] == \"avg. body mass\"\n", " melted_df.loc[avg_body_mass_filter, \"value\"] /= 1000\n", " melted_df = melted_df.assign(units=np.where(avg_body_mass_filter, \"kg\", \"mm\"))\n", "\n", " rescaled_df = rescale_in_group_df(melted_df, \"value\", \"variable\")\n", " rescaled_df.loc[:, \"rescaled_value_pct\"] = (100 * rescaled_df[\"rescaled_value\"]).astype(int)\n", " return rescaled_df.sort_values(by=\"species\").reset_index(drop=True)" ] }, { "cell_type": "code", "execution_count": 4, "id": "7c4336eb-7356-4830-ab61-4c233815f071", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:19:43.087260Z", "iopub.status.busy": "2024-04-26T12:19:43.087260Z", "iopub.status.idle": "2024-04-26T12:19:43.405669Z", "shell.execute_reply": "2024-04-26T12:19:43.405669Z" } }, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
speciesvariablevalueunitsrescaled_valuerescaled_value_pct
0Adelieavg. bill length38.823973mm0.0000000
1Adelieavg. bill depth18.347260mm0.97858497
2Adelieavg. flipper length190.102740mm0.0000000
3Adelieavg. body mass3.706164kg0.0000000
\n", "
" ], "text/plain": [ " species variable value units rescaled_value \\\n", "0 Adelie avg. bill length 38.823973 mm 0.000000 \n", "1 Adelie avg. bill depth 18.347260 mm 0.978584 \n", "2 Adelie avg. flipper length 190.102740 mm 0.000000 \n", "3 Adelie avg. body mass 3.706164 kg 0.000000 \n", "\n", " rescaled_value_pct \n", "0 0 \n", "1 97 \n", "2 0 \n", "3 0 " ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "penguins_df = get_data()\n", "penguins_df.head(4)" ] }, { "cell_type": "code", "execution_count": 5, "id": "3145b502-08cb-4395-81f0-581746adecf3", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:19:43.405669Z", "iopub.status.busy": "2024-04-26T12:19:43.405669Z", "iopub.status.idle": "2024-04-26T12:19:43.421374Z", "shell.execute_reply": "2024-04-26T12:19:43.421374Z" } }, "outputs": [], "source": [ "font_family = \"roboto\"\n", "axis_color = \"lightgray\"\n", "axis_text_data = {\"x\": [\"avg. bill length\"]*3, \\\n", " \"y\": [0, 50, 100], \\\n", " \"text\": [\"0%\", \"50%\", \"100%\"]}\n", "\n", "penguins_colors = {\"Adelie\": \"#ff5a5f\", \"Chinstrap\": \"#ffb400\", \"Gentoo\": \"#007a87\"}\n", "penguins_tooltips = layer_tooltips().title(\"@species\")\\\n", " .line(\"@variable (@units): @value\").format(\"@value\", \".2~f\")\n", "penguins_theme = theme(\n", " text=element_text(family=font_family, size=18),\n", " plot_title=element_text(size=28, hjust=.5, face='bold'),\n", " axis_title='blank', axis_text_y='blank', axis_line_x='blank', \\\n", " axis_ticks=element_line(color=axis_color), \\\n", " panel_grid=element_line(color=axis_color), \\\n", " panel_inset=[0, 80], \\\n", " legend_position=[1, 0], legend_justification=[1, 0],\n", " tooltip=element_rect(),\n", " axis_tooltip='blank',\n", ")" ] }, { "cell_type": "code", "execution_count": 6, "id": "1cef4685-588c-4c5d-bf3a-576f9933cda9", "metadata": { "execution": { "iopub.execute_input": "2024-04-26T12:19:43.421374Z", "iopub.status.busy": "2024-04-26T12:19:43.421374Z", "iopub.status.idle": "2024-04-26T12:19:43.533166Z", "shell.execute_reply": "2024-04-26T12:19:43.531172Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(penguins_df) + \\\n", " geom_area(aes(\"variable\", \"rescaled_value_pct\", paint_a=\"species\"), position='identity', flat=True, \\\n", " size=2.5, color_by='paint_a', fill_by='paint_a', alpha=.2) + \\\n", " geom_point(aes(\"variable\", \"rescaled_value_pct\", paint_a=\"species\"), size=6, color_by='paint_a', tooltips=penguins_tooltips) + \\\n", " geom_text(aes(\"x\", \"y\", label=\"text\"), data=axis_text_data, hjust=1, fontface='bold', family=font_family, size=10) + \\\n", " scale_x_discrete() + \\\n", " scale_manual('paint_a', name='', values=penguins_colors) + \\\n", " coord_polar(ylim=[-15, 100]) + \\\n", " ggsize(800, 600) + \\\n", " ggtitle(\"Penguins species\") + \\\n", " penguins_theme + flavor_solarized_light()" ] } ], "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 }