Theme-driven Facet Spacing Options

You can control spacing in multipanel (faceted) plots.

  • strip_spacing - the gap between the facet strip (title bar) and the plot panel. By default, there is no gap.
  • panel_spacing - the gap between plot panels in both directions (default: 10).
  • strip_spacing_x, strip_spacing_y, panel_spacing_x, panel_spacing_y - directional overrides to control spacing horizontally (suffix _x) or vertically (suffix _y) only.
In [1]:
import pandas as pd

from lets_plot import *
In [2]:
LetsPlot.setup_html()
In [3]:
df = pd.read_csv("https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv")
print(df.shape)
df.head()
(234, 12)
Out[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
3 4 audi a4 2.0 2008 4 auto(av) f 21 30 p compact
4 5 audi a4 2.8 1999 6 auto(l5) f 16 26 p compact
In [4]:
# Base plot

p = ggplot(df, aes(x="cty", y="hwy")) + \
    geom_point(aes(color="class"), shape=21) + \
    theme_light() + \
    facet_grid(x="cyl", y="year")
p
Out[4]:
In [5]:
# Increase the gap between facet strip and panel

p + theme(strip_spacing=10)
Out[5]:
In [6]:
# Make panels closer together for a more compact view

p + theme(panel_spacing=3)
Out[6]:
In [7]:
# Fine-tune horizontal and vertical spacing independently

p + theme(strip_spacing_y=5, panel_spacing_x=1, panel_spacing_y=5)
Out[7]: