Legend Key Parameters in 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 keys
  • legend_key_width - key background width
  • legend_key_height - key background height
  • legend_key_spacing - spacing between legend keys
  • legend_key_spacing_x - spacing in the horizontal direction
  • legend_key_spacing_y - spacing in the 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(x='hwy')) + \
    geom_dotplot(aes(fill='class'), color='pen') + \
    theme(legend_background=element_rect(size=0.5))

p
Out[5]:

Add background underneath legend keys:

In [6]:
p1 = p + theme(legend_key=element_rect(fill='#efedf5', color='#756bb1'))
p1
Out[6]:

Change size of legend keys:

In [7]:
p1 + theme(legend_key_size=30)
Out[7]:
In [8]:
p2 = p1 + theme(legend_key_width=80, legend_key_height=20)
p2
Out[8]:

Add spacing between legend keys:

In [9]:
p2 + theme(legend_key_spacing_y=10)
Out[9]: