## ----message=F, warning=FALSE-------------------------------------------------
library(dplyr)
library(ggplot2)
library(tidyr)
library(rfars)

## ----include=FALSE------------------------------------------------------------
knitr::opts_chunk$set(fig.width = 7, fig.height = 5)

## ----results='asis'-----------------------------------------------------------
knitr::kable(rfars::annual_counts, format = "html")

## ----fig.width = 7, fig.height=3----------------------------------------------
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")

## ----message=FALSE, eval=F----------------------------------------------------
#  myFARS <- get_fars(years = 2019:2023, proceed = T, states = "VA")

## ----include=FALSE------------------------------------------------------------
vignette_data <- rfars:::vignette_data

## ----results='asis', eval=FALSE-----------------------------------------------
#  my_counts <- counts(
#    df = myFARS,
#    where = list(states = "VA"),
#    what = "crashes",
#    interval = c("year", "month")
#    )

## ----echo=FALSE---------------------------------------------------------------
my_counts <- vignette_data$counts_1

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

## -----------------------------------------------------------------------------
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, alpha=.9) + 
  scale_color_brewer() +
  labs(x=NULL, y=NULL, title = "Fatal Crashes in Virginia") +
  theme(plot.title.position = "plot")

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

## ----results='asis', eval=FALSE-----------------------------------------------
#  counts(
#    myFARS,
#    where = list(states = "VA"),
#    what = "fatalities",
#    interval = c("year")
#    ) %>%
#    knitr::kable(format = "html")

## ----results='asis', echo=FALSE-----------------------------------------------
vignette_data$counts_2 %>%
  knitr::kable(format = "html")

## ----results='asis', eval=FALSE-----------------------------------------------
#  counts(
#    df = myFARS,
#    where = list(states = "VA"),
#    what = "fatalities",
#    interval = c("year"),
#    involved = "speeding"
#  ) %>%
#    knitr::kable(format = "html")

## ----results='asis', echo=FALSE-----------------------------------------------
vignette_data$counts_3 %>%
  knitr::kable(format = "html")

## ----results='asis', eval=FALSE-----------------------------------------------
#  counts(
#    myFARS,
#    where = list(states = "VA", urb="rural"),
#    what = "fatalities",
#    interval = c("year"),
#    involved = "speeding"
#  ) %>%
#    knitr::kable(format = "html")

## ----results='asis', echo=FALSE-----------------------------------------------
vignette_data$counts_4 %>%
  knitr::kable(format = "html")

## ----results='asis', eval=FALSE-----------------------------------------------
#  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")

## ----results='asis', echo=FALSE-----------------------------------------------
vignette_data$counts_5 %>%
  pivot_wider(names_from = "year", values_from = "n") %>%
  arrange(desc(`2023`)) %>%
  knitr::kable(format = "html")

## ----eval=FALSE---------------------------------------------------------------
#  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)

## ----echo=FALSE---------------------------------------------------------------
vignette_data$counts_6 %>%
  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)

## ----eval=FALSE---------------------------------------------------------------
#  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)

## ----echo=FALSE---------------------------------------------------------------
vignette_data$counts_7 %>%
  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)

