【问题标题】:How can I add a legend for error bars in ggplot?如何在 ggplot 中为误差线添加图例?
【发布时间】:2022-05-01 12:03:11
【问题描述】:

一天以来,我一直在尝试修改我认为在 GGPlot 上简单明了的图例,但没有取得太大进展(我在 Stackexchange 上进行了广泛搜索,但看不到明显的相似之处)。

简单地说:我展示了一个收入与预测的条形图,其中水平线代表预测。我设法获得了带有零宽度误差线的水平条。这些列被捕获在图例中,但我不知道如何为错误栏添加图例:

这是一个可重现的示例。我在最终图像中添加了我需要的完整美学设置,以防它们引起我遗漏的一些冲突:

df = data.frame(Team=c("Dunder Mifflin","Saber","Wernham Hogg",
                       "OsCorp","Queen Industries","Stark Industries"),
                Industry=c("Paper","Paper","Paper",
                           "Tech","Tech","Tech"),
                Revenue=c(20,100,10,
                          50,150,200),
                Forecast=c(30,120,5,
                           70,120,120))

g1 = ggplot(df) + 
  geom_col(aes(x=Team,y = Revenue,fill=Industry)) +
  geom_errorbar(aes(x=Team,y=Forecast, ymax=Forecast, ymin=Forecast)) +
  # Everything from here probably irrelevant aesthetic settings,
  # included in case there is some clash I am unaware of
  scale_fill_hue(c = 40) + 
  facet_grid(cols = vars(Industry),
             scales = "free_x",
             space = "free_x",
             switch="x") +
  theme_bw() + 
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        axis.line = element_line(),
        strip.placement = "outside",
        strip.text.x = element_text(size=10),
        strip.background.x = element_rect(colour = "grey", fill = "white"),
        text=element_text(family="Roboto",face="bold"))

print(g1)

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    试试这个技巧。您可以使用geom_line(),传递一个零值并添加选项color='forecast'。这将创造一个类似于预期的传说。代码如下:

    library(ggplot2)
    #Plot
    ggplot(df) + 
      geom_col(aes(x=Team,y = Revenue,fill=Industry)) +
      geom_line(aes(x=Team,y=0,color='forecast',group=1))+
      geom_errorbar(aes(x=Team,y=Forecast, ymax=Forecast, ymin=Forecast)) +
      # Everything from here probably irrelevant aesthetic settings,
      # included in case there is some clash I am unaware of
      scale_fill_hue(c = 40) +
      scale_color_manual(values='black')+
      facet_grid(cols = vars(Industry),
                 scales = "free_x",
                 space = "free_x",
                 switch="x") +
      theme_bw() + 
      theme(panel.grid.major = element_blank(),
            panel.grid.minor = element_blank(),
            panel.border = element_blank(),
            axis.line = element_line(),
            strip.placement = "outside",
            strip.text.x = element_text(size=10),
            strip.background.x = element_rect(colour = "grey", fill = "white"),
            text=element_text(family="Roboto",face="bold"))+
      labs(color='')
    

    输出:

    【讨论】:

    • 嗨@Duck 非常感谢。这按预期工作,除了现在在 y=0 处有一条黑色水平线跨越两个方面(我完全复制了您的代码)。我正在使用 ggplot2_3.3.2 和 R 3.6.1
    • @SueDohNimh 也许是主题,你能否重新启动 R 并再次运行代码仅加载解决方案中的包?
    • 嗯重新启动了 R(确保 env 是干净的),仍然得到相同的结果。我可以并且会尝试对此进行一些修改以查看发生了什么,但同时我只想添加一个简单的解决方法,即设置 y=-10 并将 y 轴固定到位:)
    • @SueDohNimh 这很聪明:) 我会赞成你的问题,因为它是一个好问题! +1
    【解决方案2】:

    您可以使用 coord_cartesian 来限制坐标轴并更改线以使其低于 y=0,因此如下所示:

    ggplot(df) + 
    geom_col(aes(x=Team,y = Revenue,fill=Industry)) +
    geom_line(aes(x=Team,y=-10,color='forecast',group=1))+
    geom_errorbar(aes(x=Team,y=Forecast, ymax=Forecast, ymin=Forecast)) +
    scale_fill_hue(c = 40) +
    scale_color_manual(values='black')+
    facet_grid(cols = vars(Industry),
             scales = "free_x",
             space = "free_x",
             switch="x") +
    theme_bw() + 
    theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        axis.line = element_line(),
        strip.placement = "outside",
        strip.text.x = element_text(size=10),
        strip.background.x = element_rect(colour = "grey", fill = "white"),
        text=element_text(family="Roboto",face="bold"))+
    labs(color='')+
    coord_cartesian(ylim=c(0,200))
    #assuming 200 is your ymax
    

    【讨论】:

      猜你喜欢
      • 2019-09-17
      • 2016-01-03
      • 2021-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-10
      相关资源
      最近更新 更多