Home > OS >  How do I update the legend labels in a time series chart?
How do I update the legend labels in a time series chart?

Time:10-30

Is it possible to add a label to the geom_line() lines to update the series label in the legend?

Instead of the legend showing the hex colour code, I would like it to show #eb6b63 -> Revenue #407edb -> Profit

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

The above code produces the below plot.

Any help would be greatly appreciated.

enter image description here

CodePudding user response:

Try using 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'))

Usually it is preferable to reshape your data into a long format so you can set the color aesthetic with a variable rather than manually.

  • Related