In [1]:
from lets_plot import *
from lets_plot.mapping import as_discrete
LetsPlot.setup_html()
In [2]:
data = {"x": [1.0]}

ggplot(data, aes(x = "x", y = as_discrete("x", levels = [1.0]))) + geom_point()
Out[2]:
In [3]:
data = { "v1": ["a", "b", "c"]}

ggplot(data, aes(x = as_discrete("v1", levels = ["b", "c", "a"]), color = as_discrete("v1", order = -1))) + geom_point()
Out[3]:
In [4]:
d = {
    'x': [0, 1, 2],
    'v': [3, 1, 2],
    'o': [5, 6, 4]
}

p = ggplot(d) + ggsize(300, 200)

gggrid([
    p + geom_point(aes(x='x', color='v')) + ggtitle("Default"),
    p + geom_point(aes(x='x', color=as_discrete('v'))) + ggtitle('as_discrete(v)')
]) + ggsize(600, 200)
Out[4]:

Single order option

In [5]:
gggrid([
    p + geom_point(aes(x='x', color=as_discrete('v', levels=[1, 2, 3]))) + ggtitle("levels=[1, 2, 3]"),
    p + geom_point(aes(x='x', color=as_discrete('v', order=-1))) + ggtitle("order=-1"),
    p + geom_point(aes(x='x', color=as_discrete('v', order_by='o'))) + ggtitle("order_by='o'")
]) + ggsize(1000, 200)
Out[5]:

orderBy

In [6]:
gggrid([
    p + geom_point(aes(x='x', color=as_discrete('v', order_by='o', order=-1))) + ggtitle('order_by=o, order=-1'),
    p + geom_point(aes(x='x', color=as_discrete('v', order_by='o', order=1))) + ggtitle('order_by=o, order=1'),
]) + ggsize(600, 200)
Out[6]:

Order with levels

In [7]:
p + geom_point(aes(x='x', color=as_discrete('v', levels=[1, 2, 3], order=-1))) + ggtitle('levels=[1, 2, 3], order=-1') + ggsize(800, 600)
Out[7]:
In [8]:
gggrid([
    p + geom_point(aes(x='x', color=as_discrete('v', levels=[1, 2, 3], order=-1))) + ggtitle('levels=[1, 2, 3], order=-1'),
    p + geom_point(aes(x='x', color=as_discrete('v', levels=[1, 2, 3], order_by='o'))) + ggtitle('levels=[1, 2, 3], order_by=o'),
]) + ggsize(600, 200)
    
Out[8]:

Label

In [9]:
gggrid([
    p + geom_point(aes(x='x', color=as_discrete('v', label='VVV'))) + ggtitle("label=VVV"),
    p + geom_point(aes(x='x', color=as_discrete('v', label='VVV', levels=[1, 2, 3]))) + ggtitle("label=VVV, levels=[1, 2, 3]"),
]) + ggsize(600, 200)
Out[9]:

Grouping

In [10]:
df = {
    'x': [0, 5, 10, 15],
    'y': [0, 5, 10, 15],
    'a': [0, 0, 1, 1],
    'c': ['a', 'a', 'b', 'b']
}

ggplot(df, aes(x='x', y='y')) + geom_line(aes(color=as_discrete('a')), size=3)
Out[10]:
In [11]:
df = {
    'displ': [11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    'hwy':   [27, 25, 23, 45, 43, 41, 39, 37, 35, 33, 31, 29],
    'cyl':   [8, 8, 8, 4, 4, 4, 5, 5, 5, 6, 6, 6]
}

ggplot(df, aes(x='displ', y='hwy')) \
    + geom_point(aes(color=as_discrete('cyl'))) \
    + geom_smooth(aes(color=as_discrete('cyl')), method='lm', size=1, se=False)
Out[11]:
In [12]:
ggplot(df, aes(x='displ', y='hwy')) \
    + geom_point(aes(color=as_discrete('cyl', levels=[4, 5, 6, 8]))) \
    + geom_smooth(aes(color=as_discrete('cyl')), method='lm', size=1, se=False)
Out[12]:
In [ ]: