{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from lets_plot import *\n", "import lets_plot.mapping as pm" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df = {\n", " 'x': [0, 5, 10, 15],\n", " 'y': [0, 5, 10, 15],\n", " 'a': [1, 2, 3, 4],\n", " 'b': [4, 5, 6, 7]\n", "}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# as_discrete, no scale\n", "p = ggplot(df, aes(x='x', y='y')) + geom_point(aes(color=pm.as_discrete('a', label='custom name')), size=9)\n", "p" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# as_discrete, scale\n", "p = ggplot(df, aes(x='x', y='y')) \\\n", " + geom_point(aes(color='a', fill=pm.as_discrete('b')), shape=21, size=9, stroke=5) \\\n", " + scale_color_discrete()\n", "p" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# data in mappings, scale_color_discrete\n", "p = ggplot() + geom_point(aes(x=[0, 5, 10], y=[0, 5, 10], color=[1, 2, 4]), size=9) + scale_color_discrete()\n", "p" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Issue 1\n", "\n", "`as_discrete` is not working when used in \"ggplot()\" mapping." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "p3 = ggplot(df, aes(x='x', y='y', color=pm.as_discrete('a'))) + geom_point(size=9)\n", "p3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Issue 2\n", "`as_discrete` doesn't create groups the way discrete variable does." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df = {\n", " 'x': [0, 5, 10, 15],\n", " 'y': [0, 5, 10, 15],\n", " 'a': [0, 0, 1, 1],\n", " 'c': ['a', 'a', 'b', 'b']\n", "}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "p4 = ggplot(df, aes(x='x', y='y')) + geom_line(aes(color=pm.as_discrete('a')), size=3)\n", "p4" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# expected: 2 lines ('c' is a discrete variable)\n", "p5 = ggplot(df, aes(x='x', y='y')) + geom_line(aes(color='c'), size=3)\n", "p5" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# expected: 2 lines (`group` is defined manually)\n", "p6 = ggplot(df, aes(x='x', y='y')) + geom_line(aes(color='a', group='a'), size=3)\n", "p6" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Issue 2a\n", "Also about groups but with `stat` this time" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "mpg_df = pd.read_csv('https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv')\n", "mpg_plot = ggplot(mpg_df, aes(x='displ', y='hwy'))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cyl_factor = pm.as_discrete('cyl')\n", "mpg_plot + geom_point(aes(color=cyl_factor)) \\\n", "+ geom_smooth(aes(color=cyl_factor), method='lm', size=1, se=False)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# expected: seperate regression line for each group\n", "mpg_plot + geom_point(aes(color=cyl_factor)) \\\n", "+ geom_smooth(aes(color='cyl', group='cyl'), method='lm', size=1, se=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Issue 3\n", "\"Not an aesthetic 'group'\" error when used with `as_discrete`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mpg_plot + geom_point(aes(color=cyl_factor)) \\\n", "+ geom_smooth(aes(color=cyl_factor, group='cyl'), method='lm', size=1, se=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Issue 4\n", "Nice to have parameter `ordered`. Owerwise have to use `scale_discrete(breaks=[...])` to order groups by number of cylinders:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mpg_plot + geom_point(aes(color=cyl_factor)) \\\n", "+ scale_color_discrete(breaks=[4, 5, 6, 8])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mpg_plot + geom_point(aes(color=pm.as_discrete('cyl', label='cyl')))" ] } ], "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.7.10" } }, "nbformat": 4, "nbformat_minor": 2 }