Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

geom_point how do i add legends when i have two y variable

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
may i ask how do i add legends to delay and delay2 in the graph thank you.
enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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"))

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading