{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Customizing the Legend" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2024-08-23T10:51:21.864125Z", "iopub.status.busy": "2024-08-23T10:51:21.862937Z", "iopub.status.idle": "2024-08-23T10:51:24.584759Z", "shell.execute_reply": "2024-08-23T10:51:24.584487Z" } }, "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, "metadata": { "execution": { "iopub.execute_input": "2024-08-23T10:51:24.586627Z", "iopub.status.busy": "2024-08-23T10:51:24.586382Z", "iopub.status.idle": "2024-08-23T10:51:25.131523Z", "shell.execute_reply": "2024-08-23T10:51:25.131634Z" } }, "outputs": [ { "data": { "application/kotlindataframe+json": "{\"nrow\":5,\"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\"},{\"untitled\":4,\"manufacturer\":\"audi\",\"model\":\"a4\",\"displ\":2.0,\"year\":2008,\"cyl\":4,\"trans\":\"auto(av)\",\"drv\":\"f\",\"cty\":21,\"hwy\":30,\"fl\":\"p\",\"class\":\"compact\"},{\"untitled\":5,\"manufacturer\":\"audi\",\"model\":\"a4\",\"displ\":2.8,\"year\":1999,\"cyl\":6,\"trans\":\"auto(l5)\",\"drv\":\"f\",\"cty\":16,\"hwy\":26,\"fl\":\"p\",\"class\":\"compact\"}]}", "text/html": [ " \n", " \n", " \n", " \n", " \n", "
\n", "\n", "

DataFrame: rowsCount = 5, columnsCount = 12

\n", "
untitledmanufacturermodeldisplyearcyltransdrvctyhwyflclass
1audia41.80000019994auto(l5)f1829pcompact
2audia41.80000019994manual(m5)f2129pcompact
3audia42.00000020084manual(m6)f2031pcompact
4audia42.00000020084auto(av)f2130pcompact
5audia42.80000019996auto(l5)f1626pcompact
\n", " \n", " \n", " " ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "val df = DataFrame.readCSV(\"https://raw.githubusercontent.com/JetBrains/lets-plot-kotlin/master/docs/examples/data/mpg.csv\")\n", "df.head()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2024-08-23T10:51:25.133241Z", "iopub.status.busy": "2024-08-23T10:51:25.132986Z", "iopub.status.idle": "2024-08-23T10:51:25.158566Z", "shell.execute_reply": "2024-08-23T10:51:25.158265Z" } }, "outputs": [], "source": [ "val mpg = df.toMap()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Scale Parameters Related to the Legend" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2024-08-23T10:51:25.160471Z", "iopub.status.busy": "2024-08-23T10:51:25.160124Z", "iopub.status.idle": "2024-08-23T10:51:25.302750Z", "shell.execute_reply": "2024-08-23T10:51:25.302924Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Default legend\n", "val p1 = letsPlot(mpg) { x = \"cty\"; y = \"hwy\" } + geomPoint() { color = \"cyl\" }\n", "p1" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2024-08-23T10:51:25.304592Z", "iopub.status.busy": "2024-08-23T10:51:25.304349Z", "iopub.status.idle": "2024-08-23T10:51:25.352567Z", "shell.execute_reply": "2024-08-23T10:51:25.352677Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Legend name\n", "p1 + scaleColorContinuous(name = \"Number of cylinders\")" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2024-08-23T10:51:25.354086Z", "iopub.status.busy": "2024-08-23T10:51:25.353814Z", "iopub.status.idle": "2024-08-23T10:51:25.410144Z", "shell.execute_reply": "2024-08-23T10:51:25.410317Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Breaks and labels\n", "p1 + scaleColorContinuous(breaks = listOf(4, 5, 6, 8), labels = listOf(\"four\", \"five\", \"six\", \"eight\"))" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "execution": { "iopub.execute_input": "2024-08-23T10:51:25.411919Z", "iopub.status.busy": "2024-08-23T10:51:25.411670Z", "iopub.status.idle": "2024-08-23T10:51:25.459271Z", "shell.execute_reply": "2024-08-23T10:51:25.459444Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Format labels\n", "p1 + scaleColorContinuous(format = \"{} cylinders\\nin the vehicle’s engine\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Guides" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `guideLegend()`" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "execution": { "iopub.execute_input": "2024-08-23T10:51:25.460894Z", "iopub.status.busy": "2024-08-23T10:51:25.460658Z", "iopub.status.idle": "2024-08-23T10:51:25.524902Z", "shell.execute_reply": "2024-08-23T10:51:25.525118Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Default categorical legend\n", "val p2 = letsPlot(mpg) { x = \"displ\"; y = \"hwy\"; color = \"manufacturer\" } + geomPoint(size = 5)\n", "p2" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "execution": { "iopub.execute_input": "2024-08-23T10:51:25.526776Z", "iopub.status.busy": "2024-08-23T10:51:25.526475Z", "iopub.status.idle": "2024-08-23T10:51:25.582168Z", "shell.execute_reply": "2024-08-23T10:51:25.582341Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Legend name\n", "p2 + scaleColorDiscrete(guide = guideLegend(\"Company\"))" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "execution": { "iopub.execute_input": "2024-08-23T10:51:25.583682Z", "iopub.status.busy": "2024-08-23T10:51:25.583379Z", "iopub.status.idle": "2024-08-23T10:51:25.632286Z", "shell.execute_reply": "2024-08-23T10:51:25.632391Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Layout the legend in two columns\n", "p2 + scaleColorDiscrete(guide = guideLegend(ncol = 2))" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "execution": { "iopub.execute_input": "2024-08-23T10:51:25.633345Z", "iopub.status.busy": "2024-08-23T10:51:25.633206Z", "iopub.status.idle": "2024-08-23T10:51:25.681629Z", "shell.execute_reply": "2024-08-23T10:51:25.681802Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Fill by rows\n", "p2 + scaleColorDiscrete(guide = guideLegend(ncol = 2, byRow = true))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `guideColorbar()`" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "execution": { "iopub.execute_input": "2024-08-23T10:51:25.683600Z", "iopub.status.busy": "2024-08-23T10:51:25.683356Z", "iopub.status.idle": "2024-08-23T10:51:25.740633Z", "shell.execute_reply": "2024-08-23T10:51:25.740438Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Default color legend\n", "val p3 = letsPlot(mpg) { x = \"displ\"; y = \"hwy\" } + geomPoint() { color = \"cty\" }\n", "p3" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "execution": { "iopub.execute_input": "2024-08-23T10:51:25.742292Z", "iopub.status.busy": "2024-08-23T10:51:25.742047Z", "iopub.status.idle": "2024-08-23T10:51:25.789434Z", "shell.execute_reply": "2024-08-23T10:51:25.789607Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Legend name\n", "p3 + scaleColorContinuous(guide = guideColorbar(\"City mileage\"))" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "execution": { "iopub.execute_input": "2024-08-23T10:51:25.791455Z", "iopub.status.busy": "2024-08-23T10:51:25.791169Z", "iopub.status.idle": "2024-08-23T10:51:25.836868Z", "shell.execute_reply": "2024-08-23T10:51:25.836650Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Adjust colorbar size\n", "p3 + scaleColorContinuous(guide = guideColorbar(barWidth = 10, barHeight = 200))" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "execution": { "iopub.execute_input": "2024-08-23T10:51:25.838268Z", "iopub.status.busy": "2024-08-23T10:51:25.838023Z", "iopub.status.idle": "2024-08-23T10:51:25.886437Z", "shell.execute_reply": "2024-08-23T10:51:25.886610Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Fewer bins\n", "p3 + scaleColorContinuous(breaks = listOf(13, 22, 31), guide = guideColorbar(nbin = 3))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `guides()`" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "execution": { "iopub.execute_input": "2024-08-23T10:51:25.887893Z", "iopub.status.busy": "2024-08-23T10:51:25.887733Z", "iopub.status.idle": "2024-08-23T10:51:25.944470Z", "shell.execute_reply": "2024-08-23T10:51:25.944648Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Default complex legend\n", "val p4 = letsPlot(mpg) { x = \"displ\"; y = \"hwy\" } + geomPoint(size = 5) { color = \"cty\"; shape = \"drv\" }\n", "p4" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "execution": { "iopub.execute_input": "2024-08-23T10:51:25.946032Z", "iopub.status.busy": "2024-08-23T10:51:25.945786Z", "iopub.status.idle": "2024-08-23T10:51:25.989958Z", "shell.execute_reply": "2024-08-23T10:51:25.990143Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Guides for 'color' and 'shape' aesthetics\n", "p4 + guides(color = guideColorbar(barWidth = 10), shape = guideLegend(ncol = 2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Customizing Legend Appearance" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "execution": { "iopub.execute_input": "2024-08-23T10:51:25.991491Z", "iopub.status.busy": "2024-08-23T10:51:25.991251Z", "iopub.status.idle": "2024-08-23T10:51:26.032210Z", "shell.execute_reply": "2024-08-23T10:51:26.032000Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p4 + guides(shape = guideLegend(size = 8, color = \"#f03b20\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Legend Position" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "execution": { "iopub.execute_input": "2024-08-23T10:51:26.033644Z", "iopub.status.busy": "2024-08-23T10:51:26.033396Z", "iopub.status.idle": "2024-08-23T10:51:26.080070Z", "shell.execute_reply": "2024-08-23T10:51:26.079852Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Draw legend below the plot area\n", "p4 + theme().legendPositionBottom()" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "execution": { "iopub.execute_input": "2024-08-23T10:51:26.081636Z", "iopub.status.busy": "2024-08-23T10:51:26.081394Z", "iopub.status.idle": "2024-08-23T10:51:26.124810Z", "shell.execute_reply": "2024-08-23T10:51:26.124675Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Specify position, justification and orientation\n", "p4 + theme().legendPosition(1, 1).legendJustification(1, 1).legendDirectionHorizontal()" ] } ], "metadata": { "kernelspec": { "display_name": "Kotlin", "language": "kotlin", "name": "kotlin" }, "language_info": { "codemirror_mode": "text/x-kotlin", "file_extension": ".kt", "mimetype": "text/x-kotlin", "name": "kotlin", "nbconvert_exporter": "", "pygments_lexer": "kotlin", "version": "1.9.23" } }, "nbformat": 4, "nbformat_minor": 4 }