{ "cells": [ { "cell_type": "markdown", "id": "9198ba07-3560-4e2a-a448-bbca65083507", "metadata": {}, "source": [ "# Custom Legend with `manualKey` Option\n", "\n", "In Lets-Plot-Kotlin, 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 new `manualKey` option in plot layer addresses this limitation by allowing you to create custom legend entries specific to individual plot layers.\n", "\n", "The `manualKey` option can be used in two ways:\n", "\n", "- Simple case: pass a string to serve as the label for the legend entry.\n", "- Advanced case: use the `layerKey()` 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", " - `alpha`, `color`, `fill`, `shape`, `size`, `width`, `height`, `linetype`, `stroke` - aesthetic parameters to be applied in the legend" ] }, { "cell_type": "code", "execution_count": 1, "id": "bf802200-d055-4c58-97e6-1eb7fab93cb6", "metadata": { "execution": { "iopub.execute_input": "2024-08-21T15:17:04.814381Z", "iopub.status.busy": "2024-08-21T15:17:04.813173Z", "iopub.status.idle": "2024-08-21T15:17:07.618092Z", "shell.execute_reply": "2024-08-21T15:17:07.617799Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ " \n", " " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%useLatestDescriptors\n", "%use lets-plot\n", "%use dataframe" ] }, { "cell_type": "code", "execution_count": 2, "id": "de365bce-3ff1-4f16-a21f-021f46c21681", "metadata": { "execution": { "iopub.execute_input": "2024-08-21T15:17:07.619843Z", "iopub.status.busy": "2024-08-21T15:17:07.619566Z", "iopub.status.idle": "2024-08-21T15:17:07.652479Z", "shell.execute_reply": "2024-08-21T15:17:07.652322Z" } }, "outputs": [ { "data": { "text/plain": [ "Lets-Plot Kotlin API v.4.8.0. Frontend: Notebook with dynamically loaded JS. Lets-Plot JS v.4.4.0." ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "LetsPlot.getInfo()" ] }, { "cell_type": "code", "execution_count": 3, "id": "c601089f-7889-4db7-b297-e01c63cf1a9a", "metadata": { "execution": { "iopub.execute_input": "2024-08-21T15:17:07.653941Z", "iopub.status.busy": "2024-08-21T15:17:07.653782Z", "iopub.status.idle": "2024-08-21T15:17:08.211059Z", "shell.execute_reply": "2024-08-21T15:17:08.210559Z" } }, "outputs": [ { "data": { "application/kotlindataframe+json": "{\"nrow\":3,\"ncol\":12,\"columns\":[\"untitled\",\"manufacturer\",\"model\",\"displ\",\"year\",\"cyl\",\"trans\",\"drv\",\"cty\",\"hwy\",\"fl\",\"class\"],\"kotlin_dataframe\":[{\"untitled\":1,\"manufacturer\":\"audi\",\"model\":\"a4\",\"displ\":1.8,\"year\":1999,\"cyl\":4,\"trans\":\"auto(l5)\",\"drv\":\"f\",\"cty\":18,\"hwy\":29,\"fl\":\"p\",\"class\":\"compact\"},{\"untitled\":2,\"manufacturer\":\"audi\",\"model\":\"a4\",\"displ\":1.8,\"year\":1999,\"cyl\":4,\"trans\":\"manual(m5)\",\"drv\":\"f\",\"cty\":21,\"hwy\":29,\"fl\":\"p\",\"class\":\"compact\"},{\"untitled\":3,\"manufacturer\":\"audi\",\"model\":\"a4\",\"displ\":2.0,\"year\":2008,\"cyl\":4,\"trans\":\"manual(m6)\",\"drv\":\"f\",\"cty\":20,\"hwy\":31,\"fl\":\"p\",\"class\":\"compact\"}]}", "text/html": [ " \n", " \n", " \n", " \n", " \n", "DataFrame: rowsCount = 3, columnsCount = 12
\n", "untitled | manufacturer | model | displ | year | cyl | trans | drv | cty | hwy | fl | class |
---|---|---|---|---|---|---|---|---|---|---|---|
1 | audi | a4 | 1.800000 | 1999 | 4 | auto(l5) | f | 18 | 29 | p | compact |
2 | audi | a4 | 1.800000 | 1999 | 4 | manual(m5) | f | 21 | 29 | p | compact |
3 | audi | a4 | 2.000000 | 2008 | 4 | manual(m6) | f | 20 | 31 | p | compact |