as_discrete#
- 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 * 3LetsPlot.setup_html() 4n = 100 5np.random.seed(42) 6data = { 7 'x': np.random.normal(size=n), 8 'y': np.random.normal(size=n), 9 'c': np.random.randint(5, size=n), 10} 11ggplot(data, aes('x', 'y')) + \ 12 geom_point(aes(color=as_discrete('c')))
1import numpy as np 2from lets_plot import * 3LetsPlot.setup_html() 4n = 100 5np.random.seed(42) 6data = { 7 'x': np.random.uniform(size=100), 8 'c': np.random.choice(list('abcde'), size=100), 9} 10ggplot(data) + \ 11 geom_boxplot(aes(as_discrete('c', label='class', order=1), 'x'))
1import numpy as np 2from lets_plot import * 3LetsPlot.setup_html() 4n = 100 5np.random.seed(42) 6data = { 7 'x': np.random.normal(size=n), 8 'c': np.random.choice(list('abcde'), size=n), 9 'i': np.random.randint(3, size=n), 10} 11ggplot(data) + \ 12 geom_bar(aes(as_discrete('c', order=1, order_by='..count..'), 'x', \ 13 fill=as_discrete('i', order=1, order_by='..count..')))