knitr::opts_chunk$set(
  collapse = TRUE,
  echo = F,
  warning = F,
  comment = "#>",
  fig.height=4,
  fig.width = 6,
  fig.align = "center",
  eval = T
)
library(magrittr)
library(KeyWestMopeds)
library(dplyr)
library(ggplot2)
library(tidyr)
data("mv_crashes_all")
data("fl_city_codes")
data("fl_city_pop")
mv_crashes_city <-
  mv_crashes_all %>% 
  dplyr::mutate(city_code = stringr::str_sub(dhscntycty, start = -2)) %>%
        dplyr::filter(!grepl("00", city_code)) %>%
        dplyr::filter(in_town_flag == "Yes") %>%
        dplyr::select(-city_code) %>%
        dplyr::rename(year = calendar_year, city_code = dhscntycty) %>%
        dplyr::mutate(year = year %>% as.integer)

Florida City Crashes by Year and Injury

  mv_crashes_city %>% 
  dplyr::group_by(year) %>% 
  dplyr::summarise(total_crashes  = n(),
            total_persons = sum(total_persons),
            total_injured = sum(number_of_injured),
            total_serious = sum(number_of_serious_injuries),
            total_killed = sum(number_of_killed)
            ) %>%
  kableExtra::kbl() %>%
  kableExtra::kable_material(c("striped", "hover", "condensed"))

Top 100 Florida Cities for Total Crashes in 2019

 mv_crashes_city_1 <-
  mv_crashes_city %>% 
  dplyr::filter(year == 2019) %>% 
  dplyr::group_by(year, city_code) %>% 
  dplyr::summarize(tot_crashes = n()) %>% 
  dplyr::arrange(desc(tot_crashes)) %>% 
  dplyr::left_join(., fl_city_codes, by = "city_code") %>% 
  dplyr::select(year, city_code, city, tot_crashes) %>% 
  dplyr::slice_head(n = 100)
DT::datatable(mv_crashes_city_1)
#eliminate "." in census dataset
fl_city_pop$city <- gsub("\\.", "", fl_city_pop$city)
fl_city_codes$city <- gsub("^FT", "FORT", fl_city_codes$city)

Top 100 Florida Cities for Crashes Per 100,000 Residents

From Florida Traffic Crashes Report Manual, there were 634 city codes in fl_city_codes. Some cities have multiple codes in fl_city_codes. For example, Jacksonville is coded as both "0225" and "0238" and Miami as "0166" and "0191". The U.S. Census bureau listed 412 unique cities in fl_city_pop. Additionally, the U.S. Census bureau also used "." to abbreviate "Saint" to "St." so the period was eliminated from the city field. When the two dataframes were merged, there were 383 successful matches.

fl_city_codes_pop <- 
  dplyr::left_join(fl_city_codes, fl_city_pop, by = "city") %>% 
  #dplyr::filter(year == 2019) %>% 
  dplyr::mutate(year = year %>% as.integer)
mv_crashes_city %>% 
dplyr::filter(year == 2019) %>% 
dplyr::left_join(., fl_city_codes_pop) %>% 
dplyr::group_by(year, city, pop) %>% 
dplyr::summarize(tot_crashes = n()) %>% 
dplyr::arrange(desc(tot_crashes)) %>% 
dplyr::select(year, city, tot_crashes, pop) %>% 
dplyr::mutate(crashes_per_100k = `/`(tot_crashes, pop) * 1e5,
         crashes_per_100k = crashes_per_100k %>% round(1)) %>% 
dplyr::select(year, city, tot_crashes, pop, crashes_per_100k) %>% 
dplyr::arrange(desc(crashes_per_100k)) %>% 
dplyr::filter(pop > 10000) %>% 
dplyr::ungroup() %>% 
dplyr::slice_head(n = 100) %>% 
DT::datatable()

Florida City Crashes by Day

Data was available for the calendar years 2012 through 2019. The crashes were summed by date and faceted by year. Years 2012 through 2014 were omitted to expand the vertical plotting area and make the variation more obvious. The number of crashes decreased beginning in 2019.

mv_crashes_city %>% 
dplyr::mutate(date = stringr::str_sub(crash_date, start = 1, end = 10),
       date = as.Date(date, format = "%Y/%m/%d"),
       year = stringr::str_sub(crash_date, start = 1, end = 4),
       year = as.integer(year)) %>% 
dplyr::filter(year %in% c(2015:2019)) %>% 
dplyr::group_by(date, year) %>% 
dplyr::count() %>% 
ggplot2::ggplot() +
aes(date, n) + 
geom_line(alpha = .5) + 
facet_wrap(.~year, scales = "free_x", ncol = 1, strip.position = "right") +
geom_smooth(span = .1, method = 'loess', formula = 'y ~ x') +
theme_minimal() +
  scale_x_date(name = "") +
  scale_y_continuous(name = "") +
  labs(title = "Florida Motorvehicle Crashes in Cities\n 2015 - 2020")

Top 100 Florida Cities by Motorcycle Crashes

mc_crashes_city <-
  mv_crashes_city %>% 
  dplyr::filter(motorcycle_involved == "Yes" & year == 2019) %>% 
  dplyr::left_join(., fl_city_codes_pop) %>% 
  dplyr::group_by(year, city_code, city) %>% 
  dplyr::summarize(tot_crashes = n()) %>% 
  dplyr::arrange(desc(tot_crashes)) %>% 
  dplyr::ungroup() %>% 
  dplyr::slice_head(n = 100)
DT::datatable(mc_crashes_city)
#write file to public datasets
#file <- "~/Dropbox/public/datasets/2021-08-20-fl_city_motorcycle_crashes_total.csv"
#write.csv(mv_crashes_city_1, file = file, row.names = F)

Top 100 Florida Cities for Motorcycle Crashes Per 100,000 Residents

mc_crashes_city_pc <-
  mv_crashes_city %>% 
  dplyr::filter(motorcycle_involved == "Yes" & year == 2019) %>% 
  dplyr::left_join(., fl_city_codes_pop) %>%
  dplyr::group_by(year, city_code, city, pop) %>% 
  dplyr::summarize(tot_crashes = n()) %>% 
  dplyr::filter(pop > 20000) %>% 
  dplyr::mutate(crashes_per_100k = (tot_crashes / pop) * 1e5) %>% 
  dplyr::mutate(crashes_per_100k = crashes_per_100k %>% round(1)) %>% 
  dplyr::arrange(desc(crashes_per_100k)) %>% 
  dplyr::ungroup() %>% 
  dplyr::slice_head(n = 100) 
DT::datatable(mc_crashes_city_pc)
#file <- "~/Dropbox/public/datasets/2021-08-20-fl_city_motorcycle_crashes_per_100k.csv"
#write.csv(mc_crashes_city_pc, file = file, row.names = F)
 mc_crashes_beach <-
  mv_crashes_city %>% 
  dplyr::filter(motorcycle_involved == "Yes") %>%
  dplyr::left_join(., fl_city_codes_pop) %>%
  dplyr::group_by(year, city_code, city, pop) %>% 
  dplyr::summarize(tot_crashes = n()) %>% 
  tidyr::drop_na() %>% 
  dplyr::ungroup() %>% 
  mutate(beach = "No")
mc_crashes_beach$beach[grep("BEACH", mc_crashes_beach$city)] <- "Yes"
mc_crashes_beach_1 <-
  mc_crashes_beach %>% 
  mutate(beach = factor(beach)) %>% 
  group_by(year, beach) %>% 
  summarize(tot_crashes = sum(tot_crashes),
            tot_pop = sum(pop)) %>% 
  mutate(mc_crashes_per_100k = tot_crashes / tot_pop,
         mc_crashes_per_100k = (mc_crashes_per_100k * 1e5) %>% round(1))
 p1 <-
   mc_crashes_beach_1 %>% 
   ggplot() +
   aes(year, mc_crashes_per_100k, group = beach, color = beach) +
   geom_line() +
   geom_point() + 
   scale_y_continuous(name = "per 100,000 residents") +
   scale_x_continuous(name = "") +
   labs(title = "Florida Motorcycle Accident Rates by Location") +
   theme_light()
 p1
 filename <- "../../blogdown-default/content/post/2021-08-09-motorcycle-and-moped-accidents-in-florida/beach-accident-rate.jpg"
 ggsave(p1, filename = filename, width = 6, height = 4, units = "in", dpi = 300)


RobWiederstein/KeyWestMopeds documentation built on Dec. 18, 2021, 10:53 a.m.