Kaitlin Kavlie PSYC-541
Lab #11: Choropleths
I ran the packages below.
library(tidyverse)
library(broom)
library(plotly)
library(tidycensus)
library(sf)
library(leaflet)
library(trendyy)
library(usdata)
Then I entered, ran, and removed my census API key.
census_api_key("70254b0b13d46bff0d9a603bf6343509f408cf44", overwrite = TRUE)
To install your API key for use in future sessions, run this function with `install = TRUE`.
I used the first code to read in the cigarette smoking data.
cig_smoking <- read_csv("Cig_smoking_percent.csv")
states_leaflet <- get_acs(geography = "state",
variables = "B01003_001",
geometry = TRUE)
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)`.
|
| | 0%
|
|= | 1%
|
|== | 1%
|
|=== | 2%
|
|==== | 3%
|
|===== | 3%
|
|====== | 4%
|
|======= | 4%
|
|======= | 5%
|
|======== | 5%
|
|========= | 6%
|
|========== | 6%
|
|========== | 7%
|
|=========== | 7%
|
|============ | 8%
|
|============= | 8%
|
|============= | 9%
|
|============== | 9%
|
|=============== | 10%
|
|================ | 10%
|
|================ | 11%
|
|================= | 11%
|
|================== | 12%
|
|=================== | 13%
|
|==================== | 13%
|
|===================== | 14%
|
|====================== | 14%
|
|====================== | 15%
|
|======================= | 15%
|
|======================== | 16%
|
|========================= | 16%
|
|========================== | 17%
|
|=========================== | 18%
|
|============================ | 18%
|
|============================ | 19%
|
|============================= | 19%
|
|============================== | 19%
|
|============================== | 20%
|
|=============================== | 20%
|
|=============================== | 21%
|
|================================ | 21%
|
|================================= | 22%
|
|================================== | 22%
|
|=================================== | 23%
|
|==================================== | 24%
|
|===================================== | 24%
|
|====================================== | 25%
|
|======================================= | 25%
|
|======================================= | 26%
|
|======================================== | 26%
|
|======================================== | 27%
|
|========================================= | 27%
|
|========================================== | 27%
|
|========================================== | 28%
|
|=========================================== | 28%
|
|=========================================== | 29%
|
|============================================ | 29%
|
|============================================= | 29%
|
|============================================= | 30%
|
|============================================== | 30%
|
|=============================================== | 31%
|
|================================================ | 32%
|
|================================================= | 32%
|
|================================================== | 33%
|
|=================================================== | 33%
|
|==================================================== | 34%
|
|===================================================== | 35%
|
|====================================================== | 35%
|
|====================================================== | 36%
|
|======================================================= | 36%
|
|======================================================== | 37%
|
|========================================================= | 38%
|
|========================================================== | 38%
|
|=========================================================== | 39%
|
|============================================================ | 39%
|
|============================================================ | 40%
|
|============================================================= | 40%
|
|============================================================== | 41%
|
|=============================================================== | 41%
|
|================================================================ | 42%
|
|================================================================= | 43%
|
|================================================================== | 43%
|
|================================================================== | 44%
|
|=================================================================== | 44%
|
|==================================================================== | 44%
|
|==================================================================== | 45%
|
|===================================================================== | 45%
|
|===================================================================== | 46%
|
|====================================================================== | 46%
|
|======================================================================= | 47%
|
|======================================================================== | 47%
|
|======================================================================== | 48%
|
|========================================================================= | 48%
|
|========================================================================== | 49%
|
|=========================================================================== | 49%
|
|============================================================================ | 50%
|
|============================================================================= | 50%
|
|============================================================================= | 51%
|
|============================================================================== | 51%
|
|=============================================================================== | 52%
|
|================================================================================ | 52%
|
|================================================================================ | 53%
|
|================================================================================= | 53%
|
|================================================================================= | 54%
|
|================================================================================== | 54%
|
|=================================================================================== | 54%
|
|=================================================================================== | 55%
|
|==================================================================================== | 55%
|
|===================================================================================== | 56%
|
|====================================================================================== | 57%
|
|======================================================================================= | 57%
|
|======================================================================================== | 58%
|
|========================================================================================= | 58%
|
|========================================================================================= | 59%
|
|========================================================================================== | 59%
|
|=========================================================================================== | 60%
|
|============================================================================================ | 60%
|
|============================================================================================= | 61%
|
|============================================================================================== | 62%
|
|=============================================================================================== | 62%
|
|=============================================================================================== | 63%
|
|================================================================================================ | 63%
|
|================================================================================================= | 64%
|
|================================================================================================== | 64%
|
|================================================================================================== | 65%
|
|=================================================================================================== | 65%
|
|==================================================================================================== | 66%
|
|===================================================================================================== | 66%
|
|====================================================================================================== | 67%
|
|======================================================================================================= | 68%
|
|======================================================================================================== | 68%
|
|========================================================================================================= | 69%
|
|========================================================================================================== | 69%
|
|========================================================================================================== | 70%
|
|=========================================================================================================== | 70%
|
|=========================================================================================================== | 71%
|
|============================================================================================================ | 71%
|
|============================================================================================================= | 71%
|
|============================================================================================================= | 72%
|
|============================================================================================================== | 72%
|
|============================================================================================================== | 73%
|
|=============================================================================================================== | 73%
|
|================================================================================================================ | 74%
|
|================================================================================================================= | 74%
|
|================================================================================================================== | 75%
|
|=================================================================================================================== | 76%
|
|==================================================================================================================== | 76%
|
|===================================================================================================================== | 77%
|
|====================================================================================================================== | 77%
|
|====================================================================================================================== | 78%
|
|======================================================================================================================= | 78%
|
|======================================================================================================================= | 79%
|
|======================================================================================================================== | 79%
|
|========================================================================================================================= | 79%
|
|========================================================================================================================= | 80%
|
|========================================================================================================================== | 80%
|
|=========================================================================================================================== | 81%
|
|============================================================================================================================ | 82%
|
|============================================================================================================================= | 82%
|
|============================================================================================================================= | 83%
|
|============================================================================================================================== | 83%
|
|=============================================================================================================================== | 83%
|
|=============================================================================================================================== | 84%
|
|================================================================================================================================ | 84%
|
|================================================================================================================================= | 85%
|
|================================================================================================================================== | 85%
|
|=================================================================================================================================== | 86%
|
|==================================================================================================================================== | 87%
|
|===================================================================================================================================== | 87%
|
|===================================================================================================================================== | 88%
|
|====================================================================================================================================== | 88%
|
|======================================================================================================================================= | 89%
|
|======================================================================================================================================== | 89%
|
|======================================================================================================================================== | 90%
|
|========================================================================================================================================= | 90%
|
|========================================================================================================================================== | 91%
|
|=========================================================================================================================================== | 91%
|
|=========================================================================================================================================== | 92%
|
|============================================================================================================================================ | 92%
|
|============================================================================================================================================= | 93%
|
|============================================================================================================================================== | 93%
|
|============================================================================================================================================== | 94%
|
|=============================================================================================================================================== | 94%
|
|================================================================================================================================================ | 94%
|
|================================================================================================================================================ | 95%
|
|================================================================================================================================================= | 95%
|
|================================================================================================================================================== | 96%
|
|=================================================================================================================================================== | 97%
|
|==================================================================================================================================================== | 97%
|
|==================================================================================================================================================== | 98%
|
|===================================================================================================================================================== | 98%
|
|====================================================================================================================================================== | 99%
|
|======================================================================================================================================================= | 99%
|
|========================================================================================================================================================| 100%
state_colors <- colorNumeric(palette = "viridis", domain = states_leaflet$estimate)
states_leaflet %>%
leaflet() %>%
addTiles() %>%
addPolygons(weight = 1,
fillColor = ~state_colors(estimate),
label = ~paste0(NAME, ", income = ", estimate),
highlight = highlightOptions(weight = 2)) %>%
setView(-95, 40, zoom = 4) %>%
addLegend(pal = state_colors, values = ~estimate)
Warning: sf layer has inconsistent datum (+proj=longlat +datum=NAD83 +no_defs).
Need '+proj=longlat +datum=WGS84'
cig_colors <- colorNumeric(palette = "viridis", domain = cig_smoking$Cig_percent)
states_leaflet %>%
leaflet() %>%
addTiles() %>%
addPolygons(weight = 1,
fillColor = ~cig_colors(Cig_percent),
label = ~paste0(location, ", Cigarette percentage = ", Cig_percent),
highlight = highlightOptions(weight = 2)) %>%
setView(-95, 40, zoom = 4) %>%
addLegend(pal = cig_colors, values = ~Cig_percent)
Warning: sf layer has inconsistent datum (+proj=longlat +datum=NAD83 +no_defs).
Need '+proj=longlat +datum=WGS84'
Error in paste0(location, ", Cigarette percentage = ", Cig_percent) :
object 'Cig_percent' not found
nicotine <- trendy("nicotine",
geo = "US",
from = "2019-01-01", to = "2020-01-01")
nicotine_states <- nicotine %>%
get_interest_region()
nicotine_states
NA
###this doesn’t work either, keep getting errors for addPolygon function
nicotine_colors <-colorNumeric(palette = "viridis", domain = nicotine_states$hits)
states_leaflet %>%
rename(location = NAME) %>%
inner_join(nicotine_states)
Joining, by = "location"
Simple feature collection with 51 features and 9 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -179.1489 ymin: 18.91036 xmax: 179.7785 ymax: 71.36516
Geodetic CRS: NAD83
First 10 features:
GEOID location variable estimate moe hits keyword geo gprop geometry
1 35 New Mexico B01003_001 2097021 NA 62 nicotine US web MULTIPOLYGON (((-109.0502 3...
2 06 California B01003_001 39346023 NA 66 nicotine US web MULTIPOLYGON (((-118.6044 3...
3 01 Alabama B01003_001 4893186 NA 67 nicotine US web MULTIPOLYGON (((-88.05338 3...
4 13 Georgia B01003_001 10516579 NA 76 nicotine US web MULTIPOLYGON (((-81.27939 3...
5 05 Arkansas B01003_001 3011873 NA 87 nicotine US web MULTIPOLYGON (((-94.61792 3...
6 41 Oregon B01003_001 4176346 NA 68 nicotine US web MULTIPOLYGON (((-123.6647 4...
7 28 Mississippi B01003_001 2981835 NA 75 nicotine US web MULTIPOLYGON (((-88.50297 3...
8 08 Colorado B01003_001 5684926 NA 78 nicotine US web MULTIPOLYGON (((-109.0603 3...
9 49 Utah B01003_001 3151239 NA 69 nicotine US web MULTIPOLYGON (((-114.053 37...
10 40 Oklahoma B01003_001 3949342 NA 78 nicotine US web MULTIPOLYGON (((-103.0026 3...
leaflet() %>%
addTiles() %>%
addPolygons(weight = 1,
fillColor = ~nicotine_colors(hits),
label = ~paste0(location, ", Search volume = ", hits),
highlight = highlightOptions(weight = 2)) %>%
setView(-95, 40, zoom = 4) %>%
addLegend(pal = nicotine_colors, values = ~hits)
Error in derivePolygons(data, lng, lat, missing(lng), missing(lat), "addPolygons") :
Polygon data not found; please provide addPolygons with data and/or lng/lat arguments
nicotine_model <- lm(cig_smoking ~ Cig_percent, data = nicotine_data)
Error in is.data.frame(data) : object 'nicotine_data' not found
nicotine_data %>%
drop_na() %>%
plot_ly(x = ~hits,
y = ~Cig_percent,
hoverinfo = "text",
text = ~paste("State: ", location, "<br>", "Nicotine's search rate: ", hits, "<br>", "Cig smoking percent:", Cig_percent)) %>%
add_markers(showlegend = F) %>%
add_lines(y = ~fitted(nicotine_model)) %>%
layout(title = "Relationship between google searches for 'nicotine' and smoking percent rates, by state",
xaxis = list(title = "Google search volume for 'nicotine'"),
yaxis = list(title = "State smoking rate, per capita"))
smoking <- trendy("smoking",
geo = "US",
from = "2019-01-01", to = "2020-01-01")
smoking_states <- smoking %>%
get_interest_region()
smoking_states
NA
smoking_states %>%
mutate(State = state2abbr(location)) %>%
inner_join(cig_smoking)
Joining, by = "State"
smoking_colors <-colorNumeric(palette = "viridis", domain = smoking_states$hits)
states_leaflet %>%
rename(location = NAME) %>%
inner_join(smoking_states)
Joining, by = "location"
Simple feature collection with 51 features and 9 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -179.1489 ymin: 18.91036 xmax: 179.7785 ymax: 71.36516
Geodetic CRS: NAD83
First 10 features:
GEOID location variable estimate moe hits keyword geo gprop geometry
1 35 New Mexico B01003_001 2097021 NA 62 smoking US web MULTIPOLYGON (((-109.0502 3...
2 06 California B01003_001 39346023 NA 75 smoking US web MULTIPOLYGON (((-118.6044 3...
3 01 Alabama B01003_001 4893186 NA 87 smoking US web MULTIPOLYGON (((-88.05338 3...
4 13 Georgia B01003_001 10516579 NA 90 smoking US web MULTIPOLYGON (((-81.27939 3...
5 05 Arkansas B01003_001 3011873 NA 88 smoking US web MULTIPOLYGON (((-94.61792 3...
6 41 Oregon B01003_001 4176346 NA 85 smoking US web MULTIPOLYGON (((-123.6647 4...
7 28 Mississippi B01003_001 2981835 NA 75 smoking US web MULTIPOLYGON (((-88.50297 3...
8 08 Colorado B01003_001 5684926 NA 90 smoking US web MULTIPOLYGON (((-109.0603 3...
9 49 Utah B01003_001 3151239 NA 68 smoking US web MULTIPOLYGON (((-114.053 37...
10 40 Oklahoma B01003_001 3949342 NA 98 smoking US web MULTIPOLYGON (((-103.0026 3...
leaflet() %>%
addTiles() %>%
addPolygons(weight = 1,
fillColor = ~smoking_colors(Cig_percent),
label = ~paste0(location, ", Cigarette percentage = ", Cig_percent),
highlight = highlightOptions(weight = 2)) %>%
setView(-95, 40, zoom = 4) %>%
addLegend(pal = smoking_colors, values = ~hits)
Error in derivePolygons(data, lng, lat, missing(lng), missing(lat), "addPolygons") :
Polygon data not found; please provide addPolygons with data and/or lng/lat arguments
smoking_model <- lm(Cig_smoking ~ hits, data = smoking_data)
Error in is.data.frame(data) : object 'smoking_data' not found
smoking_data %>%
drop_na() %>%
plot_ly(x = ~hits,
y = ~Cig_smoking,
hoverinfo = "text",
text = ~paste("State: ", location, "<br>", "'Smoking's search rate: ", hits, "<br>", "Cigarette Smoking: ", cig_smoking)) %>%
add_markers(showlegend = F) %>%
add_lines(y = ~fitted(smoking_model)) %>%
layout(title = "Relationship between google searches for 'smoking' and rates of cigarette smoking, by state",
xaxis = list(title = "Google search volume for 'smoking'"),
yaxis = list(title = "State cigarette smoking rate, per capita"))