Facet Strip Text Angle

Facet strip labels can be rotated with the angle parameter in elementText(). Use stripTextX for horizontal facet labels and stripTextY for vertical facet labels. Use stripText to change both.

In [1]:
%use lets-plot
In [2]:
LetsPlot.getInfo()
Out[2]:
Lets-Plot Kotlin API v.4.14.0. Frontend: Notebook with dynamically loaded JS. Lets-Plot JS v.4.10.1.
Outputs: Web (HTML+JS), Static SVG (hidden)
In [3]:
val mtcars = mapOf(
    "mpg" to listOf(21.0, 21.0, 22.8, 21.4, 18.7, 18.1, 14.3, 24.4, 22.8, 19.2, 17.8, 16.4),
    "wt" to listOf(2.620, 2.875, 2.320, 3.215, 3.440, 3.460, 3.570, 3.190, 3.150, 3.440, 3.440, 4.070),
    "cyl" to listOf("6", "6", "4", "6", "8", "6", "8", "4", "4", "6", "6", "8")
)

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 [4]:
val basePlot = letsPlot(mtcars) { x = "mpg"; y = "wt" } +
    geomPoint(color = "steelblue") +
    facetGrid(y = "cyl") +
    themeBW() +
    theme(
        panelBorder = elementRect(color = "gray20"),
        stripBackgroundY = elementRect(fill = "aliceblue", color = "steelblue")
    ) +
    ggtitle("Default strip label appearance") +
    ggsize(height = 300, width = 500)

basePlot
Out[4]:
3 4 4 3 4 6 14 16 18 20 22 24 3 4 8 Default strip label appearance wt mpg
In [5]:
basePlot +
    theme(
        stripTextY = elementText(
            angle = 0,
            size = 24,
            face = "bold",
            color = "darkblue / 0.4"
        )
    ) +
    ggtitle("Natural strip text direction")
Out[5]:
3 4 4 3 4 6 14 16 18 20 22 24 3 4 8 Natural strip text direction wt mpg

Rotating Horizontal and Vertical Strip Labels

In [6]:
val sales = mapOf(
    "region" to listOf("North", "North", "South", "South", "North", "North", "South", "South"),
    "segment" to listOf("Retail", "Retail", "Retail", "Retail", "Online", "Online", "Online", "Online"),
    "quarter" to listOf("Q1", "Q2", "Q1", "Q2", "Q1", "Q2", "Q1", "Q2"),
    "value" to listOf(12, 15, 9, 11, 16, 18, 13, 17)
)

letsPlot(sales) { x = "quarter"; y = "value" } +
    geomBar(stat = Stat.identity, fill = "steelblue", color = "white") +
    facetGrid(x = "region", y = "segment") +
    themeBW() +
    theme(
        stripTextX = elementText(angle = 35, size = 13, face = "bold", color = "steelblue"),
        stripTextY = elementText(angle = 35, size = 13, face = "bold", color = "salmon"),
        stripBackgroundX = elementRect(fill = "aliceblue", color = "cadetblue"),
        stripBackgroundY = elementRect(fill = "lemonchiffon", color = "tan")
    ) +
    ggsize(700, 420) +
    ggtitle("Custom angles for facet strip labels")
Out[6]:
0 5 10 15 North South Online Q1 Q2 0 5 10 15 Q1 Q2 Retail Custom angles for facet strip labels value quarter