0

I am plotting the following line plot from two different databases:

ggplot() + 
  geom_line(data = Hour_of_accident_15, mapping = aes(x=hour_of_crash, y = n), 
            color = "green") + 
  geom_line(data = Hour_of_accident_16, mapping = aes(x=hour_of_crash, y = n), 
            color = "red") + 
  theme(plot.title = element_text(face = "bold", hjust = 0.5, size = 15)) + 
  scale_x_discrete(limit = 0:24, 
                   labels = paste(c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
                                    "11", "12", "13", "14", "15", "16", "17", "18", "19", 
                                    "20", "21", "22", "23", "24"))) + 
  labs(x = "Hours", 
       y="Number of car accident", 
       title = "Number of fatal car accident per hour") 

The graph comes out fine, however, I am trying to name the two colored lines (as years, one being 2015 and the other 2016) but I cannot seem to make it work.

Thank you for your help.

Axeman
  • 32,068
  • 8
  • 81
  • 94
  • yes exactly, sorry – Charlotte May 14 '18 at 14:36
  • 2
    No need to apologize! Did the linked questions solve your problem? – Axeman May 14 '18 at 14:39
  • 1
    actually I had already tried that link and it did not work.. I figured it's because I use two different data frames – Charlotte May 14 '18 at 15:03
  • 1
    You'll have to be specific, divibisan and I posted different links. – Axeman May 14 '18 at 15:05
  • Would this work: scale_color_discrete(name = "Years", labels = c("2015", "2016")) – SCDCE May 14 '18 at 15:08
  • Perhaps: `ggplot(mapping = aes(x=hour_of_crash, y = n)) + geom_line(aes(color = "2015"), Hour_of_accident_15) + geom_line(aes(color = "2016"), Hour_of_accident_16) + scale_color_manual(values = c(2015 = "green", 2016 = "red"))`. Can't test without any data, or knowing if you actually are looking for a legend. – Axeman May 14 '18 at 15:09

0 Answers0