#!/usr/bin/env python # coding: utf-8 # # Multi-line Labels in Legend # # The 'newline' character (`\n`) now works as 'line break' in the legend text. # In[1]: from lets_plot import * LetsPlot.setup_html() # In[2]: import pandas as pd mpg = pd.read_csv ("https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv") # In[3]: p = ggplot(mpg, aes("displ", "hwy")) + geom_point(aes(color="cty", shape="drv"), size=5) # #### Legend labels with 'line breaks'. # In[4]: cty_breaks = [35,30,25,20,15,10] cty_labels = [str(br) + "\n(mpg)" for br in cty_breaks] drv_breaks = ["f", "r", "4"] drv_labels = ["Front-wheel\ndrive", "Rear \n wheel \n drive", "4 wheel drive"] p + \ scale_color_continuous(breaks=cty_breaks, labels=cty_labels) + \ scale_shape(breaks=drv_breaks, labels=drv_labels) + \ theme(legend_position="bottom")