Reputation: 85
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.
Upvotes: 0
Views: 228
Reputation: 18692
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.
Upvotes: 1