lets_plot.mapping.as_discrete

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

The function is used to annotate a numeric data series as categorical data with the possibility of its ordering for the purposes of given visualization.

Parameters
  • variable (str) – The name of the variable.

  • label (str) – The name of the scale - used as the axis label or the legend title.

  • order_by (str) – The variable name to order by.

  • order (int) – The ordering direction. 1 for ascending, -1 for descending.

Returns

Variable meta information.

Return type

MappingMeta or list

Note

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')))