Rotate Strip Text in ggplot2
Asked Answered
P

3

12

I'm having a hard time figuring out how to rotate the strip.text attribute in theme from ggplot2. I'm using R version 3.4.2 and ggplot2 version 2.2.1.

Below is the data for the MWE.

> dput(dd)
structure(list(type = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 
4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L), .Label = c("blossum", 
"happy", "rugged", "theatre"), class = "factor"), min = c(3, 
2, 4, 6, 3, 2, 4, 6, 3, 2, 4, 6, 3, 2, 4, 6, 3, 2, 4, 6), max = c(8, 
3, 7, 9, 8, 3, 7, 9, 8, 3, 7, 9, 8, 3, 7, 9, 8, 3, 7, 9), avg = c(5, 
1, 3, 3, 5, 1, 3, 3, 5, 1, 3, 3, 5, 1, 3, 3, 5, 1, 3, 3), y = c(1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 
5L, 5L, 5L)), .Names = c("type", "min", "max", "avg", "y"), row.names = c(NA, 
20L), class = "data.frame")

Now when using the element_text() attribute, I can color the strip.text but cannot get the angle to change. I want the strip (facet) names to run horizontally on the left. Here is the code:

library(ggplot2)
ggplot(dd, aes(x = y, ymin = min, ymax = max, y = avg)) +
  facet_wrap(~ type, ncol = 1, strip.position = "left") +
  geom_ribbon(aes(x = y, ymin = min, ymax = max),
              color='black', fill='gray70') + coord_flip() +
  theme(strip.background = element_blank(),
        strip.text = element_text(angle=90, color='blue4'),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        axis.line.y = element_blank())

And that produces this graph: picture of the graph

However, this is not what I am wanting, as their is no difference when the angle is changed in strip.text = element_blank(angle=90, color='blue4').

Powell answered 20/2, 2018 at 19:29 Comment(1)
In ggplot2 version 3.3.2, and R 3.6.2, the answer above no-longer works for me. The argument: strip.text.y must be replaced with strip.text.y.left, and the angle argument is no longer offset by 180 degrees. This question is also very similar to this question: #40484590 I'm unable to post this as a comment due to low reputation.Aaron
P
13

Nevermind I figured it out by using strip.text.y = element_text(angle = 180). Not sure why strip.text alone does not work.

Powell answered 20/2, 2018 at 19:31 Comment(1)
This answser does not work any more. See https://mcmap.net/q/899168/-rotate-strip-text-in-ggplot2Ahner
A
14

Use strip.text.y.left now.
Add angle = 0 as an argument.

ggplot(dd, aes(x = y, ymin = min, ymax = max, y = avg)) +
  facet_wrap(~ type, ncol = 1, strip.position = "left") +
  geom_ribbon(aes(x = y, ymin = min, ymax = max),
              color='black', fill='gray70') + coord_flip() +
  theme(
    strip.background = element_blank(),
    strip.text.y.left = element_text(angle = 0, color = 'blue4'), #THE LINE THAT IS EDITED
    axis.title.y = element_blank(),
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank(),
    axis.line.y = element_blank())

enter image description here

Answer taken from this comment

Ahner answered 21/9, 2022 at 12:30 Comment(0)
P
13

Nevermind I figured it out by using strip.text.y = element_text(angle = 180). Not sure why strip.text alone does not work.

Powell answered 20/2, 2018 at 19:31 Comment(1)
This answser does not work any more. See https://mcmap.net/q/899168/-rotate-strip-text-in-ggplot2Ahner
S
1
ggplot(dd, aes(x = y, ymin = min, ymax = max, y = avg)) +
  facet_wrap(~ type, ncol = 1, strip.position = "left") +
  geom_ribbon(aes(x = y, ymin = min, ymax = max),
    color='black', fill='gray70') + coord_flip() + 
  theme(strip.background = element_blank(),
    strip.text.y = element_text(angle=180, color='blue4'),
    axis.title.y = element_blank(),
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank(),
    axis.line.y = element_blank()
  )
Sharp answered 21/2, 2018 at 14:48 Comment(1)
This answser does not work any more. See https://mcmap.net/q/899168/-rotate-strip-text-in-ggplot2Ahner

© 2022 - 2024 — McMap. All rights reserved.