theme()
¶New parameters in theme()
to customize the legend key:
legend_key
- background underneath legend keys, set with element_rect()
legend_key_size
- size of legend keyslegend_key_width
- key background widthlegend_key_height
- key background heightlegend_key_spacing
- spacing between legend keyslegend_key_spacing_x
- spacing in the horizontal directionlegend_key_spacing_y
- spacing in the vertical directionfrom 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(x='hwy')) + \
geom_dotplot(aes(fill='class'), color='pen') + \
theme(legend_background=element_rect(size=0.5))
p
Add background underneath legend keys:
p1 = p + theme(legend_key=element_rect(fill='#efedf5', color='#756bb1'))
p1
Change size of legend keys:
p1 + theme(legend_key_size=30)
p2 = p1 + theme(legend_key_width=80, legend_key_height=20)
p2
Add spacing between legend keys:
p2 + theme(legend_key_spacing_y=10)