Horizontal and Vertical Facet Labels

New theme() parameters allow to set the style for horizontal and vertical facet labels separately:

  • strip_background_x and strip_background_y parameters set the background of horizontal and vertical facet labels respectively, specified with element_rect();

  • strip_text_x and strip_text_y - horizontal and vertical facet labels, specified with element_text().

In [1]:
import pandas as pd

from lets_plot import *
In [2]:
LetsPlot.setup_html()
In [3]:
data = pd.DataFrame.from_records([
    ("pet", "cat", 5, "carnivore"),
    ("pet", "dog", 10, "carnivore"),
    ("pet", "rabbit", 2, "herbivore"),
    ("pet", "hamster", 1, "herbivore"),

    ("farm", "cow", 500, "herbivore"),
    ("farm", "pig", 100, "carnivore"),
    ("farm", "horse", 700, "herbivore"),
])
data.columns = ("animal_type", "animal", "weight", "diet")
In [4]:
p = ggplot(data, aes(x='animal', y='weight')) + \
    geom_bar(stat='identity') + \
    facet_grid(x='animal_type', y='diet', scales='free') + \
    theme_bw()

p
Out[4]:

Change the style for facet labels:

In [5]:
p + theme(strip_text_x=element_text(face='bold'),
          strip_background_x=element_rect(fill='pink'),
          strip_text_y=element_text(face='italic'),
          strip_background_y=element_rect(fill='light_blue'))
Out[5]:

Hide horizontal facet labels:

In [6]:
p + theme(strip_text_x=element_blank())
Out[6]: