1

This is the code I used:

ggplot(Delays2006, aes(Month)) + geom_point(aes(y = delay), color = "blue", size = 2) 
+ geom_point(aes(y=delay2)) + scale_x_discrete(limits=(month.name))
+ labs(x= "Months", y = "Delays in minutes") + ggtitle(" Flight Data 2006")

This is the output:

enter image description here

May I ask how do I add legends to delay and delay2 in this graph. Thank you.

Phil
  • 7,287
  • 3
  • 36
  • 66
jerry
  • 21
  • 4

1 Answers1

1

If you want to have a legend you have to map on aesthetics, i.e. do geom_point(aes(y = delay, color = "delay")) + geom_point(aes(y=delay2, color = "delay2")). Colors and labels could then be set via scale_color_manual.

Using mtcars as example data:

library(ggplot2)

ggplot(mtcars, aes(mpg)) +
  geom_point(aes(y = hp, color = "hp")) +
  geom_point(aes(y = cyl, color = "cyl")) +
  scale_color_manual(values = c(hp = "blue", cyl = "green"), labels = c(hp = "Horse Power", cyl = "Cylinders"))

stefan
  • 90,330
  • 6
  • 25
  • 51