How to add colour and legend in multiple line plot

2019-05-30 19:45发布

问题:

I am plotting two lines on same plot with sme x axis by following lines. i am implementing the lower line but unable to see colors and legend

ggplot(final, aes(x = Date)) + geom_line(aes(y = cocastock)) + geom_line(aes(y = procterstock))  + scale_color_manual(values = c(cocastock = '#008B00', procterstock = '#FFFFFF'))

also tried

ggplot(final, aes(x = Date)) + geom_line(aes(y = cocastock)) + geom_line(aes(y = procterstock))  + scale_color_manual(values = c('#008B00','#FFFFFF'))

but dosen't work

回答1:

scale_colour_manual only works when you have specified colour in aes, hence you need:

ggplot(final, aes(x = Date)) + 
  geom_line(aes(y = cocastock, colour = "cocastock")) + 
  geom_line(aes(y = procterstock, colour = "procterstock"))  + 
  scale_color_manual(values = c(cocastock = '#008B00', procterstock = '#FFFFFF'))


标签: r ggplot2 colors