在ggplot2中旋转图例键

3

我的数据集如下

dput(data2)
structure(list(School = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 
3L, 3L), .Label = c("School1", "School2", "School3"), class = "factor"), 
    Year = c(2015L, 2014L, 2013L, 2015L, 2014L, 2013L, 2015L, 
    2014L, 2013L), Rate = c(70L, 50L, 30L, 80L, 90L, 11L, 60L, 
    50L, 40L)), .Names = c("School", "Year", "Rate"), class = "data.frame", row.names = c(NA, 
-9L))


   School Year Rate
1 School1 2015   70
2 School1 2014   50
3 School1 2013   30
4 School2 2015   80
5 School2 2014   90
6 School2 2013   11
7 School3 2015   60
8 School3 2014   50
9 School3 2013   40

我使用ggplot2绘制这些数据,如下所示

library(ggplot2)
ggplot(data=data2,aes(x=School,y=Rate)) +
  geom_bar(stat = "identity", fill="orange",width = 0.5) + 
  geom_hline(aes(yintercept = 220,color="red"), size = 1) +
  coord_flip() 

我得到了以下图表,现在想要将图例中的线条旋转,如下所示:enter image description here

我在Stack Overflow上看到一些帖子提到图例中的线条为垂直显示,但只有当使用geom_linerange时才会出现这种情况,而我不能在我的示例中使用它。

请问有人可以帮助我理解如何在图例中旋转线条吗?

一个替代方案是使用grid包中的视口来导航视图,然后查看是否能够旋转图例键使用的视口。


Ggplot2有很多令人惊叹的插件,您可以在r-studio中使用它们,使您的生活变得更加轻松。这里您可以找到各种插件,每个插件都针对特定需求进行了优化。特别是在您的情况下,我认为ggedit是您的雷神之锤。它可以帮助您使用所见即所得的工具编辑ggplot图形,并且您将在下一次尝试中节省时间。 希望这可以帮助您! 祝您好运! - Moctar Haiz
2个回答

3

如果你的实际情况需要根据不同的几何图形有更复杂的图例要求,这里的答案可能会有所帮助:如何旋转ggplot2中的图例符号?

话虽如此,如果您只需要为geom_hline层提供一些东西,则与geom_vline关联的图例就足以模拟它:

ggplot(data = data2, aes(x = School, y = Rate)) +
  geom_col(fill = "orange", width = 0.5) + 

  # do not specify color within aes() for geom_hline
  geom_hline(aes(yintercept = 220), color="red", size = 1) +

  # specify color within aes() for geom_vline instead, but don't let the line
  # be invisible with alpha = 0
  geom_vline(aes(xintercept = 1, color = "legend.label"), alpha = 0) +

  # set up the color legend as per normal. it's based on an invisible geom_vline,
  # but is visible with alpha = 1 & size = 1
  scale_color_manual(
    name = "legend.title",
    values = "red",
    guide = guide_legend(override.aes = list(alpha = 1, size = 1))) +

  coord_flip()

绘图

顺便说一句,geom_col()等同于geom_bar(stat = "identity"),看起来更简洁。


2
有一个软件包实现了常见的ggplot2 Geoms、Stats和Positions的水平版本:ggstance 你可以使用它来避免所有绘图的coord_flip,而是使用水平geom条形图,使用xy,以你想要的方式。这样,你就可以使用具有垂直图例的geom_vline
data2 <- tibble::tribble(
  ~School, ~Year, ~Rate,
  "School1", 2015L,   70L,
  "School1", 2014L,   50L,
  "School1", 2013L,   30L,
  "School2", 2015L,   80L,
  "School2", 2014L,   90L,
  "School2", 2013L,   11L,
  "School3", 2015L,   60L,
  "School3", 2014L,   50L,
  "School3", 2013L,   40L
)

library(ggplot2)
library(ggstance) # devtools::install_github("lionel-/ggstance")
#> 
#> Attachement du package : 'ggstance'
#> The following objects are masked from 'package:ggplot2':
#> 
#>     geom_errorbarh, GeomErrorbarh
ggplot(data=data2,aes(x=Rate,y=School)) +
  geom_barh(stat = "identity", fill="orange",width = 0.5) + 
  geom_vline(aes(xintercept = 220,color="red"), size = 1)

创建于2018年6月3日,使用reprex包(v0.2.0)生成。

非常感谢。@Z.Lin - 你的答案相当聪明,我试了一下,但它搞乱了我的图表中的一些其他图例。Moctair - 你的回答将救我的命。我会将以上所有建议结合起来,这应该可以解决我的问题。在尝试了这里的一些答案之后,我不断遇到这个错误 https://github.com/tidyverse/ggplot2/issues/2483,现在正在尝试找到解决方法。 - Vikram
哦!在整个早上寻找如何使斜杆正确定向之后,终于有了一个很棒的解决方案。非常感谢你! - iago

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接