#reads in Washington DC traffic incident dataset
crash_df <- read.csv("Crashes_in_DC.csv")
The year is extracted from the report date. 2009 is the first year with complete data, so incidents from a prior report year are filtered out. Additionally, records with coordinates outside of the DC area are removed.
#adjusts format of report date variable
crash_df$report_date <- ymd_hm(substr(crash_df$REPORTDATE,1,16))
#creates report year variable
crash_df$report_year <- year(crash_df$report_date)
#filters incident data
crash_df <- crash_df %>% filter(report_year >= 2009, LONGITUDE < 0,!is.na(LONGITUDE),!is.na(LATITUDE))
These heatmaps show the density of all traffic incidents in the DC area. Each map is limited to a single year, allowing for comparison in incident patterns over time.
create_heatmap <-function(data,year){
data %>%
filter(report_year == year) %>%
leaflet() %>%
addTiles() %>% # Add a base map
setView(lng = mean(crash_df$LONGITUDE, na.rm = TRUE),
lat = mean(crash_df$LATITUDE, na.rm = TRUE),
zoom = 12) %>%
addHeatmap(
lng = ~LONGITUDE,
lat = ~LATITUDE,
blur = 15, max = 1, radius = 9
) %>%
addLegend("bottomright",
title = "Traffic Incident Density",
colors = c("blue", "green", "yellow", "red"),
labels = c("Low", "Medium", "High", "Very High"))
}
create_heatmap(crash_df,2024)
create_heatmap(crash_df,2023)
create_heatmap(crash_df,2022)
These dot maps show the location of incidents that resulted in a fatality.
create_map <- function(data,var,years){
data %>%
filter(data[[var]] == 1, report_year %in% years) %>%
leaflet() %>%
addTiles() %>% # Add a base map
setView(lng = mean(crash_df$LONGITUDE, na.rm = TRUE),
lat = mean(crash_df$LATITUDE, na.rm = TRUE),
zoom = 12) %>%
addCircles(radius = 30,color = "red")
}
create_map(crash_df,"FATAL_PEDESTRIAN",c(2015:2024))
create_map(crash_df,"FATAL_BICYCLIST",c(2015:2024))
create_map(crash_df,"FATAL_DRIVER",c(2015:2024))