Counts"

Generating Counts

A first step in many transportation safety analyses involves counting the number of relevant crashes, fatalities, or people involved. counts() lets users specify what to count, where to count them (rural/urban and/or in specified states or regions), who to include, the interval over which to count (annually or monthly), and factors involved in the crashes. It returns a simple tibble that can be easily piped into ggplot() to quickly visualize counts.

First we load the required libraries:

library(rfars)
library(dplyr)
library(ggplot2)

Then pull a year of FARS data for Virginia:

myFARS <- get_fars(years = 2021, states = "VA", proceed = T)

Then we can use counts() to reduce the data to desired counts.

Here we count crashes:

my_counts <- counts(
  myFARS,
  what = "crashes",
  interval = c("month")
  )

This returns the following dataframe:

knitr::kable(my_counts, format = "html")

Which we can graph:

my_counts %>%
  ggplot(aes(x=date, y=n, label=scales::comma(n))) + 
    geom_col() + 
    geom_label(vjust=1.2) +
    labs(x=NULL, y=NULL, title = "Fatal Crashes in Virginia")

We could alternatively count fatalities:

counts(
  myFARS,
  what = "fatalities",
  interval = c("month")
  ) %>%
  ggplot(aes(x=date, y=n, label=scales::comma(n))) + 
    geom_col() + 
    geom_label(vjust=1.2) +
    labs(x=NULL, y=NULL, title = "Fatalities in Virginia")

Or fatalities involving speeding:

counts(myFARS,
       what = "fatalities",
       interval = c("month"),
       involved = "speeding"
       ) %>%
  ggplot(aes(x=date, y=n, label=scales::comma(n))) + 
    geom_col() + 
    geom_label(vjust=1.2) +
    labs(x=NULL, y=NULL, title = "Speeding-Related Fatalities in Virginia")

Or fatalities involving speeding in rural areas:

counts(myFARS,
       what = "fatalities",
       where = list(urb="rural"),
       interval = c("month"),
       involved = "speeding"
       ) %>%
  ggplot(aes(x=date, y=n, label=scales::comma(n))) + 
    geom_col() + 
    geom_label(vjust=1.2) +
    labs(x=NULL, y=NULL, title = "Speeding-Related Fatalities in Rural Virginia")

We can use compare_counts() to quickly produce comparison graphs.

Here we compare speeding-related fatalities in rural and urban areas:

compare_counts(
  df = myFARS,
  interval = "month",
  involved = "speeding",
  what = "fatalities",
  where = list(urb="rural"),
  where2 = list(urb="urban")
  ) %>%
  ggplot(aes(x=date, y=n, label=scales::comma(n))) + 
    geom_col() + 
    geom_label(vjust=1.2) +
    facet_wrap(.~urb) +
    labs(x=NULL, y=NULL, title = "Speeding-Related Fatalities in Virginia", fill=NULL)

And here we compare speeding-related crashes to those related to distraction:

compare_counts(
  df = myFARS,
  interval = "month",
  involved = "speeding",
  involved2 = "distracted driver",
  what = "crashes",
  ) %>%
  ggplot(aes(x=date, y=n, label=scales::comma(n))) + 
    geom_col() + 
    geom_label(vjust=1.2) +
    facet_wrap(.~involved) +
    labs(x=NULL, y=NULL, title = "Speeding- and Distraction-Related Crashes in Virginia", fill=NULL)

See the documentation for more information on the available options. counts() compare_counts()



Try the rfars package in your browser

Any scripts or data that you put into this service are public.

rfars documentation built on June 22, 2024, 11:42 a.m.