Download notebook (.ipynb)

Using Dictionaries for Customizing Scale’ Breaks, Labels and Values#

breaks and lables arguments in scale_xxx() function family each accepts list of values for customizing the scale breaks or labels.

However, it is often more convenient to use a dictionary of breaks (in the labels parameter) or a dictionary of labels (in the breaks parameter) for the breaks/labels customization.

Similar situation is with parameters breaks and values in the scale_xxx_manual() function family.

import pandas as pd

from lets_plot import *
LetsPlot.setup_html()
mpg_df = pd.read_csv ("https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv")
mpg_df.head(3)
Unnamed: 0 manufacturer model displ year cyl trans drv cty hwy fl class
0 1 audi a4 1.8 1999 4 auto(l5) f 18 29 p compact
1 2 audi a4 1.8 1999 4 manual(m5) f 21 29 p compact
2 3 audi a4 2.0 2008 4 manual(m6) f 20 31 p compact
p = ggplot(mpg_df, aes(x='displ', y='hwy', color='drv')) + geom_point() 
p

1. Customize the Legend’ Labels#

p = p + scale_color_discrete(labels={
    'f': 'front',
    'r': 'rear',
    '4': '4WD'    
})
p

2. Customize the X-Axis breaks and labels#

p = p + scale_x_continuous(breaks={
    'min': 1.6,
    '3.4': 3.4,
    '5.2': 5.2,
    'max': 7
})
p

3. Manually Reassign Colors#

p + scale_color_manual(values={ 
    'f': 'dark_blue', 
    'r': 'dark_green',
    '4': 'dark_magenta'
})