{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "f3f5cd41", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import numpy as np\n", "import pandas as pd\n", "\n", "from lets_plot.mapping import as_discrete\n", "from lets_plot import *\n", "\n", "LetsPlot.setup_html()" ] }, { "cell_type": "markdown", "id": "15508d12-1922-4c06-b9c4-6643387922a8", "metadata": {}, "source": [ "# override_aes in guide_legend()ΒΆ\n", "The `override_aes` parameter can be used to specify aesthetic parameters of legend keys" ] }, { "cell_type": "markdown", "id": "a3031c6a-3ef7-4ca2-bd5c-912c9ac4dfe3", "metadata": {}, "source": [ "## Demo" ] }, { "cell_type": "code", "execution_count": 2, "id": "b0516089-88bf-4a33-acfb-fab6ecefee14", "metadata": {}, "outputs": [], "source": [ "mpg = pd.read_csv (\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv\")" ] }, { "cell_type": "code", "execution_count": 3, "id": "24bcdf82-a6a8-4327-97a5-470d32b2e741", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# By default alpha is not applied to the legend.\n", "\n", "base_mpg_plot = ggplot(mpg, aes('displ', 'hwy', color='drv')) + \\\n", " geom_point(size=4, alpha=0.2, stroke=0)\n", "base_mpg_plot" ] }, { "cell_type": "code", "execution_count": 4, "id": "e8dd95ce-d2f5-4663-8e76-0d51f0a203dd", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Applying alpha and size for better look.\n", "\n", "base_mpg_plot + guides(color=guide_legend(override_aes={'alpha': 0.4, 'size': 8}))" ] }, { "cell_type": "markdown", "id": "f5b38a9a-5835-4030-85ce-0fcc9c8602c9", "metadata": {}, "source": [ "## Experiments with parameters" ] }, { "cell_type": "code", "execution_count": 5, "id": "f8b4c449", "metadata": {}, "outputs": [], "source": [ "np.random.seed(1)\n", "\n", "n = 4\n", "x = list(range(n))\n", "y = np.random.rand(n)\n", "v = np.random.rand(n)\n", "data = {'x': x, 'y': y, 'v': v}" ] }, { "cell_type": "code", "execution_count": 6, "id": "4e04a12d", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Base plot\n", "p = ggplot(data, aes('x', 'y')) + geom_point(aes(color=as_discrete('x'))) + ggsize(400, 200)\n", "p" ] }, { "cell_type": "code", "execution_count": 7, "id": "04c6a892", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Specifying common aesthetics for all the legend labels.\n", "\n", "p + guides(color=guide_legend(\n", " override_aes=dict(shape=22, size=8, stroke=1.8, fill='light-grey', alpha=0.6)\n", "))" ] }, { "cell_type": "code", "execution_count": 8, "id": "98097225", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Using a list of values.\n", "\n", "p + guides(color=guide_legend(\n", " override_aes=dict(color=['red', 'blue', 'green', 'pink'])\n", "))" ] }, { "cell_type": "code", "execution_count": 9, "id": "89ef8a97-02cc-4d23-9b84-fccb493628a4", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Use 'None' to keep the original value.\n", "\n", "p + guides(color=guide_legend(\n", " override_aes=dict(color=['red', None, 'green', None])\n", "))" ] }, { "cell_type": "code", "execution_count": 10, "id": "6e9cb997", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Using a list that is smaller than the number of legend keys.\n", "# The last value spreads to the rest of the keys.\n", "\n", "p + guides(color=guide_legend(\n", " override_aes=dict(color=['grey'], size=[10, 8])\n", "))" ] }, { "cell_type": "code", "execution_count": 11, "id": "fa2b1a19-995c-4596-8dfc-d6831b46b646", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Using a list that is smaller than the number of legend keys.\n", "# 'None' can be used to stop value propagation; the rest keys will get their original values.\n", "\n", "p + guides(color=guide_legend(\n", " override_aes=dict(color=['grey', None], size=[10, None, 8])\n", "))" ] }, { "cell_type": "markdown", "id": "ccebef12-d878-48aa-a497-627d284d2fbc", "metadata": {}, "source": [ "## Reproduced an example from [rbind.io](https://aosmith.rbind.io/2020/07/09/ggplot2-override-aes/)\n" ] }, { "cell_type": "code", "execution_count": 12, "id": "612092f4-190b-411c-bda3-6f5e57d8ef2f", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dat = pd.DataFrame({\n", " 'g1': ['High', 'Low', 'High', 'Low', 'High', 'Low', 'High', 'Low', 'High', 'Low', 'High', 'Low', 'High', 'Low', 'High', 'Low'],\n", " 'g2': ['Control', 'Control', 'Treatment', 'Treatment', 'Control', 'Control', 'Treatment', 'Treatment', 'Control', 'Control', 'Treatment', 'Treatment', 'Control', 'Control', 'Treatment', 'Treatment'],\n", " 'x': [0.42, 0.39, 0.56, 0.59, 0.17, 0.95, 0.85, 0.25, 0.31, 0.75, 0.58, 0.9, 0.6, 0.86, 0.61, 0.61],\n", " 'y': [-1.4, 3.6, 1.1, -0.1, 0.5, 0, -1.8, 0.8, -1.1, -0.6, 0.2, 0.3, 1.1, 1.6, 0.9, -0.6]\n", "})\n", "\n", "bp = ggplot(dat, aes('x', 'y', color='g1', shape='g2')) + \\\n", " geom_point(size=5, alpha=0.6)\n", "bp" ] }, { "cell_type": "code", "execution_count": 13, "id": "16324c8c-df68-4159-b4d0-9eb50790bf8a", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Customized scales and default legend do not correspond well.\n", "\n", "cp = ggplot(dat, aes(x='x', y='y', fill='g1', shape='g2')) + \\\n", " geom_point(size=5, color='black') + \\\n", " scale_fill_manual(values=[\"#002F70\", \"#EDB4B5\"]) + \\\n", " scale_shape_manual(values=[21, 24])\n", "cp" ] }, { "cell_type": "code", "execution_count": 14, "id": "f4f54d77-5bd3-4477-83c2-8998da6fdab2", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# This can be fixed via override_aes.\n", "\n", "cp + guides(\n", " fill=guide_legend(override_aes={'shape': 21}),\n", " shape=guide_legend(override_aes={'fill': 'black'})\n", ")\n" ] }, { "cell_type": "markdown", "id": "dd3315a9-2a01-43e8-bf3e-749283bd94c8", "metadata": {}, "source": [ "## Testing" ] }, { "cell_type": "code", "execution_count": 15, "id": "f2889194", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# `fill` and `size` are mapped to the same variable. \n", "# The result is not defined if the same legend is set via different aesthetics.\n", "\n", "pie_plot = ggplot({'n': ['a', 'b', 'c']}) + geom_pie(aes(fill='n', size='n'), stat='identity')\n", "gggrid([\n", " pie_plot,\n", " pie_plot + guides(\n", " fill=guide_legend(override_aes=dict(color='blue')),\n", " size=guide_legend(override_aes=dict(stroke=4, color='black'))\n", " )\n", "]) + ggsize(800, 300)" ] }, { "cell_type": "code", "execution_count": 16, "id": "12cd9926-b947-407d-8106-294b02c2a062", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Verifying different aesthetics\n", "\n", "p + guides(color=guide_legend(override_aes=dict(\n", " color=['red', 'blue', 'green', 'pink'], \n", " shape=22, \n", " size=8, \n", " stroke=1.8, \n", " fill='white', \n", " alpha=0.4\n", ")))" ] }, { "cell_type": "code", "execution_count": 17, "id": "52eb6673-d236-4d27-8af2-5e0a8398d63e", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# An empty dictionary\n", "\n", "p + guides(color=guide_legend(override_aes={}))" ] }, { "cell_type": "code", "execution_count": 18, "id": "7c7db11a-ad6d-44a4-aa2f-e6143c5aab1a", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Invalid or 'None' values\n", "\n", "p + guides(color=guide_legend(override_aes=dict(color=[None], shape=-11, size=0, stroke=-2)))" ] }, { "cell_type": "code", "execution_count": 19, "id": "7b57d837-9a59-431b-83f7-5e041032e200", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# An empty list of values\n", "\n", "p + guides(color=guide_legend(override_aes=dict(color=[])))" ] }, { "cell_type": "code", "execution_count": 20, "id": "7a5ca960-6362-4317-9eb5-f0d99ea776da", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# `override_aes` in the case when some legend labels are combined into one;\n", "# see issue https://github.com/JetBrains/lets-plot/issues/1129 for details.\n", "\n", "df = pd.DataFrame({\n", " 'category': ['A', 'B', 'C', 'D'],\n", " 'value': [10, 15, 20, 25]\n", "})\n", "\n", "ggplot(df) + ggsize(400,200) + \\\n", " geom_point(aes(x='category', y='value', color='category')) + \\\n", " scale_color_manual(values=['red', 'blue', 'green', 'black'], labels=['Label A', 'Label B', 'Label C', 'Label C']) + \\\n", " guides(color = guide_legend(override_aes=dict(color=['pink', 'gray', 'green', 'orange'])))\n" ] }, { "cell_type": "markdown", "id": "4f4a7d72-7dd4-4ee8-965d-bff65fa2eb74", "metadata": {}, "source": [ "## Custom legend" ] }, { "cell_type": "code", "execution_count": 21, "id": "a113117f-67db-48e6-9cc2-9e443c5252a3", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Verifying how `overrides_aes` works for custom legends.\n", "# Note that the same effect can be achieved easier by using direct parameters.\n", "\n", "m_plot = ggplot(data, aes('x', 'y')) + \\\n", " geom_point(manual_key=\"Point\") + \\\n", " geom_line(manual_key=\"Line\") \n", "\n", "gggrid([\n", " m_plot,\n", " m_plot + guides(manual=guide_legend(override_aes=dict(size=6))),\n", " m_plot + guides(manual=guide_legend(override_aes=dict(size=[6, 2], color=['red', 'blue'])))\n", "]) + ggsize(900, 200)" ] }, { "cell_type": "code", "execution_count": 22, "id": "f274f43f-231e-4d78-a382-6c6b91bfeb6c", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Changing the order in the legend via `index` in `layer_key`\n", "\n", "m_plot2 = ggplot(data, aes('x', 'y')) + \\\n", " geom_point(manual_key=layer_key(\"Point\", index = 1)) + \\\n", " geom_line(manual_key=layer_key(\"Line\", index = 0))\n", "\n", "gggrid([\n", " m_plot2,\n", " m_plot2 + guides(manual=guide_legend(override_aes=dict(size=[2], color=['red', 'blue'])))\n", "]) + ggsize(600, 200)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "4ab8d56a-eedc-4e80-b710-b31a9e0eddc2", "metadata": {}, "outputs": [], "source": [] } ], "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.8.19" } }, "nbformat": 4, "nbformat_minor": 5 }