【问题标题】:How do I update the legend labels in a time series chart?如何更新时间序列图表中的图例标签?
【发布时间】:2021-12-14 22:47:13
【问题描述】:

是否可以在 geom_line() 行中添加标签以更新图例中的系列标签?

我希望它显示的不是显示十六进制颜色代码的图例 #eb6b63 -> 收入 #407edb -> 利润

ggplot(RevGroup, aes(x=MonthYear)) +
  geom_line(aes(y=sum_rev_month, color = '#eb6b63'))+
  geom_line(aes(y=sum_prof_month, color = '#407edb')) +
  myTheme+
  labs(colour = "Amount($)", title = 'Yearly Revenue and Profit trends', x = 'Year', y = 'Amount($)')

上面的代码产生了下面的情节。

任何帮助将不胜感激。

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    尝试使用scale_color_manual

    ggplot(RevGroup, aes(x=MonthYear)) +
      geom_line(aes(y=sum_rev_month, color = "Revenue"))+
      geom_line(aes(y=sum_prof_month, color = "Profit")) +
      myTheme +
      labs(colour = "Amount($)", title = 'Yearly Revenue and Profit trends', x = 'Year', y = 'Amount($)') +
      scale_color_manual(values = c('#eb6b63', '#407edb'))
    

    通常最好将数据重新整形为长格式,这样您就可以使用变量而不是手动设置color 美学。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-22
      • 1970-01-01
      • 2023-01-30
      • 2022-01-20
      • 1970-01-01
      • 2022-07-05
      相关资源
      最近更新 更多