Setup

#reads in Washington DC traffic incident dataset
crash_df <- read.csv("Crashes_in_DC.csv")

Data Preprocessing

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

Geospatial Visualizations

Incident Heatmap

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

2024 Incident Density

create_heatmap(crash_df,2024)

2023 Incident Density

create_heatmap(crash_df,2023)

2022 Incident Density

create_heatmap(crash_df,2022)

Incident Type Maps

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

Incidents with a Pedestrian Fatality (2015-2024)

create_map(crash_df,"FATAL_PEDESTRIAN",c(2015:2024))

Incidents with a Bicyclist Fatality (2015-2024)

create_map(crash_df,"FATAL_BICYCLIST",c(2015:2024))

Incidents with a Driver Fatality (2015-2024)

create_map(crash_df,"FATAL_DRIVER",c(2015:2024))