Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ### My remix of the "NICS Firearm Background Checks" thread - /u/MetricT
- ### https://www.reddit.com/r/dataisbeautiful/comments/ie4wxu/fbi_firearm_background_checks_over_time_oc/
- ###
- ### First, you need to convert the data from this pdf into something useable:
- ###
- ### https://www.fbi.gov/file-repository/nics_firearm_checks_-_month_year.pdf/view
- ###
- ### I used Tabula [https://tabula.technology/] to scan the PDF and save it as a spreadsheet
- ### If you do this, export it as "stream" instead of the default "lattice" to get it in the
- ### correct form.
- ###
- ### Fire up the .csv Tabula made in Excel and trim off the "Total" column, although strictly
- ### speaking it wasn't necessary.
- ###
- ### At this point, R should handle the rest...
- ###
- ### The "geom_inauguration_dates" is a function I wrote. I haven't published it yet because
- ### it's not a true geom_* object, it just uses a similar function call. If anyone wants to
- ### help me turn it into an actual geom_ object, hit me up...
- data <-
- "NICS_Firearm_Background_Checks.csv" %>%
- read_csv(col_names = TRUE, col_types = "dnnnnnnnnnnnn") %>%
- pivot_longer(-Year, names_to = "Month", values_to = "NICS_Firearm_Background_Checks") %>%
- filter(!is.na(NICS_Firearm_Background_Checks)) %>%
- filter(Year >= 1999) %>%
- mutate(yearmonth = yearmonth(paste(Year, " ", Month))) %>%
- select(yearmonth, NICS_Firearm_Background_Checks)
- model <-
- data %>%
- as_tsibble(index = "yearmonth") %>%
- model(STL(NICS_Firearm_Background_Checks ~ trend(window = 90))) %>%
- components()
- graph <-
- model %>%
- select(yearmonth, NICS_Firearm_Background_Checks, trend, season_adjust) %>%
- rename(values = NICS_Firearm_Background_Checks) %>%
- filter(yearmonth >= yearmonth("1999 Jan")) %>%
- ggplot(aes(x = yearmonth(yearmonth))) +
- geom_point(aes(y = values), alpha = 0.2) +
- geom_line(aes(y = trend * 0.85), size = 0.8, linetype = "dotted", color = "black") +
- geom_line(aes(y = season_adjust), size = 1.2, color = "darkseagreen4") +
- labs(x = "Date",
- y = "# of Background Checks",
- title = "NICS Firearm Background Checks by Month") +
- scale_y_continuous(labels = scales::comma) +
- theme_bw() +
- geom_inauguration_dates(min(model$yearmonth), max(model$yearmonth), 3500000) +
- theme(legend.position = "none")
- print(graph)
Add Comment
Please, Sign In to add comment