在ggplot2中旋转条形文本

问题描述 投票:0回答:3

我很难弄清楚如何从

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
着色,但无法更改角度。我希望条带(面)名称在左侧水平运行。这是代码:

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

这就产生了这个图表:

然而,这不是我想要的,因为当角度改变时,它们没有区别

strip.text = element_blank(angle=90, color='blue4')

r ggplot2 facet-wrap
3个回答
13
投票

没关系,我通过使用

strip.text.y = element_text(angle = 180)
弄清楚了。不知道为什么
strip.text
单独不起作用。


9
投票

立即使用

strip.text.y.left

添加
angle = 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.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())

答案取自此评论


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()
  )
© www.soinside.com 2019 - 2024. All rights reserved.