Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotate Strip Text in ggplot2

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').

like image 371
sempervent Avatar asked Feb 20 '18 19:02

sempervent


3 Answers

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

like image 120
sempervent Avatar answered Nov 15 '22 20:11

sempervent


Use strip.text.y.left now, not strip.text.y.
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 MUST BE 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

like image 4
Julien Avatar answered Nov 15 '22 20:11

Julien


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()
  )
like image 1
yang Avatar answered Nov 15 '22 20:11

yang