在ggplot中使用两个单独的数据帧时命名颜色线

时间:2018-05-14 14:21:28

标签: r ggplot2

我正在从两个不同的数据库中绘制以下线图:

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

图表很好,但是,我试图命名两条彩色线条(多年,一条是2015年,另一条是2016年),但我似乎无法让它发挥作用。

感谢您的帮助。

0 个答案:

没有答案