1

I want to have the strip text along the vertical axis in facet_grid() in ggplot2 such that

  • the text reads horizontally (i.e., angle = 0),
  • the text goes over multiple lines, and
  • part of the text is italicized

When I use labeller = label_parsed, however, apparently the final line of the strip text is vertically centered, and the entire strip text is not well-adjusted positionally.

set.seed(1)
d <- data.frame(
  x = rnorm(20),
  y = rnorm(20),
  f1 = rep(letters[1:2], each = 10),
  f2 = rep(letters[1:2], 10)
)
levels(d$f2) <- c(
  "a" = expression(paste("short label")),
  "b" = expression(paste("Long and\npartly italicized\nlabel over\nmultiple", italic("lines")))
)
ggplot(d, aes(x, y)) +
  geom_point() +
  facet_grid(f2 ~ f1, scale = "free_y", labeller = label_parsed) +
  theme(strip.text.y = element_text(angle = 0)) 

Problematic position of strip label

Is there a way to fix this? I thought about using vjust but it unnecessarily adjusts the other label ("short label" above) as well.

Akira Murakami
  • 463
  • 1
  • 4
  • 14
  • Often times the answer is to use `atop()`, although in my experience that can work best when you only have a single line break. See an example of how to do this [here](https://stackoverflow.com/a/16490888/2461552). Also see info discussed [here](https://stackoverflow.com/a/13225241/2461552). – aosmith May 01 '19 at 18:02
  • Thank you! Using `atop()` indeed looks like a way forward, though as you say vertical spacing does not work as nicely as I wish when the label runs more than two lines. – Akira Murakami May 01 '19 at 21:10

0 Answers0