lets_plot.mapping.as_discrete#

lets_plot.mapping.as_discrete(variable, label=None, order_by=None, order=None, levels=None)#

The function converts a column to a discrete scale and allows you to specify the order of its values.

Parameters:
variablestr

The name of the variable.

labelstr

The name of the scale - used as the axis label or the legend title.

order_bystr

The variable name to order by.

orderint

The ordering direction. 1 for ascending, -1 for descending.

levelslist

The list of values that defines a specific order of categories.

Returns:
MappingMeta or list

Variable meta information.

Notes

The plot will use a discrete scale for the aesthetic mapping. It is similar to the factor() function from R but works differently - there is no data transformation.

To enable ordering mode, at least one ordering parameter (order_by or order) should be specified. By the default, it will use descending direction and ordering by eigenvalues. You cannot specify different order settings for the same variable. But if these settings don’t contradict each other, they will be combined.

Examples

 1import numpy as np
 2from lets_plot import *
 3from lets_plot.mapping import as_discrete
 4LetsPlot.setup_html()
 5n = 100
 6np.random.seed(42)
 7data = {
 8    'x': np.random.normal(size=n),
 9    'y': np.random.normal(size=n),
10    'c': np.random.randint(5, size=n),
11}
12ggplot(data, aes('x', 'y')) + \
13    geom_point(aes(color=as_discrete('c')))

 1import numpy as np
 2from lets_plot import *
 3from lets_plot.mapping import as_discrete
 4LetsPlot.setup_html()
 5n = 100
 6np.random.seed(42)
 7data = {
 8    'x': np.random.uniform(size=100),
 9    'c': np.random.choice(list('abcde'), size=100),
10}
11ggplot(data) + \
12    geom_boxplot(aes(as_discrete('c', label='class', order=1), 'x'))

 1import numpy as np
 2from lets_plot import *
 3from lets_plot.mapping import as_discrete
 4LetsPlot.setup_html()
 5n = 100
 6np.random.seed(42)
 7data = {
 8    'x': np.random.normal(size=n),
 9    'c': np.random.choice(list('abcde'), size=n),
10    'i': np.random.randint(3, size=n),
11}
12ggplot(data) + \
13    geom_bar(aes(as_discrete('c', order=1, order_by='..count..'), 'x', \
14                 fill=as_discrete('i', order=1, order_by='..count..')))