Legend Spacing and Margins

New parameters in theme() to customize the legend spacing and margins:

  • legend_box - arrangement of multiple legends ("horizontal" or "vertical");
  • legend_box_just - justification of each legend within the overall bounding box ("top", "bottom", "left", "right", "center");
  • legend_box_spacing - spacing between plotting area and legend box;

  • legend_margin - margin around each legend;

  • legend_spacing - spacing between legends, legend_spacing_x/legend_spacing_y - in the horizontal/vertical direction.
In [1]:
from IPython.display import Image

display(Image('images/theme_legend_scheme.png'))
In [2]:
import pandas as pd

from lets_plot import *
In [3]:
LetsPlot.setup_html()
In [4]:
mpg = pd.read_csv ("https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv")
mpg.head(3)
Out[4]:
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
In [5]:
p = ggplot(mpg, aes('displ', 'cty', )) + \
    geom_point(aes(fill='drv', size='hwy'), shape=21) + \
    scale_size(range=[1, 10], breaks=[15, 30, 40]) + \
    theme(legend_position='bottom', legend_background=element_rect(size=1))
p
Out[5]:

legend_margin

In [6]:
p + theme(legend_margin=[10, 20])
Out[6]:

legend_box

In [7]:
p + theme(legend_box='horizontal')
Out[7]:

legend_box_just

In [8]:
p + theme(legend_box_just='center')
Out[8]:

legend_box_spacing

In [9]:
p + theme(legend_box='horizontal',  legend_box_spacing=50)
Out[9]:

legend_spacing

In [10]:
p + theme(legend_box='horizontal', legend_spacing_x=50)
Out[10]: