diff --git a/docs/f-23f/scale_params_with_dict.ipynb b/docs/f-23f/scale_params_with_dict.ipynb new file mode 100644 index 00000000000..300607fd873 --- /dev/null +++ b/docs/f-23f/scale_params_with_dict.ipynb @@ -0,0 +1,413 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "answering-manner", + "metadata": {}, + "source": [ + "# Using a Dictionary as Parameter Values in Scaling Functions\n", + "\n", + "\n", + "The `labels` argument for scales can be specified using a dictinary where keys are treated as the original values (breaks) and the corresponding values are used as the labels to display. The `breaks` dictionary maps labels to breaks.\n", + "\n", + "The same opportunity is for `values` parameter in manual scale: if this is a dictinary, then the values will be matched based on the names." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "pressed-angola", + "metadata": {}, + "outputs": [], + "source": [ + "import pandas as pd\n", + "\n", + "from lets_plot import *" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "recorded-horizon", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "\n", + " \n", + " \n", + " " + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "LetsPlot.setup_html()" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "honest-stewart", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
Unnamed: 0manufacturermodeldisplyearcyltransdrvctyhwyflclass
01audia41.819994auto(l5)f1829pcompact
12audia41.819994manual(m5)f2129pcompact
23audia42.020084manual(m6)f2031pcompact
\n", + "
" + ], + "text/plain": [ + " Unnamed: 0 manufacturer model displ year cyl trans drv cty hwy \\\n", + "0 1 audi a4 1.8 1999 4 auto(l5) f 18 29 \n", + "1 2 audi a4 1.8 1999 4 manual(m5) f 21 29 \n", + "2 3 audi a4 2.0 2008 4 manual(m6) f 20 31 \n", + "\n", + " fl class \n", + "0 p compact \n", + "1 p compact \n", + "2 p compact " + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mpg_df = pd.read_csv (\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv\")\n", + "mpg_df.head(3)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "previous-serbia", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + " " + ], + "text/plain": [ + "" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "p = ggplot(mpg_df, aes(x='displ', y='hwy', color='drv')) + geom_point() \n", + "p" + ] + }, + { + "cell_type": "markdown", + "id": "brazilian-stack", + "metadata": {}, + "source": [ + "#### Use `labels` and `breaks` Dictionaries" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "amber-jackson", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + " " + ], + "text/plain": [ + "" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "drv_dict = {\n", + " 'f': 'front-wheel',\n", + " 'r': 'rear-wheel',\n", + " '4': '4wd' \n", + "}\n", + "breaks_dict = {\n", + " 'min': 1.6,\n", + " '3.4': 3.4,\n", + " '5.2': 5.2,\n", + " 'max': 7\n", + "}\n", + "\n", + "p + scale_color_discrete(labels=drv_dict) + scale_x_continuous(breaks=breaks_dict)" + ] + }, + { + "cell_type": "markdown", + "id": "classical-tournament", + "metadata": {}, + "source": [ + "#### Use `values` Dictionary" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "departmental-inventory", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + " " + ], + "text/plain": [ + "" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "values_dict = { \n", + " 'f': 'dark_blue', \n", + " 'r': 'dark_green',\n", + " '4': 'dark_magenta'\n", + "}\n", + "\n", + "p + scale_color_manual(values=values_dict)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.7.10" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/future_changes.md b/future_changes.md index 65ff368a0b1..77411dde9af 100644 --- a/future_changes.md +++ b/future_changes.md @@ -7,6 +7,12 @@ See: [example notebook](https://nbviewer.jupyter.org/github/JetBrains/lets-plot/blob/master/docs/f-23f/new_stat_count_vars.ipynb). +- Using a dictionary as a value of `'labels'`, `'breaks'` ([[#169](https://github.com/JetBrains/lets-plot/issues/169)]) + and `'values'` ([[#882](https://github.com/JetBrains/lets-plot/issues/882)]) parameters in scaling functions: + + See: [example notebook](https://nbviewer.jupyter.org/github/JetBrains/lets-plot/blob/master/docs/f-23f/scale_params_with_dict.ipynb). + + ### Changed - The `plot_margin` parameter in `theme()` and the `margin` parameter in `element_text()` accept a number or a list of numbers: @@ -27,4 +33,6 @@ - Enormous CPU / Time/ Memory consumption on some data [[#932](https://github.com/JetBrains/lets-plot/issues/932)]. - scale_x_log2(), scale_y_log2() as a shortcut for trans='log2' [[#922](https://github.com/JetBrains/lets-plot/issues/922)]. - How to calculate proportion of points with same coordinate [[#936](https://github.com/JetBrains/lets-plot/issues/936)]. -- gggrid: composite plot is not visible if saved with ggsave [[#942](https://github.com/JetBrains/lets-plot/issues/942)]. \ No newline at end of file +- gggrid: composite plot is not visible if saved with ggsave [[#942](https://github.com/JetBrains/lets-plot/issues/942)]. +- Make scale's 'breaks' / 'labels' parameters understand dict of breaks as keys and labels as values [[#169](https://github.com/JetBrains/lets-plot/issues/169)]. +- scale manual: "values" parameter should accept dictionary as a value [[#882](https://github.com/JetBrains/lets-plot/issues/882)]. \ No newline at end of file diff --git a/python-package/lets_plot/plot/scale.py b/python-package/lets_plot/plot/scale.py index 49cf9ea4763..d33fa5d5619 100644 --- a/python-package/lets_plot/plot/scale.py +++ b/python-package/lets_plot/plot/scale.py @@ -35,10 +35,10 @@ def scale_shape(solid=True, name=None, breaks=None, labels=None, limits=None, na Are the shapes solid (default) True, or hollow (False). name : str The name of the scale - used as the axis label or the legend title. - breaks : list - A vector specifying values to display as ticks on axis. - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list Continuous scale: a numeric vector of length two providing limits of the scale. Discrete scale: a vector specifying the data range for the scale @@ -107,17 +107,18 @@ def scale_manual(aesthetic, values, *, ---------- aesthetic : str or list The name(s) of the aesthetic(s) that this scale works with. - values : list of str + values : list of str or dict A set of aesthetic values to map data values to. - The values will be matched in order (usually alphabetical) with the limits of the scale. + If this is a list, the values will be matched in order (usually alphabetical) with the limits of the scale. + If a dictionary, then the values will be matched based on the names. name : str The name of the scale - used as the axis label or the legend title. If None, the default, the name of the scale is taken from the first mapping used for that aesthetic. - breaks : list - A vector specifying values to display as ticks on axis. - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list Continuous scale: a numeric vector of length two providing limits of the scale. Discrete scale: a vector specifying the data range for the scale @@ -159,6 +160,23 @@ def scale_manual(aesthetic, values, *, breaks=[2, 4, 7], labels=['red', 'green', 'blue']) """ + + # 'values' - dict of limits or breaks as keys and values as values + if isinstance(values, dict): + if breaks is None and limits is None: + breaks = list(values.keys()) + values = list(values.values()) + else: + base_order = breaks if limits is None else limits + if isinstance(base_order, dict): + base_order = list(base_order.values()) + new_values = [values[break_value] for break_value in base_order if break_value in values] + if new_values: + no_match_values = list(set(values.values()) - set(new_values)) # doesn't preserve order + values = new_values + no_match_values + else: + values = None + return _scale(aesthetic, name=name, breaks=breaks, @@ -180,17 +198,18 @@ def scale_color_manual(values, name=None, breaks=None, labels=None, limits=None, Parameters ---------- - values : list of str + values : list of str or dict A set of aesthetic values to map data values to. - The values will be matched in order (usually alphabetical) with the limits of the scale. + If this is a list, the values will be matched in order (usually alphabetical) with the limits of the scale. + If a dictionary, then the values will be matched based on the names. name : str The name of the scale - used as the axis label or the legend title. If None, the default, the name of the scale is taken from the first mapping used for that aesthetic. - breaks : list - A vector specifying values to display as ticks on axis. - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list Continuous scale: a numeric vector of length two providing limits of the scale. Discrete scale: a vector specifying the data range for the scale @@ -249,17 +268,18 @@ def scale_fill_manual(values, name=None, breaks=None, labels=None, limits=None, Parameters ---------- - values : list of str + values : list of str or dict A set of aesthetic values to map data values to. - The values will be matched in order (usually alphabetical) with the limits of the scale. + If this is a list, the values will be matched in order (usually alphabetical) with the limits of the scale. + If a dictionary, then the values will be matched based on the names. name : str The name of the scale - used as the axis label or the legend title. If None, the default, the name of the scale is taken from the first mapping used for that aesthetic. - breaks : list - A vector specifying values to display as ticks on axis. - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list Continuous scale: a numeric vector of length two providing limits of the scale. Discrete scale: a vector specifying the data range for the scale @@ -318,17 +338,18 @@ def scale_size_manual(values, name=None, breaks=None, labels=None, limits=None, Parameters ---------- - values : list of str + values : list of str or dict A set of aesthetic values to map data values to. - The values will be matched in order (usually alphabetical) with the limits of the scale. + If this is a list, the values will be matched in order (usually alphabetical) with the limits of the scale. + If a dictionary, then the values will be matched based on the names. name : str The name of the scale - used as the axis label or the legend title. If None, the default, the name of the scale is taken from the first mapping used for that aesthetic. - breaks : list - A vector specifying values to display as ticks on axis. - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list Continuous scale: a numeric vector of length two providing limits of the scale. Discrete scale: a vector specifying the data range for the scale @@ -387,17 +408,18 @@ def scale_shape_manual(values, name=None, breaks=None, labels=None, limits=None, Parameters ---------- - values : list of str + values : list of str or dict A set of aesthetic values to map data values to. - The values will be matched in order (usually alphabetical) with the limits of the scale. + If this is a list, the values will be matched in order (usually alphabetical) with the limits of the scale. + If a dictionary, then the values will be matched based on the names. name : str The name of the scale - used as the axis label or the legend title. If None, the default, the name of the scale is taken from the first mapping used for that aesthetic. - breaks : list - A vector specifying values to display as ticks on axis. - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list Continuous scale: a numeric vector of length two providing limits of the scale. Discrete scale: a vector specifying the data range for the scale @@ -456,17 +478,18 @@ def scale_linetype_manual(values, name=None, breaks=None, labels=None, limits=No Parameters ---------- - values : list of str + values : list of str or dict A set of aesthetic values to map data values to. - The values will be matched in order (usually alphabetical) with the limits of the scale. + If this is a list, the values will be matched in order (usually alphabetical) with the limits of the scale. + If a dictionary, then the values will be matched based on the names. name : str The name of the scale - used as the axis label or the legend title. If None, the default, the name of the scale is taken from the first mapping used for that aesthetic. - breaks : list - A vector specifying values to display as ticks on axis. - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list Continuous scale: a numeric vector of length two providing limits of the scale. Discrete scale: a vector specifying the data range for the scale @@ -526,17 +549,18 @@ def scale_alpha_manual(values, name=None, breaks=None, labels=None, limits=None, Parameters ---------- - values : list of str + values : list of str or dict A set of aesthetic values to map data values to. - The values will be matched in order (usually alphabetical) with the limits of the scale. + If this is a list, the values will be matched in order (usually alphabetical) with the limits of the scale. + If a dictionary, then the values will be matched based on the names. name : str The name of the scale - used as the axis label or the legend title. If None, the default, the name of the scale is taken from the first mapping used for that aesthetic. - breaks : list - A vector specifying values to display as ticks on axis. - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list Continuous scale: a numeric vector of length two providing limits of the scale. Discrete scale: a vector specifying the data range for the scale @@ -608,10 +632,10 @@ def scale_continuous(aesthetic, *, The name of the scale - used as the axis label or the legend title. If None, the default, the name of the scale is taken from the first mapping used for that aesthetic. - breaks : list - A vector specifying values to display as ticks on axis. - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list A numeric vector of length two providing limits of the scale. na_value @@ -687,10 +711,10 @@ def scale_gradient(aesthetic, *, The name of the scale - used as the axis label or the legend title. If None, the default, the name of the scale is taken from the first mapping used for that aesthetic. - breaks : list - A vector specifying values to display as ticks on axis. - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list Continuous scale: a numeric vector of length two providing limits of the scale. Discrete scale: a vector specifying the data range for the scale @@ -763,10 +787,10 @@ def scale_fill_gradient(low=None, high=None, name=None, breaks=None, labels=None The name of the scale - used as the axis label or the legend title. If None, the default, the name of the scale is taken from the first mapping used for that aesthetic. - breaks : list - A vector specifying values to display as ticks on axis. - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list Continuous scale: a numeric vector of length two providing limits of the scale. Discrete scale: a vector specifying the data range for the scale @@ -838,10 +862,10 @@ def scale_fill_continuous(low=None, high=None, name=None, breaks=None, labels=No The name of the scale - used as the axis label or the legend title. If None, the default, the name of the scale is taken from the first mapping used for that aesthetic. - breaks : list - A vector specifying values to display as ticks on axis. - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list A numeric vector of length two providing limits of the scale. na_value @@ -911,10 +935,10 @@ def scale_color_gradient(low=None, high=None, name=None, breaks=None, labels=Non The name of the scale - used as the axis label or the legend title. If None, the default, the name of the scale is taken from the first mapping used for that aesthetic. - breaks : list - A vector specifying values to display as ticks on axis. - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list Continuous scale: a numeric vector of length two providing limits of the scale. Discrete scale: a vector specifying the data range for the scale @@ -986,10 +1010,10 @@ def scale_color_continuous(low=None, high=None, name=None, breaks=None, labels=N The name of the scale - used as the axis label or the legend title. If None, the default, the name of the scale is taken from the first mapping used for that aesthetic. - breaks : list - A numeric vector of positions (of ticks). - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list A numeric vector of length two providing limits of the scale. na_value @@ -1060,10 +1084,10 @@ def scale_gradient2(aesthetic, *, The name of the scale - used as the axis label or the legend title. If None, the default, the name of the scale is taken from the first mapping used for that aesthetic. - breaks : list - A vector specifying values to display as ticks on axis. - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list Continuous scale: a numeric vector of length two providing limits of the scale. Discrete scale: a vector specifying the data range for the scale @@ -1143,10 +1167,10 @@ def scale_fill_gradient2(low=None, mid=None, high=None, midpoint=0, name=None, b The name of the scale - used as the axis label or the legend title. If None, the default, the name of the scale is taken from the first mapping used for that aesthetic. - breaks : list - A vector specifying values to display as ticks on axis. - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list Continuous scale: a numeric vector of length two providing limits of the scale. Discrete scale: a vector specifying the data range for the scale @@ -1223,10 +1247,10 @@ def scale_color_gradient2(low=None, mid=None, high=None, midpoint=0, name=None, The name of the scale - used as the axis label or the legend title. If None, the default, the name of the scale is taken from the first mapping used for that aesthetic. - breaks : list - A vector specifying values to display as ticks on axis. - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list Continuous scale: a numeric vector of length two providing limits of the scale. Discrete scale: a vector specifying the data range for the scale @@ -1300,10 +1324,10 @@ def scale_gradientn(aesthetic, *, The name of the scale - used as the axis label or the legend title. If None, the default, the name of the scale is taken from the first mapping used for that aesthetic. - breaks : list - A vector specifying values to display as ticks on axis. - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list Continuous scale: a numeric vector of length two providing limits of the scale. Discrete scale: a vector specifying the data range for the scale @@ -1377,10 +1401,10 @@ def scale_color_gradientn(colors=None, name=None, breaks=None, labels=None, limi The name of the scale - used as the axis label or the legend title. If None, the default, the name of the scale is taken from the first mapping used for that aesthetic. - breaks : list - A vector specifying values to display as ticks on axis. - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list Continuous scale: a numeric vector of length two providing limits of the scale. Discrete scale: a vector specifying the data range for the scale @@ -1451,10 +1475,10 @@ def scale_fill_gradientn(colors=None, name=None, breaks=None, labels=None, limit The name of the scale - used as the axis label or the legend title. If None, the default, the name of the scale is taken from the first mapping used for that aesthetic. - breaks : list - A vector specifying values to display as ticks on axis. - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list Continuous scale: a numeric vector of length two providing limits of the scale. Discrete scale: a vector specifying the data range for the scale @@ -1534,10 +1558,10 @@ def scale_hue(aesthetic, *, The name of the scale - used as the axis label or the legend title. If None, the default, the name of the scale is taken from the first mapping used for that aesthetic. - breaks : list - A vector specifying values to display as ticks on axis. - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list Continuous scale: a numeric vector of length two providing limits of the scale. Discrete scale: a vector specifying the data range for the scale @@ -1619,10 +1643,10 @@ def scale_fill_hue(h=None, c=None, l=None, h_start=None, direction=None, name=No The name of the scale - used as the axis label or the legend title. If None, the default, the name of the scale is taken from the first mapping used for that aesthetic. - breaks : list - A vector specifying values to display as ticks on axis. - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list Continuous scale: a numeric vector of length two providing limits of the scale. Discrete scale: a vector specifying the data range for the scale @@ -1700,10 +1724,10 @@ def scale_color_hue(h=None, c=None, l=None, h_start=None, direction=None, name=N The name of the scale - used as the axis label or the legend title. If None, the default, the name of the scale is taken from the first mapping used for that aesthetic. - breaks : list - A vector specifying values to display as ticks on axis. - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list Continuous scale: a numeric vector of length two providing limits of the scale. Discrete scale: a vector specifying the data range for the scale @@ -1780,10 +1804,10 @@ def scale_discrete(aesthetic, *, The name of the scale - used as the axis label or the legend title. If None, the default, the name of the scale is taken from the first mapping used for that aesthetic. - breaks : list - A vector specifying values to display as ticks on axis. - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list A vector specifying the data range for the scale and the default order of their display in guides. @@ -1856,10 +1880,10 @@ def scale_fill_discrete(direction=None, The name of the scale - used as the axis label or the legend title. If None, the default, the name of the scale is taken from the first mapping used for that aesthetic. - breaks : list - A vector specifying values to display as ticks on axis. - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list A vector specifying the data range for the scale and the default order of their display in guides. @@ -1929,10 +1953,10 @@ def scale_color_discrete(direction=None, The name of the scale - used as the axis label or the legend title. If None, the default, the name of the scale is taken from the first mapping used for that aesthetic. - breaks : list - A vector specifying values to display as ticks on axis. - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list A vector specifying the data range for the scale and the default order of their display in guides. @@ -2005,10 +2029,10 @@ def scale_grey(aesthetic, *, The name of the scale - used as the axis label or the legend title. If None, the default, the name of the scale is taken from the first mapping used for that aesthetic. - breaks : list - A vector specifying values to display as ticks on axis. - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list Continuous scale: a numeric vector of length two providing limits of the scale. Discrete scale: a vector specifying the data range for the scale @@ -2085,10 +2109,10 @@ def scale_fill_grey(start=None, end=None, name=None, breaks=None, labels=None, l The name of the scale - used as the axis label or the legend title. If None, the default, the name of the scale is taken from the first mapping used for that aesthetic. - breaks : list - A vector specifying values to display as ticks on axis. - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list Continuous scale: a numeric vector of length two providing limits of the scale. Discrete scale: a vector specifying the data range for the scale @@ -2160,10 +2184,10 @@ def scale_color_grey(start=None, end=None, name=None, breaks=None, labels=None, The name of the scale - used as the axis label or the legend title. If None, the default, the name of the scale is taken from the first mapping used for that aesthetic. - breaks : list - A vector specifying values to display as ticks on axis. - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list Continuous scale: a numeric vector of length two providing limits of the scale. Discrete scale: a vector specifying the data range for the scale @@ -2263,10 +2287,10 @@ def scale_brewer(aesthetic, *, The name of the scale - used as the axis label or the legend title. If None, the default, the name of the scale is taken from the first mapping used for that aesthetic. - breaks : list - A vector specifying values to display as ticks on axis. - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list Continuous scale: a numeric vector of length two providing limits of the scale. Discrete scale: a vector specifying the data range for the scale @@ -2361,10 +2385,10 @@ def scale_fill_brewer(type=None, palette=None, direction=None, name=None, breaks The name of the scale - used as the axis label or the legend title. If None, the default, the name of the scale is taken from the first mapping used for that aesthetic. - breaks : list - A vector specifying values to display as ticks on axis. - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list Continuous scale: a numeric vector of length two providing limits of the scale. Discrete scale: a vector specifying the data range for the scale @@ -2456,10 +2480,10 @@ def scale_color_brewer(type=None, palette=None, direction=None, name=None, break The name of the scale - used as the axis label or the legend title. If None, the default, the name of the scale is taken from the first mapping used for that aesthetic. - breaks : list - A vector specifying values to display as ticks on axis. - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list Continuous scale: a numeric vector of length two providing limits of the scale. Discrete scale: a vector specifying the data range for the scale @@ -2568,10 +2592,10 @@ def scale_viridis(aesthetic, *, The name of the scale - used as the axis label or the legend title. If None, the default, the name of the scale is taken from the first mapping used for that aesthetic. - breaks : list - A vector specifying values to display as ticks on axis. - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list Continuous scale: a numeric vector of length two providing limits of the scale. Discrete scale: a vector specifying the data range for the scale @@ -2672,10 +2696,10 @@ def scale_fill_viridis(alpha=None, begin=None, end=None, direction=None, option= The name of the scale - used as the axis label or the legend title. If None, the default, the name of the scale is taken from the first mapping used for that aesthetic. - breaks : list - A vector specifying values to display as ticks on axis. - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list Continuous scale: a numeric vector of length two providing limits of the scale. Discrete scale: a vector specifying the data range for the scale @@ -2771,10 +2795,10 @@ def scale_color_viridis(alpha=None, begin=None, end=None, direction=None, option The name of the scale - used as the axis label or the legend title. If None, the default, the name of the scale is taken from the first mapping used for that aesthetic. - breaks : list - A vector specifying values to display as ticks on axis. - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list Continuous scale: a numeric vector of length two providing limits of the scale. Discrete scale: a vector specifying the data range for the scale @@ -2848,10 +2872,10 @@ def scale_alpha(range=None, name=None, breaks=None, labels=None, limits=None, na The name of the scale - used as the axis label or the legend title. If None, the default, the name of the scale is taken from the first mapping used for that aesthetic. - breaks : list - A numeric vector of positions (of ticks). - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list A vector specifying the data range for the scale and the default order of their display in guides. @@ -2917,10 +2941,10 @@ def scale_size(range=None, name=None, breaks=None, labels=None, limits=None, na_ The name of the scale - used as the axis label or the legend title. If None, the default, the name of the scale is taken from the first mapping used for that aesthetic. - breaks : list - A numeric vector of positions (of ticks). - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list A vector specifying the data range for the scale and the default order of their display in guides. @@ -2987,10 +3011,10 @@ def scale_size_area(max_size=None, name=None, breaks=None, labels=None, limits=N The name of the scale - used as the axis label or the legend title. If None, the default, the name of the scale is taken from the first mapping used for that aesthetic. - breaks : list - A numeric vector of positions (of ticks). - labels : list - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list A vector specifying the data range for the scale and the default order of their display in guides. @@ -3062,10 +3086,10 @@ def scale_linewidth(range=None, name=None, breaks=None, labels=None, limits=None The name of the scale - used as the axis label or the legend title. If None, the default, the name of the scale is taken from the first mapping used for that aesthetic. - breaks : list - A numeric vector of positions (of ticks). - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list A vector specifying the data range for the scale and the default order of their display in guides. @@ -3131,10 +3155,10 @@ def scale_stroke(range=None, name=None, breaks=None, labels=None, limits=None, The name of the scale - used as the axis label or the legend title. If None, the default, the name of the scale is taken from the first mapping used for that aesthetic. - breaks : list - A numeric vector of positions (of ticks). - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list A vector specifying the data range for the scale and the default order of their display in guides. @@ -3207,10 +3231,10 @@ def _scale(aesthetic, *, The name(s) of the aesthetic(s) that this scale works with. name : str The name of the scale - used as the axis label or the legend title - breaks : list - A numeric vector of positions (of ticks) - labels : list - A vector of labels (on ticks) + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list A numeric vector of length two providing limits of the scale. expand : list @@ -3240,6 +3264,30 @@ def _scale(aesthetic, *, args = locals().copy() args.pop('other') + # 'breaks' - dict of labels as keys and breaks as values + if isinstance(breaks, dict): + if labels is None: + args['labels'] = list(breaks.keys()) + breaks = list(breaks.values()) + args['breaks'] = breaks + + # 'labels' - dict of breaks as keys and labels as values + if isinstance(labels, dict): + if breaks is None: + args['breaks'] = list(labels.keys()) + args['labels'] = list(labels.values()) + else: + new_labels = [] + new_breaks = [] + for break_value in breaks: + if break_value in labels: + new_labels.append(labels[break_value]) + new_breaks.append(break_value) + + breaks_without_label = [item for item in breaks if item not in new_breaks] # keeps order + args['breaks'] = new_breaks + breaks_without_label + args['labels'] = new_labels + specs = [] if isinstance(aesthetic, list): args.pop('aesthetic') diff --git a/python-package/lets_plot/plot/scale_identity_.py b/python-package/lets_plot/plot/scale_identity_.py index 268e6d4ff18..6479d328919 100644 --- a/python-package/lets_plot/plot/scale_identity_.py +++ b/python-package/lets_plot/plot/scale_identity_.py @@ -33,10 +33,10 @@ def scale_identity(aesthetic, *, The name(s) of the aesthetic(s) that this scale works with. name : str The name of the scale - used as the axis label or the legend title. - breaks : list of float - A vector specifying values to display as ticks on axis. - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list Continuous scale: a numeric vector of length two providing limits of the scale. Discrete scale: a vector specifying the data range for the scale @@ -100,10 +100,10 @@ def scale_color_identity(name=None, breaks=None, labels=None, limits=None, na_va ---------- name : str The name of the scale - used as the axis label or the legend title. - breaks : list of float - A vector specifying values to display as ticks on axis. - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list Continuous scale: a numeric vector of length two providing limits of the scale. Discrete scale: a vector specifying the data range for the scale @@ -168,10 +168,10 @@ def scale_fill_identity(name=None, breaks=None, labels=None, limits=None, na_val ---------- name : str The name of the scale - used as the axis label or the legend title. - breaks : list of float - A vector specifying values to display as ticks on axis. - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list Continuous scale: a numeric vector of length two providing limits of the scale. Discrete scale: a vector specifying the data range for the scale @@ -236,10 +236,10 @@ def scale_shape_identity(name=None, breaks=None, labels=None, limits=None, na_va ---------- name : str The name of the scale - used as the axis label or the legend title. - breaks : list of float - A vector specifying values to display as ticks on axis. - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list Continuous scale: a numeric vector of length two providing limits of the scale. Discrete scale: a vector specifying the data range for the scale @@ -304,10 +304,10 @@ def scale_linetype_identity(name=None, breaks=None, labels=None, limits=None, na ---------- name : str The name of the scale - used as the axis label or the legend title. - breaks : list of float - A vector specifying values to display as ticks on axis. - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list Continuous scale: a numeric vector of length two providing limits of the scale. Discrete scale: a vector specifying the data range for the scale @@ -375,10 +375,10 @@ def scale_alpha_identity(name=None, breaks=None, labels=None, limits=None, na_va ---------- name : str The name of the scale - used as the axis label or the legend title. - breaks : list of float - A vector specifying values to display as ticks on axis. - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list Continuous scale: a numeric vector of length two providing limits of the scale. Discrete scale: a vector specifying the data range for the scale @@ -440,10 +440,10 @@ def scale_size_identity(name=None, breaks=None, labels=None, limits=None, na_val ---------- name : str The name of the scale - used as the axis label or the legend title. - breaks : list of float - A vector specifying values to display as ticks on axis. - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list Continuous scale: a numeric vector of length two providing limits of the scale. Discrete scale: a vector specifying the data range for the scale @@ -505,10 +505,10 @@ def scale_linewidth_identity(name=None, breaks=None, labels=None, limits=None, n ---------- name : str The name of the scale - used as the axis label or the legend title. - breaks : list of float - A vector specifying values to display as ticks on axis. - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list Continuous scale: a numeric vector of length two providing limits of the scale. Discrete scale: a vector specifying the data range for the scale @@ -568,10 +568,10 @@ def scale_stroke_identity(name=None, breaks=None, labels=None, limits=None, na_v ---------- name : str The name of the scale - used as the axis label or the legend title. - breaks : list of float - A vector specifying values to display as ticks on axis. - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list Continuous scale: a numeric vector of length two providing limits of the scale. Discrete scale: a vector specifying the data range for the scale diff --git a/python-package/lets_plot/plot/scale_position.py b/python-package/lets_plot/plot/scale_position.py index 1345d22888a..6bd84ba0475 100644 --- a/python-package/lets_plot/plot/scale_position.py +++ b/python-package/lets_plot/plot/scale_position.py @@ -42,10 +42,10 @@ def scale_x_continuous(name=None, *, The name of the scale - used as the axis label or the legend title. If None, the default, the name of the scale is taken from the first mapping used for that aesthetic. - breaks : list - A numeric vector of positions (of ticks). - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list A numeric vector of length two providing limits of the scale. expand : list @@ -121,10 +121,10 @@ def scale_y_continuous(name=None, *, The name of the scale - used as the axis label or the legend title. If None, the default, the name of the scale is taken from the first mapping used for that aesthetic. - breaks : list - A numeric vector of positions (of ticks). - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list A numeric vector of length two providing limits of the scale. expand : list @@ -199,10 +199,10 @@ def scale_x_log10(name=None, *, The name of the scale - used as the axis label or the legend title. If None, the default, the name of the scale is taken from the first mapping used for that aesthetic. - breaks : list - A numeric vector of positions (of ticks). - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list A numeric vector of length two providing limits of the scale. expand : list @@ -271,10 +271,10 @@ def scale_y_log10(name=None, *, The name of the scale - used as the axis label or the legend title. If None, the default, the name of the scale is taken from the first mapping used for that aesthetic. - breaks : list - A numeric vector of positions (of ticks). - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list A numeric vector of length two providing limits of the scale. expand : list @@ -343,10 +343,10 @@ def scale_x_log2(name=None, *, The name of the scale - used as the axis label or the legend title. If None, the default, the name of the scale is taken from the first mapping used for that aesthetic. - breaks : list - A numeric vector of positions (of ticks). - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list A numeric vector of length two providing limits of the scale. expand : list @@ -415,10 +415,10 @@ def scale_y_log2(name=None, *, The name of the scale - used as the axis label or the legend title. If None, the default, the name of the scale is taken from the first mapping used for that aesthetic. - breaks : list - A numeric vector of positions (of ticks). - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list A numeric vector of length two providing limits of the scale. expand : list @@ -487,10 +487,10 @@ def scale_x_reverse(name=None, *, The name of the scale - used as the axis label or the legend title. If None, the default, the name of the scale is taken from the first mapping used for that aesthetic. - breaks : list - A numeric vector of positions (of ticks). - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list A numeric vector of length two providing limits of the scale. expand : list @@ -559,10 +559,10 @@ def scale_y_reverse(name=None, *, The name of the scale - used as the axis label or the legend title. If None, the default, the name of the scale is taken from the first mapping used for that aesthetic. - breaks : list - A numeric vector of positions (of ticks). - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list A numeric vector of length two providing limits of the scale. expand : list @@ -636,10 +636,10 @@ def scale_x_discrete(name=None, *, The name of the scale - used as the axis label or the legend title. If None, the default, the name of the scale is taken from the first mapping used for that aesthetic. - breaks : list - A vector specifying values to display as ticks on axis. - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list A vector specifying the data range for the scale and the default order of their display in guides. expand : list @@ -718,10 +718,10 @@ def scale_x_discrete_reversed(name=None, *, The name of the scale - used as the axis label or the legend title. If None, the default, the name of the scale is taken from the first mapping used for that aesthetic. - breaks : list - A vector specifying values to display as ticks on axis. - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list A vector specifying the data range for the scale and the default order of their display in guides. expand : list @@ -795,10 +795,10 @@ def scale_y_discrete(name=None, *, The name of the scale - used as the axis label or the legend title. If None, the default, the name of the scale is taken from the first mapping used for that aesthetic. - breaks : list - A vector specifying values to display as ticks on axis. - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list A vector specifying the data range for the scale and the default order of their display in guides. expand : list @@ -877,10 +877,10 @@ def scale_y_discrete_reversed(name=None, *, The name of the scale - used as the axis label or the legend title. If None, the default, the name of the scale is taken from the first mapping used for that aesthetic. - breaks : list - A vector specifying values to display as ticks on axis. - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list A vector specifying the data range for the scale and the default order of their display in guides. expand : list of two numbers @@ -959,10 +959,10 @@ def scale_x_datetime(name=None, *, The name of the scale - used as the axis label or the legend title. If None, the default, the name of the scale is taken from the first mapping used for that aesthetic. - breaks : list - A vector specifying values to display as ticks on axis. - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list A vector of length two providing limits of the scale. expand : list @@ -1041,10 +1041,10 @@ def scale_y_datetime(name=None, *, The name of the scale - used as the axis label or the legend title. If None, the default, the name of the scale is taken from the first mapping used for that aesthetic. - breaks : list - A vector specifying values to display as ticks on axis. - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list A vector of length two providing limits of the scale. expand : list of two numbers @@ -1124,10 +1124,10 @@ def scale_x_time(name=None, *, The name of the scale - used as the axis label or the legend title. If None, the default, the name of the scale is taken from the first mapping used for that aesthetic. - breaks : list - A numeric vector of positions (of ticks). - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list A numeric vector of length two providing limits of the scale. expand : list @@ -1200,10 +1200,10 @@ def scale_y_time(name=None, *, The name of the scale - used as the axis label or the legend title. If None, the default, the name of the scale is taken from the first mapping used for that aesthetic. - breaks : list - A numeric vector of positions (of ticks). - labels : list of str - A vector of labels (on ticks). + breaks : list or dict + A list of data values specifying the positions of ticks, or a dictionary which maps the tick labels to the breaks values. + labels : list of str or dict + A list of labels on ticks, or a dictionary which maps the breaks values to the tick labels. limits : list A numeric vector of length two providing limits of the scale. expand : list diff --git a/python-package/test/plot/test_scale.py b/python-package/test/plot/test_scale.py index 9e5d174770c..81a817a5b34 100644 --- a/python-package/test/plot/test_scale.py +++ b/python-package/test/plot/test_scale.py @@ -65,3 +65,67 @@ def test_scale_x_discrete(): assert as_dict['aesthetic'] == 'x' assert as_dict['name'] == 'N' assert as_dict['discrete'] + + +# Use dictionary in scale_xxx(labels) + +def test_scale_labels_dict(): + spec = gg.scale_x_discrete(labels=dict(a="A", b="B", c="C")) + as_dict = spec.as_dict() + assert as_dict['breaks'] == ['a', 'b', 'c'] + assert as_dict['labels'] == ['A', 'B', 'C'] + + +def test_scale_labels_dict_with_specified_breaks(): + spec = gg.scale_x_discrete(labels=dict(a="A", b="B", c="C"), breaks=['a', 'd', 'c']) + as_dict = spec.as_dict() + # use the order as in original 'breaks' + correct 'breaks' (without label - to the end of the list) + assert as_dict['breaks'] == ['a', 'c', 'd'] + assert as_dict['labels'] == ['A', 'C'] + + +def test_scale_labels_dict_no_matches_with_specified_breaks(): + spec = gg.scale_x_discrete(labels=dict(a="A", b="B", c="C"), breaks=['d', 'e']) + as_dict = spec.as_dict() + assert as_dict['breaks'] == ['d', 'e'] + assert as_dict['labels'] == [] + + +def test_scale_breaks_dict(): + spec = gg.scale_x_discrete(breaks=dict(A="a", B="b", C="c")) + as_dict = spec.as_dict() + assert as_dict['breaks'] == ['a', 'b', 'c'] + assert as_dict['labels'] == ['A', 'B', 'C'] + + +# Use dictionary in scale_manual(values) + +def test_scale_manual_values_dict(): + spec = gg.scale_fill_manual(values=dict(a="A", b="B", c="C")) + as_dict = spec.as_dict() + assert as_dict['breaks'] == ['a', 'b', 'c'] + assert as_dict['values'] == ['A', 'B', 'C'] + + +def test_scale_manual_values_dict_with_specified_breaks(): + spec = gg.scale_fill_manual(values=dict(a="A", b="B", c="C"), breaks=['a', 'c']) + as_dict = spec.as_dict() + # order as in original 'breaks', missing breaks - to the end of the list + assert as_dict['breaks'] == ['a', 'c'] + assert as_dict['values'] == ['A', 'C', 'B'] + + +def test_scale_manual_values_dict_with_specified_breaks_and_limits(): + spec = gg.scale_fill_manual(values=dict(a="A", b="B", c="C"), breaks=['a', 'c'], limits=['b', 'c']) + as_dict = spec.as_dict() + # priority to 'limits' as a base list to choose the order of values + assert as_dict['breaks'] == ['a', 'c'] + assert as_dict['limits'] == ['b', 'c'] + assert as_dict['values'] == ['B', 'C', 'A'] + + +def test_scale_manual_values_dict_no_matches_with_specified_breaks(): + spec = gg.scale_fill_manual(values=dict(a="A", b="B", c="C"), breaks=['d', 'e']) + as_dict = spec.as_dict() + assert as_dict['breaks'] == ['d', 'e'] + assert 'values' not in as_dict