library(tidyverse)
library(broom)
library(plotly)
library(tidycensus) # gets census data that we can use to create maps
library(sf) # helper package for mapping
library(leaflet) # interactive mapping package
library(trendyy)
library(usdata)
This first chunk gathers the data for how many times “guns” have been searched on google, by state.
guns <- trendy("guns",
geo = "US",
from = "2019-01-01", to = "2020-01-01")
guns_states <- guns %>%
get_interest_region()
guns_states
The following chunk then takes that data and makes it into a graph
states %>%
rename(location = NAME) %>%
inner_join(guns_states) %>%
ggplot() + # create graph
geom_sf(aes(fill = hits)) + # color states with hits
scale_fill_viridis_c() + # use the viridis colors
coord_sf(datum = NA) + # remove coordinates
theme_minimal() + # remove background
labs(title = "State google searches for 'guns'", fill = "Search volume")
Joining, by = "location"
states_leaflet <- get_acs(geography = "state", # gets state by state data
variables = "B19013_001", # this is state income
geometry = TRUE) # gets geometry (the maps)
Getting data from the 2016-2020 5-year ACS
Downloading feature geometry from the Census website. To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
# shift_geo = T
This chunk analyzes the file Firearm_death_rate.csv and reads it into the program for later analysis.
read.csv("Firearm_death_rate.csv")
Ths chunk then creates a graph using the Firearm_Death_Rate data. It shows the firearm death rate by state.
guns_colors <- colorNumeric(palette = "viridis", domain = guns_data$Rate)
states_leaflet %>%
rename(location = NAME) %>%
inner_join(guns_data) %>%
leaflet() %>%
addTiles() %>%
addPolygons(weight = 1,
fillColor = ~guns_colors(Rate),
label = ~paste0(location, ", Firearm death rate = ", Rate),
highlight = highlightOptions(weight = 2)) %>%
setView(-95, 40, zoom = 4) %>%
addLegend(pal = guns_colors, values = ~ Rate)
Joining, by = "location"sf layer has inconsistent datum (+proj=longlat +datum=NAD83 +no_defs).
Need '+proj=longlat +datum=WGS84'