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.
from IPython.display import Image
display(Image('images/theme_legend_scheme.png'))
import pandas as pd
from lets_plot import *
LetsPlot.setup_html()
mpg = pd.read_csv ("https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv")
mpg.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, 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
legend_margin
¶p + theme(legend_margin=[10, 20])
legend_box
¶p + theme(legend_box='horizontal')
legend_box_just
¶p + theme(legend_box_just='center')
legend_box_spacing
¶p + theme(legend_box='horizontal', legend_box_spacing=50)
legend_spacing
¶p + theme(legend_box='horizontal', legend_spacing_x=50)