Andrew Hamel
Andrew Hamel

Reputation: 366

Move facet_wrap label before y axis label

Suppose I have this dataframe:

x <- seq(-10, 9.9, by = .1)
y <- seq(0, 19.9, by = .1)
z <- rep(c(1,2,3,4),each=50)

df <- data.frame(col_x = x,
                 col_y = y,
                 col_z = z)

and I plot it as such:

ggplot(df, aes(x = col_x, y = col_y))+
  geom_point()+
  facet_wrap(~col_z, strip.position = "left", nrow = 4)+
  theme(axis.title.y = element_blank(),
        strip.text.y.left = element_text(angle=0))

However, in this plot, the facet labels are after the y axis. How do I make it so that the facet labels come before the y axis. In other words, they are further left?

enter image description here

Thanks in advance!

Upvotes: 2

Views: 855

Answers (1)

Martin Gal
Martin Gal

Reputation: 16998

You could try this:

library(ggplot2)

ggplot(df, aes(x = col_x, y = col_y))+
  geom_point() +
  facet_wrap(~col_z, strip.position = "left", nrow = 4) +
  theme(axis.title.y = element_blank(),
        strip.text.y.left = element_text(angle=0),
        strip.placement = "outside")

The strip.placement = "outside" argument moves the facet labels next to the ticks (which I think is the better placement). So your plot looks like

enter image description here

Upvotes: 3

Related Questions