In [1]:
from lets_plot import *
from lets_plot.mapping import as_discrete
In [2]:
LetsPlot.setup_html()
In [3]:
df1 = {
    'x' : ['c', 'c', 'a', 'a', 'd', 'b', 'b', 'a']
}

gggrid([
    ggplot(df1) + geom_bar(aes('x')),
    ggplot(df1) + geom_bar(aes(as_discrete('x', levels=['a','b','c','d']))),
    ggplot(df1) + geom_bar(aes(as_discrete('x', levels=['a','d']))) # missed values will be added
])
Out[3]:
In [4]:
df2 = {
  'x' : [3, 2, 1, 3, 3, 1, 4]
}

gggrid([
    ggplot(df2) + geom_bar(aes('x', fill = as_discrete('x'))),
    ggplot(df2) + geom_bar(aes('x', fill = as_discrete('x', levels=[1,2,3,4])))
],ncol=1)
Out[4]:
In [5]:
ggplot(df2) + geom_bar(aes(as_discrete('x',order=-1), fill = as_discrete('x', levels=[1,2,3,4])))
Out[5]:
In [6]:
ggplot(df2) + geom_bar(aes(as_discrete('x',order=-1), fill = as_discrete('x', levels=[1,2])))
Out[6]:
In [7]:
# https://github.com/JetBrains/lets-plot/issues/914

data = dict(
    cat = ["B", "C", "A"],
    c = ["blue", "cyan", "gray"]
)

(ggplot(data) 
     + geom_point(aes(color="c"), size=15) 
     + scale_color_identity() 
     + theme_void() + ggsize(400, 200)
     + facet_wrap("cat", ncol=3, order=0)
)
Out[7]:
In [8]:
# Facet ordering corresponds to the levels (facet_wrap)

(ggplot(data) 
     + geom_point(aes(color=as_discrete("cat", levels=["C","A","B"])), size=15)
     + theme_void() + ggsize(400, 200)
     + facet_wrap("cat", ncol=3, order=0)
)
Out[8]:
In [9]:
# Facet ordering corresponds to the levels (facet_grid)

(ggplot(data) 
     + geom_point(aes(color=as_discrete("cat", levels=["C","A","B"])), size=15)
     + theme_void() + ggsize(400, 200)
     + facet_grid("cat", x_order=0)
)
Out[9]:
In [10]:
# levels in reverse order

(ggplot(data) 
     + geom_point(aes(color=as_discrete("cat", levels=["C","A","B"], order=-1)), size=15)
     + theme_void() + ggsize(400, 200)
     + facet_wrap("cat", ncol=3, order=0)
)
Out[10]: