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:
## Warning: package 'ggplot2' was built under R version 4.4.3
annual_counts
rfars
includes annual_counts
, a table of
annual crash counts:
rfars::annual_counts %>%
filter(what == "crashes", involved == "any") %>%
ggplot(aes(x=year, y=n)) +
geom_col() +
facet_wrap(.~source, nrow=1, scales = "free_y") +
labs(title = "Total annual crashes by type (FARS = fatal, CRSS = general)", x=NULL, y=NULL) +
theme_minimal()
rfars::annual_counts %>%
filter(source=="FARS", involved != "any") %>%
ggplot(aes(x=year, y=n)) +
geom_col() +
facet_wrap(.~involved, scales = "free_y") +
labs(title = "Annual fatal crashes by factor involved", subtitle = "Derived from FARS data files", x=NULL, y=NULL) +
theme_minimal() +
theme(plot.title.position = "plot")
rfars::annual_counts %>%
filter(source=="CRSS", involved != "any") %>%
ggplot(aes(x=year, y=n)) +
geom_col() +
facet_wrap(.~involved, scales = "free_y") +
labs(title = "Annual crashes of all severity levels by factor involved", subtitle = "Derived from CRSS data files", x=NULL, y=NULL) +
theme_minimal() +
theme(plot.title.position = "plot")
Generating Custom Counts
We can use get_fars() and then counts() to generate a variety of custom counts. Below we pull two years of data:
## ✓ 2022 data downloaded
## Accident file:
## | | | 0% | |== | 2% | |=== | 4% | |===== | 7% | |====== | 9% | |======== | 11% | |========= | 13% | |=========== | 16% | |============ | 18% | |============== | 20% | |================ | 22% | |================= | 24% | |=================== | 27% | |==================== | 29% | |====================== | 31% | |======================= | 33% | |========================= | 36% | |========================== | 38% | |============================ | 40% | |============================== | 42% | |=============================== | 44% | |================================= | 47% | |================================== | 49% | |==================================== | 51% | |===================================== | 53% | |======================================= | 56% | |======================================== | 58% | |========================================== | 60% | |============================================ | 62% | |============================================= | 64% | |=============================================== | 67% | |================================================ | 69% | |================================================== | 71% | |=================================================== | 73% | |===================================================== | 76% | |====================================================== | 78% | |======================================================== | 80% | |========================================================== | 82% | |=========================================================== | 84% | |============================================================= | 87% | |============================================================== | 89% | |================================================================ | 91% | |================================================================= | 93% | |=================================================================== | 96% | |==================================================================== | 98% | |======================================================================| 100%
## Vehicle file:
## | | | 0% | |= | 1% | |== | 2% | |== | 3% | |=== | 5% | |==== | 6% | |===== | 7% | |====== | 8% | |====== | 9% | |======= | 10% | |======== | 11% | |========= | 12% | |========== | 14% | |========== | 15% | |=========== | 16% | |============ | 17% | |============= | 18% | |============== | 19% | |============== | 20% | |=============== | 22% | |================ | 23% | |================= | 24% | |================== | 25% | |================== | 26% | |=================== | 27% | |==================== | 28% | |===================== | 30% | |===================== | 31% | |====================== | 32% | |======================= | 33% | |======================== | 34% | |========================= | 35% | |========================= | 36% | |========================== | 38% | |=========================== | 39% | |============================ | 40% | |============================= | 41% | |============================= | 42% | |============================== | 43% | |=============================== | 44% | |================================ | 45% | |================================= | 47% | |================================= | 48% | |================================== | 49% | |=================================== | 50% | |==================================== | 51% | |===================================== | 52% | |===================================== | 53% | |====================================== | 55% | |======================================= | 56% | |======================================== | 57% | |========================================= | 58% | |========================================= | 59% | |========================================== | 60% | |=========================================== | 61% | |============================================ | 62% | |============================================= | 64% | |============================================= | 65% | |============================================== | 66% | |=============================================== | 67% | |================================================ | 68% | |================================================= | 69% | |================================================= | 70% | |================================================== | 72% | |=================================================== | 73% | |==================================================== | 74% | |==================================================== | 75% | |===================================================== | 76% | |====================================================== | 77% | |======================================================= | 78% | |======================================================== | 80% | |======================================================== | 81% | |========================================================= | 82% | |========================================================== | 83% | |=========================================================== | 84% | |============================================================ | 85% | |============================================================ | 86% | |============================================================= | 88% | |============================================================== | 89% | |=============================================================== | 90% | |================================================================ | 91% | |================================================================ | 92% | |================================================================= | 93% | |================================================================== | 94% | |=================================================================== | 95% | |==================================================================== | 97% | |==================================================================== | 98% | |===================================================================== | 99% | |======================================================================| 100%
## Person file:
## | | | 0% | |== | 3% | |==== | 5% | |====== | 8% | |======== | 11% | |========= | 14% | |=========== | 16% | |============= | 19% | |=============== | 22% | |================= | 24% | |=================== | 27% | |===================== | 30% | |======================= | 32% | |========================= | 35% | |========================== | 38% | |============================ | 41% | |============================== | 43% | |================================ | 46% | |================================== | 49% | |==================================== | 51% | |====================================== | 54% | |======================================== | 57% | |========================================== | 59% | |============================================ | 62% | |============================================= | 65% | |=============================================== | 68% | |================================================= | 70% | |=================================================== | 73% | |===================================================== | 76% | |======================================================= | 78% | |========================================================= | 81% | |=========================================================== | 84% | |============================================================= | 86% | |============================================================== | 89% | |================================================================ | 92% | |================================================================== | 95% | |==================================================================== | 97% | |======================================================================| 100%
## Multiple Imputation Driver BAC by Crash file:
## | | | 0% | |======== | 11% | |================ | 22% | |======================= | 33% | |=============================== | 44% | |======================================= | 56% | |=============================================== | 67% | |====================================================== | 78% | |============================================================== | 89% | |======================================================================| 100%
## Weather file(s):
## | | | 0% | |======================================================================| 100%
## Crash risk factors:
## | | | 0% | |======================================================================| 100%
## vsoe file:
## | | | 0% | |================== | 25% | |=================================== | 50% | |==================================================== | 75% | |======================================================================| 100%
## distract file:
## | | | 0% | |=================================== | 50% | |======================================================================| 100%
## drimpair file:
## | | | 0% | |=================================== | 50% | |======================================================================| 100%
## factor file:
## | | | 0% | |=================================== | 50% | |======================================================================| 100%
## maneuver file:
## | | | 0% | |=================================== | 50% | |======================================================================| 100%
## violatn file:
## | | | 0% | |=================================== | 50% | |======================================================================| 100%
## vision file:
## | | | 0% | |=================================== | 50% | |======================================================================| 100%
## damage file:
## | | | 0% | |=================================== | 50% | |======================================================================| 100%
## vehiclesf file:
## | | | 0% | |=================================== | 50% | |======================================================================| 100%
## pvehiclesf file:
## | | | 0% | |=================================== | 50% | |======================================================================| 100%
## driverrf file:
## | | | 0% | |=================================== | 50% | |======================================================================| 100%
## PBtype file:
## | | | 0% | |=== | 5% | |====== | 9% | |========== | 14% | |============= | 18% | |================ | 23% | |=================== | 27% | |====================== | 32% | |========================= | 36% | |============================= | 41% | |================================ | 45% | |=================================== | 50% | |====================================== | 55% | |========================================= | 59% | |============================================= | 64% | |================================================ | 68% | |=================================================== | 73% | |====================================================== | 77% | |========================================================= | 82% | |============================================================ | 86% | |================================================================ | 91% | |=================================================================== | 95% | |======================================================================| 100%
## SafetyEq file:
## | | | 0% | |========= | 12% | |================== | 25% | |========================== | 38% | |=================================== | 50% | |============================================ | 62% | |==================================================== | 75% | |============================================================= | 88% | |======================================================================| 100%
## Multiple Imputation Person BAC file:
## | | | 0% | |====== | 9% | |============= | 18% | |=================== | 27% | |========================= | 36% | |================================ | 45% | |====================================== | 55% | |============================================= | 64% | |=================================================== | 73% | |========================================================= | 82% | |================================================================ | 91% | |======================================================================| 100%
## nmcrash file:
## | | | 0% | |======================= | 33% | |=============================================== | 67% | |======================================================================| 100%
## nmimpair file:
## | | | 0% | |======================= | 33% | |=============================================== | 67% | |======================================================================| 100%
## nmprior file:
## | | | 0% | |======================= | 33% | |=============================================== | 67% | |======================================================================| 100%
## nmdistract file:
## | | | 0% | |======================= | 33% | |=============================================== | 67% | |======================================================================| 100%
## drugs file:
## | | | 0% | |================== | 25% | |=================================== | 50% | |==================================================== | 75% | |======================================================================| 100%
## race file:
## | | | 0% | |============== | 20% | |============================ | 40% | |========================================== | 60% | |======================================================== | 80% | |======================================================================| 100%
## personrf file:
## | | | 0% | |======================= | 33% | |=============================================== | 67% | |======================================================================| 100%
## ✓ Flat file constructed
## ✓ Multi_acc file constructed
## ✓ Multi_veh file constructed
## ✓ Multi_per file constructed
## ✓ SOE file constructed
## ✓ Prepared files saved in C:/Users/STEVEJ~1/AppData/Local/Temp/RtmpC8MgaG/FARS data/prepd/2022
## ✓ Codebook file saved in C:/Users/STEVEJ~1/AppData/Local/Temp/RtmpC8MgaG/FARS data/prepd/
## ✓ 2023 data downloaded
## Accident file:
## | | | 0% | |== | 2% | |=== | 4% | |===== | 7% | |====== | 9% | |======== | 11% | |========= | 13% | |=========== | 16% | |============ | 18% | |============== | 20% | |================ | 22% | |================= | 24% | |=================== | 27% | |==================== | 29% | |====================== | 31% | |======================= | 33% | |========================= | 36% | |========================== | 38% | |============================ | 40% | |============================== | 42% | |=============================== | 44% | |================================= | 47% | |================================== | 49% | |==================================== | 51% | |===================================== | 53% | |======================================= | 56% | |======================================== | 58% | |========================================== | 60% | |============================================ | 62% | |============================================= | 64% | |=============================================== | 67% | |================================================ | 69% | |================================================== | 71% | |=================================================== | 73% | |===================================================== | 76% | |====================================================== | 78% | |======================================================== | 80% | |========================================================== | 82% | |=========================================================== | 84% | |============================================================= | 87% | |============================================================== | 89% | |================================================================ | 91% | |================================================================= | 93% | |=================================================================== | 96% | |==================================================================== | 98% | |======================================================================| 100%
## Vehicle file:
## | | | 0% | |= | 1% | |== | 2% | |== | 3% | |=== | 4% | |==== | 6% | |===== | 7% | |====== | 8% | |====== | 9% | |======= | 10% | |======== | 11% | |========= | 12% | |========= | 13% | |========== | 15% | |=========== | 16% | |============ | 17% | |============= | 18% | |============= | 19% | |============== | 20% | |=============== | 21% | |================ | 22% | |================= | 24% | |================= | 25% | |================== | 26% | |=================== | 27% | |==================== | 28% | |==================== | 29% | |===================== | 30% | |====================== | 31% | |======================= | 33% | |======================== | 34% | |======================== | 35% | |========================= | 36% | |========================== | 37% | |=========================== | 38% | |============================ | 39% | |============================ | 40% | |============================= | 42% | |============================== | 43% | |=============================== | 44% | |=============================== | 45% | |================================ | 46% | |================================= | 47% | |================================== | 48% | |=================================== | 49% | |=================================== | 51% | |==================================== | 52% | |===================================== | 53% | |====================================== | 54% | |======================================= | 55% | |======================================= | 56% | |======================================== | 57% | |========================================= | 58% | |========================================== | 60% | |========================================== | 61% | |=========================================== | 62% | |============================================ | 63% | |============================================= | 64% | |============================================== | 65% | |============================================== | 66% | |=============================================== | 67% | |================================================ | 69% | |================================================= | 70% | |================================================== | 71% | |================================================== | 72% | |=================================================== | 73% | |==================================================== | 74% | |===================================================== | 75% | |===================================================== | 76% | |====================================================== | 78% | |======================================================= | 79% | |======================================================== | 80% | |========================================================= | 81% | |========================================================= | 82% | |========================================================== | 83% | |=========================================================== | 84% | |============================================================ | 85% | |============================================================= | 87% | |============================================================= | 88% | |============================================================== | 89% | |=============================================================== | 90% | |================================================================ | 91% | |================================================================ | 92% | |================================================================= | 93% | |================================================================== | 94% | |=================================================================== | 96% | |==================================================================== | 97% | |==================================================================== | 98% | |===================================================================== | 99% | |======================================================================| 100%
## Person file:
## | | | 0% | |== | 3% | |==== | 5% | |====== | 8% | |======== | 11% | |========= | 14% | |=========== | 16% | |============= | 19% | |=============== | 22% | |================= | 24% | |=================== | 27% | |===================== | 30% | |======================= | 32% | |========================= | 35% | |========================== | 38% | |============================ | 41% | |============================== | 43% | |================================ | 46% | |================================== | 49% | |==================================== | 51% | |====================================== | 54% | |======================================== | 57% | |========================================== | 59% | |============================================ | 62% | |============================================= | 65% | |=============================================== | 68% | |================================================= | 70% | |=================================================== | 73% | |===================================================== | 76% | |======================================================= | 78% | |========================================================= | 81% | |=========================================================== | 84% | |============================================================= | 86% | |============================================================== | 89% | |================================================================ | 92% | |================================================================== | 95% | |==================================================================== | 97% | |======================================================================| 100%
## Multiple Imputation Driver BAC by Crash file:
## | | | 0% | |======== | 11% | |================ | 22% | |======================= | 33% | |=============================== | 44% | |======================================= | 56% | |=============================================== | 67% | |====================================================== | 78% | |============================================================== | 89% | |======================================================================| 100%
## Weather file(s):
## | | | 0% | |======================================================================| 100%
## Crash risk factors:
## | | | 0% | |======================================================================| 100%
## vsoe file:
## | | | 0% | |================== | 25% | |=================================== | 50% | |==================================================== | 75% | |======================================================================| 100%
## distract file:
## | | | 0% | |=================================== | 50% | |======================================================================| 100%
## drimpair file:
## | | | 0% | |=================================== | 50% | |======================================================================| 100%
## factor file:
## | | | 0% | |=================================== | 50% | |======================================================================| 100%
## maneuver file:
## | | | 0% | |=================================== | 50% | |======================================================================| 100%
## violatn file:
## | | | 0% | |=================================== | 50% | |======================================================================| 100%
## vision file:
## | | | 0% | |=================================== | 50% | |======================================================================| 100%
## damage file:
## | | | 0% | |=================================== | 50% | |======================================================================| 100%
## vehiclesf file:
## | | | 0% | |=================================== | 50% | |======================================================================| 100%
## pvehiclesf file:
## | | | 0% | |=================================== | 50% | |======================================================================| 100%
## driverrf file:
## | | | 0% | |=================================== | 50% | |======================================================================| 100%
## PBtype file:
## | | | 0% | |=== | 5% | |====== | 9% | |========== | 14% | |============= | 18% | |================ | 23% | |=================== | 27% | |====================== | 32% | |========================= | 36% | |============================= | 41% | |================================ | 45% | |=================================== | 50% | |====================================== | 55% | |========================================= | 59% | |============================================= | 64% | |================================================ | 68% | |=================================================== | 73% | |====================================================== | 77% | |========================================================= | 82% | |============================================================ | 86% | |================================================================ | 91% | |=================================================================== | 95% | |======================================================================| 100%
## SafetyEq file:
## | | | 0% | |========= | 12% | |================== | 25% | |========================== | 38% | |=================================== | 50% | |============================================ | 62% | |==================================================== | 75% | |============================================================= | 88% | |======================================================================| 100%
## Multiple Imputation Person BAC file:
## | | | 0% | |====== | 9% | |============= | 18% | |=================== | 27% | |========================= | 36% | |================================ | 45% | |====================================== | 55% | |============================================= | 64% | |=================================================== | 73% | |========================================================= | 82% | |================================================================ | 91% | |======================================================================| 100%
## nmcrash file:
## | | | 0% | |======================= | 33% | |=============================================== | 67% | |======================================================================| 100%
## nmimpair file:
## | | | 0% | |======================= | 33% | |=============================================== | 67% | |======================================================================| 100%
## nmprior file:
## | | | 0% | |======================= | 33% | |=============================================== | 67% | |======================================================================| 100%
## nmdistract file:
## | | | 0% | |======================= | 33% | |=============================================== | 67% | |======================================================================| 100%
## drugs file:
## | | | 0% | |========= | 12% | |================== | 25% | |========================== | 38% | |=================================== | 50% | |============================================ | 62% | |==================================================== | 75% | |============================================================= | 88% | |======================================================================| 100%
## race file:
## | | | 0% | |============== | 20% | |============================ | 40% | |========================================== | 60% | |======================================================== | 80% | |======================================================================| 100%
## personrf file:
## | | | 0% | |======================= | 33% | |=============================================== | 67% | |======================================================================| 100%
## ✓ Flat file constructed
## ✓ Multi_acc file constructed
## ✓ Multi_veh file constructed
## ✓ Multi_per file constructed
## ✓ SOE file constructed
## ✓ Prepared files saved in C:/Users/STEVEJ~1/AppData/Local/Temp/RtmpC8MgaG/FARS data/prepd/2023
## ✓ Codebook file saved in C:/Users/STEVEJ~1/AppData/Local/Temp/RtmpC8MgaG/FARS data/prepd/
Then we can use counts()
to get the monthly number of
crashes in Virginia:
my_counts <- counts(
df = myFARS,
where = list(states = "VA"),
what = "crashes",
interval = c("year", "month")
)
This returns the following dataframe:
knitr::kable(my_counts, format = "html")
year | month | what | states | region | urb | who | involved | n |
---|---|---|---|---|---|---|---|---|
2022 | Jan | crashes | VA | all | all | all | any | 62 |
2022 | Feb | crashes | VA | all | all | all | any | 84 |
2022 | Mar | crashes | VA | all | all | all | any | 63 |
2022 | Apr | crashes | VA | all | all | all | any | 58 |
2022 | May | crashes | VA | all | all | all | any | 84 |
2022 | Jun | crashes | VA | all | all | all | any | 84 |
2022 | Jul | crashes | VA | all | all | all | any | 82 |
2022 | Aug | crashes | VA | all | all | all | any | 87 |
2022 | Sep | crashes | VA | all | all | all | any | 93 |
2022 | Oct | crashes | VA | all | all | all | any | 99 |
2022 | Nov | crashes | VA | all | all | all | any | 85 |
2022 | Dec | crashes | VA | all | all | all | any | 63 |
2023 | Jan | crashes | VA | all | all | all | any | 78 |
2023 | Feb | crashes | VA | all | all | all | any | 48 |
2023 | Mar | crashes | VA | all | all | all | any | 60 |
2023 | Apr | crashes | VA | all | all | all | any | 75 |
2023 | May | crashes | VA | all | all | all | any | 77 |
2023 | Jun | crashes | VA | all | all | all | any | 72 |
2023 | Jul | crashes | VA | all | all | all | any | 68 |
2023 | Aug | crashes | VA | all | all | all | any | 93 |
2023 | Sep | crashes | VA | all | all | all | any | 85 |
2023 | Oct | crashes | VA | all | all | all | any | 69 |
2023 | Nov | crashes | VA | all | all | all | any | 71 |
2023 | Dec | crashes | VA | all | all | all | any | 59 |
Which we can graph:
my_counts %>%
mutate_at("year", factor) %>%
ggplot(aes(x=month, y=n, group=year, color=year, label=scales::comma(n))) +
geom_line(linewidth = 1.5) +
labs(x=NULL, y=NULL, title = "Fatal Crashes in Virginia") +
theme_minimal() +
theme(plot.title.position = "plot")
or
my_counts %>%
mutate(date = lubridate::make_date(year, month)) %>%
ggplot(aes(x=date, y=n, label=scales::comma(n))) +
geom_col() +
labs(x=NULL, y=NULL, title = "Fatal Crashes in Virginia") +
theme(plot.title.position = "plot")
We could alternatively count annual fatalities:
counts(
myFARS,
where = list(states = "VA"),
what = "fatalities",
interval = c("year")
) %>%
knitr::kable(format = "html")
year | what | states | region | urb | who | involved | n |
---|---|---|---|---|---|---|---|
2023 | fatalities | VA | all | all | all | any | 913 |
Or fatalities involving speeding:
counts(
df = myFARS,
where = list(states = "VA"),
what = "fatalities",
interval = c("year"),
involved = "speeding"
) %>%
knitr::kable(format = "html")
year | what | states | region | urb | who | involved | n |
---|---|---|---|---|---|---|---|
2023 | fatalities | VA | all | all | all | speeding | 321 |
Or fatalities involving speeding in rural areas:
counts(
myFARS,
where = list(states = "VA", urb="rural"),
what = "fatalities",
interval = c("year"),
involved = "speeding"
) %>%
knitr::kable(format = "html")
year | what | states | region | urb | who | involved | n |
---|---|---|---|---|---|---|---|
2023 | fatalities | VA | all | rural | all | speeding | 157 |
Or we can use involved = ‘each’ to see all of the problems in one state:
counts(
df = myFARS,
where = list(states = "VA"),
what = "crashes",
interval = "year",
involved = "each"
) %>%
pivot_wider(names_from = "year", values_from = "n") %>%
arrange(desc(`2023`)) %>%
knitr::kable(format = "html")
## Note: rfars::counts() uses the variables alc_res and dr_drink to determine alcohol involvement. NHTSA reports counts using multiple imputation to estimate missing BAC values. See vignette('Alcohol Counts', package = 'rfars') for more information.
what | states | region | urb | who | involved | 2022 | 2023 |
---|---|---|---|---|---|---|---|
crashes | VA | all | all | all | roadway departure | 507 | 474 |
crashes | VA | all | all | all | speeding | 276 | 292 |
crashes | VA | all | all | all | rollover | NA | 274 |
crashes | VA | all | all | all | alcohol | NA | 223 |
crashes | VA | all | all | all | older driver | NA | 193 |
crashes | VA | all | all | all | pedbike | NA | 145 |
crashes | VA | all | all | all | pedestrian | NA | 130 |
crashes | VA | all | all | all | motorcycle | 118 | 126 |
crashes | VA | all | all | all | large trucks | 109 | 111 |
crashes | VA | all | all | all | young driver | NA | 102 |
crashes | VA | all | all | all | distracted driver | 82 | 52 |
crashes | VA | all | all | all | hit and run | 51 | 38 |
crashes | VA | all | all | all | drugs | NA | 21 |
crashes | VA | all | all | all | police pursuit | NA | 19 |
crashes | VA | all | all | all | bicyclist | NA | 15 |
crashes | VA | all | all | all | pedalcyclist | NA | 15 |
Comparing Counts
We can use compare_counts()
to quickly produce
comparison graphs. Below we compare speeding-related fatalities in rural
and urban areas:
compare_counts(
df = myFARS,
interval = "year",
involved = "speeding",
what = "fatalities",
where = list(states = "VA", urb="rural"),
where2 = list(states = "VA", urb="urban")
) %>%
ggplot(aes(x=factor(year), 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,
where = list(states = "VA"),
interval = "year",
involved = "speeding",
involved2 = "distracted driver",
what = "crashes",
) %>%
ggplot(aes(x=factor(year), 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)