【问题标题】:How to put geom_vline manually in ggplot legend (r)如何将 geom_vline 手动放入 ggplot 图例 (r)
【发布时间】:2022-11-21 15:37:39
【问题描述】:

我想制作一个带有垂直线的图来表示政策的开始。如何将这条垂直线添加到情节的图例中?

例子:

df <- tibble(year = 2010:2014, value = c(1,2,3,3,4))

df %>% 
    ggplot(aes(x = year, y = value)) + 
    geom_line() + 
    geom_vline(xintercept = 2012, linetype = 4)

我试过 show_legend = T 但没有任何反应。这是情节

【问题讨论】:

    标签: r ggplot2 geom-vline


    【解决方案1】:

    你必须把xintercept = 2012, linetype = "dotdash"部分放在aes()里面,就像下面的代码一样来显示图例

    df %>% 
      ggplot(aes(x = year, y = value)) + 
      geom_line() + 
      geom_vline(aes(xintercept = 2012, linetype = "dotdash"))+
      scale_linetype_manual(values=c("dotdash"))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-04
      • 1970-01-01
      • 2021-11-16
      • 2019-07-21
      • 1970-01-01
      相关资源
      最近更新 更多