library(ggplot2)
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ tibble  3.1.8      ✔ dplyr   1.0.10
## ✔ tidyr   1.2.1      ✔ stringr 1.4.1 
## ✔ readr   2.1.3      ✔ forcats 0.5.2 
## ✔ purrr   0.3.5      
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
cheese <- read_csv("http://jamessuleiman.com/teaching/datasets/cheese.csv")
## Rows: 24 Columns: 9
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (9): year, cheddar, mozzarella, swiss, blue, brick, muenster, neufchatel...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
deaths <- read_csv("http://jamessuleiman.com/teaching/datasets/Injury_Mortality__United_States.csv")
## Rows: 98280 Columns: 17
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (6): Sex, Age group (years), Race, Injury mechanism, Injury intent, Unit
## dbl (9): Year, Age Specific Rate, Age Specific Rate Standard Error, Age Spec...
## num (2): Deaths, Population
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
deaths_filtered <- deaths %>% filter(`Injury mechanism` != "All Mechanisms" & 
                    `Age group (years)` == "All Ages" &
                    Race == "All races" &
                    `Injury intent` == "All Intentions" &
                    Sex == "Both sexes") %>%
  select(Year, `Injury mechanism`, Deaths) %>%
  rename(year = Year, death_type = `Injury mechanism`, death_count = Deaths) %>%
  pivot_wider(names_from = death_type, values_from = death_count)
killer_muenster <- cheese %>% 
  select(year, muenster) %>% 
  inner_join(deaths_filtered, by = "year")
killer_muenster %>% select(-year) %>% cor()
killer_muenster%>%
  ggplot(aes(x = Firearm, y = muenster)) + 
  geom_point() + 
  geom_smooth(formula = y ~ x, method = "lm") +
  xlab("Annual Firearm Deaths") +
  ylab("Muenster Cheese Consumed") +
  ggtitle("Muenster Loves Guns") +
  theme_minimal()