Facet Strip Text Angle¶
Facet strip labels can be rotated with the angle parameter in element_text(). Use strip_text_x for horizontal facet labels and strip_text_y for vertical facet labels. Use strip_text to change both.
In [1]:
import pandas as pd
from lets_plot import *
LetsPlot.setup_html()
In [2]:
mtcars = pd.read_csv('https://raw.githubusercontent.com/JetBrains/lets-plot-docs/refs/heads/master/data/mtcars.csv')
mtcars.head(3)
Out[2]:
| model | mpg | cyl | disp | hp | drat | wt | qsec | vs | am | gear | carb | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | Mazda RX4 | 21.0 | 6 | 160.0 | 110 | 3.90 | 2.620 | 16.46 | 0 | 1 | 4 | 4 |
| 1 | Mazda RX4 Wag | 21.0 | 6 | 160.0 | 110 | 3.90 | 2.875 | 17.02 | 0 | 1 | 4 | 4 |
| 2 | Datsun 710 | 22.8 | 4 | 108.0 | 93 | 3.85 | 2.320 | 18.61 | 1 | 1 | 4 | 1 |
Horizontal Y-Axis Strip Labels¶
By default, y-axis strip labels are vertical. When the labels are short, it may be more convenient to display them horizontally.
In [3]:
base_plot = ggplot(mtcars, aes(x='mpg', y='wt')) + \
geom_point(color='steelblue') + \
facet_grid(y='cyl') + \
theme_bw() + \
theme(
panel_border=element_rect(color='gray20'),
strip_background_y=element_rect(fill='aliceblue', color='steelblue')
) + \
ggtitle('Default strip label appearance') + \
ggsize(height=300, width=500)
base_plot
Out[3]:
In [4]:
base_plot + theme(
strip_text_y=element_text(
angle=0, # horizontal <---
size=24,
face='bold',
color='darkblue / 0.4' # semi-transparent
)
) + ggtitle('Natural strip text direction')
Out[4]:
Rotating Horizontal and Vertical Strip Labels¶
In [5]:
data = {
'region': ['North', 'North', 'South', 'South', 'North', 'North', 'South', 'South'],
'segment': ['Retail', 'Retail', 'Retail', 'Retail', 'Online', 'Online', 'Online', 'Online'],
'quarter': ['Q1', 'Q2', 'Q1', 'Q2', 'Q1', 'Q2', 'Q1', 'Q2'],
'value': [12, 15, 9, 11, 16, 18, 13, 17]
}
ggplot(data, aes('quarter', 'value')) + \
geom_bar(stat='identity', fill='steelblue', color='white') + \
facet_grid(x='region', y='segment') + \
theme_bw() + \
theme(
strip_text_x=element_text(angle=35, size=13, face='bold', color='steelblue'),
strip_text_y=element_text(angle=35, size=13, face='bold', color='salmon'),
strip_background_x=element_rect(fill='aliceblue', color='cadetblue'),
strip_background_y=element_rect(fill='lemonchiffon', color='tan')
) + \
ggtitle('Custom angles for facet strip labels') + \
ggsize(700, 420)
Out[5]: