{ "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", " | species | \n", "variable | \n", "value | \n", "units | \n", "rescaled_value | \n", "rescaled_value_pct | \n", "
---|---|---|---|---|---|---|
0 | \n", "Adelie | \n", "avg. bill length | \n", "38.823973 | \n", "mm | \n", "0.000000 | \n", "0 | \n", "
1 | \n", "Adelie | \n", "avg. bill depth | \n", "18.347260 | \n", "mm | \n", "0.978584 | \n", "97 | \n", "
2 | \n", "Adelie | \n", "avg. flipper length | \n", "190.102740 | \n", "mm | \n", "0.000000 | \n", "0 | \n", "
3 | \n", "Adelie | \n", "avg. body mass | \n", "3.706164 | \n", "kg | \n", "0.000000 | \n", "0 | \n", "