{ "cells": [ { "cell_type": "markdown", "id": "6025439a", "metadata": {}, "source": [ "# Custom Legend with `manual_key` Option\n", "\n", "In Lets-Plot, as in ggplot2, legends are automatically generated based on the aesthetic mappings in the plot. \n", "\n", "Sometimes, however, this automatic generation doesn't provide the precise control needed for complex visualizations. \n", "\n", "The `manual_key` option allows you to create custom legend entries specific to individual plot layers.\n", "\n", "The `manual_key` option can be used in two ways:\n", "\n", "- Simple case: pass a string to serve as the label for the legend entry.\n", "\n", "- Advanced case: use the `layer_key()` function for more detailed customization:\n", "\n", " - `label` - text for the legend element\n", " - `group` - key used to group elements in the legend\n", " - `index` - position of the element within its legend group\n", " - `kwargs` - dictionary of aesthetic parameters to be applied in the legend" ] }, { "cell_type": "code", "execution_count": 1, "id": "a68e6d43-375d-40cb-9926-905fbca7c062", "metadata": { "execution": { "iopub.execute_input": "2024-11-01T20:18:28.467089Z", "iopub.status.busy": "2024-11-01T20:18:28.467014Z", "iopub.status.idle": "2024-11-01T20:18:28.816558Z", "shell.execute_reply": "2024-11-01T20:18:28.816117Z" } }, "outputs": [], "source": [ "import pandas as pd\n", "\n", "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "id": "691ff64d-5dd3-4aad-8da9-d92a3f83eaa6", "metadata": { "execution": { "iopub.execute_input": "2024-11-01T20:18:28.818049Z", "iopub.status.busy": "2024-11-01T20:18:28.817929Z", "iopub.status.idle": "2024-11-01T20:18:28.820412Z", "shell.execute_reply": "2024-11-01T20:18:28.820153Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 3, "id": "aab8ca98-c81e-4d5a-b968-cb149f771a99", "metadata": { "execution": { "iopub.execute_input": "2024-11-01T20:18:28.821416Z", "iopub.status.busy": "2024-11-01T20:18:28.821345Z", "iopub.status.idle": "2024-11-01T20:18:28.969205Z", "shell.execute_reply": "2024-11-01T20:18:28.968930Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(234, 12)\n" ] }, { "data": { "text/html": [ "\n", " | Unnamed: 0 | \n", "manufacturer | \n", "model | \n", "displ | \n", "year | \n", "cyl | \n", "trans | \n", "drv | \n", "cty | \n", "hwy | \n", "fl | \n", "class | \n", "
---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | \n", "1 | \n", "audi | \n", "a4 | \n", "1.8 | \n", "1999 | \n", "4 | \n", "auto(l5) | \n", "f | \n", "18 | \n", "29 | \n", "p | \n", "compact | \n", "
1 | \n", "2 | \n", "audi | \n", "a4 | \n", "1.8 | \n", "1999 | \n", "4 | \n", "manual(m5) | \n", "f | \n", "21 | \n", "29 | \n", "p | \n", "compact | \n", "
2 | \n", "3 | \n", "audi | \n", "a4 | \n", "2.0 | \n", "2008 | \n", "4 | \n", "manual(m6) | \n", "f | \n", "20 | \n", "31 | \n", "p | \n", "compact | \n", "