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