在ggplot2中旋转剥离文本

时间:2018-02-20 19:29:29

标签: r ggplot2 facet-wrap

我很难弄清楚如何从strip.text中转动theme中的ggplot2属性。我使用R版本3.4.2和ggplot2版本2.2.1。

以下是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")

现在使用element_text()属性时,我可以为strip.text着色,但无法更改角度。我希望strip(facet)名称在左侧水平运行。这是代码:

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())

这就产生了这个图: picture of the graph

但是,这不是我想要的,因为当strip.text = element_blank(angle=90, color='blue4')中的角度发生变化时它们没有区别。

2 个答案:

答案 0 :(得分:2)

没关系我用strip.text.y = element_text(angle = 180)弄明白了。不确定为什么单独strip.text不起作用。

答案 1 :(得分:0)

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()
  )