library(tidyverse)
── Attaching packages ────────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse 1.3.0 ──
✓ ggplot2 3.2.1 ✓ purrr 0.3.3
✓ tibble 2.1.3 ✓ dplyr 0.8.3
✓ tidyr 1.0.0 ✓ stringr 1.4.0
✓ readr 1.3.1 ✓ forcats 0.4.0
── Conflicts ───────────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
x dplyr::filter() masks stats::filter()
x dplyr::lag() masks stats::lag()
library(leaflet)
library(sf)
Linking to GEOS 3.5.1, GDAL 2.2.2, PROJ 4.9.2
library(readxl)
library(DT)
library(plotly)
Attaching package: ‘plotly’
The following object is masked from ‘package:ggplot2’:
last_plot
The following object is masked from ‘package:stats’:
filter
The following object is masked from ‘package:graphics’:
layout
library(broom)
library(tidycensus)
census_api_key("a7c59af9ec8e85e3b63423792c176d986478bd94")
To install your API key for use in future sessions, run this function with `install = TRUE`.
house_counties <- read_xlsx("Statewide Results.xlsx", sheet = 2)
New names:
* `` -> ...2
* `` -> ...3
* `` -> ...4
* `` -> ...5
house_counties <- read_xlsx("Statewide Results.xlsx", sheet = 2, range = "B7:E63")
glimpse(house_counties)
Observations: 56
Variables: 4
$ County <chr> "Beaverhead", "Big Horn", "Blaine", "Broadwater", "Carbon", "Carter", "Cascade", "Chouteau…
$ `GREG GIANFORTE\r\nRepublican` <dbl> 3113, 1705, 1173, 2249, 3450, 636, 17219, 1515, 3085, 684, 2811, 1379, 1025, 3915, 28450, …
$ `KATHLEEN WILLIAMS\r\nDemocrat` <dbl> 1681, 2865, 1742, 897, 2442, 100, 15826, 1071, 1638, 240, 1128, 2693, 226, 1752, 18051, 31…
$ `ELINOR SWANSON\r\nLibertarian` <dbl> 122, 106, 87, 105, 184, 12, 925, 66, 168, 20, 125, 155, 24, 139, 1277, 1523, 20, 111, 22, …
house_counties <- house_counties %>%
rename(Republican = "GREG GIANFORTE\r\nRepublican") %>%
rename(Democrat = "KATHLEEN WILLIAMS\r\nDemocrat") %>%
rename(Libertarian = "ELINOR SWANSON\r\nLibertarian")
glimpse(house_counties)
Observations: 56
Variables: 4
$ County <chr> "Beaverhead", "Big Horn", "Blaine", "Broadwater", "Carbon", "Carter", "Cascade", "Chouteau", "Custer", "Daniel…
$ Republican <dbl> 3113, 1705, 1173, 2249, 3450, 636, 17219, 1515, 3085, 684, 2811, 1379, 1025, 3915, 28450, 23157, 606, 1307, 32…
$ Democrat <dbl> 1681, 2865, 1742, 897, 2442, 100, 15826, 1071, 1638, 240, 1128, 2693, 226, 1752, 18051, 31297, 55, 3564, 108, …
$ Libertarian <dbl> 122, 106, 87, 105, 184, 12, 925, 66, 168, 20, 125, 155, 24, 139, 1277, 1523, 20, 111, 22, 62, 231, 200, 34, 35…
house_counties <- house_counties %>%
mutate(total_votes = Republican + Democrat + Libertarian) %>%
mutate(Repub_advantage = Republican/total_votes - Democrat/total_votes) %>%
mutate(Repub_advantage = round(Repub_advantage*100, 1))
house_counties %>%
arrange(-Repub_advantage)
NA
mt_counties <- get_acs(geography = "county",
variables = "B01003_001",
state = "MT",
geometry = TRUE)
Getting data from the 2014-2018 5-year ACS
Downloading feature geometry from the Census website. To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
Using FIPS code '30' for state 'MT'
|
| | 0%
|
|= | 0%
|
|= | 1%
|
|== | 1%
|
|== | 2%
|
|=== | 2%
|
|=== | 3%
|
|==== | 3%
|
|==== | 4%
|
|===== | 4%
|
|====== | 5%
|
|======= | 5%
|
|======= | 6%
|
|======== | 6%
|
|======== | 7%
|
|========= | 7%
|
|========= | 8%
|
|========== | 8%
|
|=========== | 9%
|
|============ | 9%
|
|============ | 10%
|
|============= | 10%
|
|============= | 11%
|
|============== | 11%
|
|============== | 12%
|
|=============== | 12%
|
|================ | 13%
|
|================= | 14%
|
|================== | 14%
|
|================== | 15%
|
|=================== | 15%
|
|=================== | 16%
|
|==================== | 16%
|
|==================== | 17%
|
|===================== | 17%
|
|====================== | 17%
|
|====================== | 18%
|
|======================= | 18%
|
|======================= | 19%
|
|======================== | 19%
|
|======================== | 20%
|
|========================= | 20%
|
|========================= | 21%
|
|========================== | 21%
|
|========================== | 22%
|
|=========================== | 22%
|
|============================ | 22%
|
|============================ | 23%
|
|============================= | 23%
|
|============================= | 24%
|
|============================== | 24%
|
|============================== | 25%
|
|=============================== | 25%
|
|=============================== | 26%
|
|================================ | 26%
|
|================================= | 26%
|
|================================= | 27%
|
|================================== | 27%
|
|================================== | 28%
|
|=================================== | 28%
|
|=================================== | 29%
|
|==================================== | 29%
|
|==================================== | 30%
|
|===================================== | 30%
|
|====================================== | 31%
|
|======================================= | 31%
|
|======================================= | 32%
|
|======================================== | 32%
|
|======================================== | 33%
|
|========================================= | 33%
|
|========================================= | 34%
|
|========================================== | 34%
|
|========================================== | 35%
|
|=========================================== | 35%
|
|============================================ | 35%
|
|============================================ | 36%
|
|============================================= | 36%
|
|============================================= | 37%
|
|============================================== | 37%
|
|============================================== | 38%
|
|=============================================== | 38%
|
|=============================================== | 39%
|
|================================================ | 39%
|
|================================================= | 39%
|
|================================================= | 40%
|
|================================================== | 40%
|
|================================================== | 41%
|
|=================================================== | 41%
|
|=================================================== | 42%
|
|==================================================== | 42%
|
|==================================================== | 43%
|
|===================================================== | 43%
|
|====================================================== | 44%
|
|======================================================= | 44%
|
|======================================================= | 45%
|
|======================================================== | 45%
|
|======================================================== | 46%
|
|========================================================= | 46%
|
|========================================================= | 47%
|
|========================================================== | 47%
|
|========================================================== | 48%
|
|=========================================================== | 48%
|
|============================================================ | 48%
|
|============================================================ | 49%
|
|============================================================= | 49%
|
|============================================================= | 50%
|
|============================================================== | 50%
|
|============================================================== | 51%
|
|=============================================================== | 51%
|
|=============================================================== | 52%
|
|================================================================ | 52%
|
|================================================================= | 52%
|
|================================================================= | 53%
|
|================================================================== | 53%
|
|================================================================== | 54%
|
|=================================================================== | 54%
|
|=================================================================== | 55%
|
|==================================================================== | 55%
|
|==================================================================== | 56%
|
|===================================================================== | 56%
|
|====================================================================== | 57%
|
|======================================================================= | 57%
|
|======================================================================= | 58%
|
|======================================================================== | 58%
|
|======================================================================== | 59%
|
|========================================================================= | 59%
|
|========================================================================= | 60%
|
|========================================================================== | 60%
|
|=========================================================================== | 61%
|
|============================================================================ | 62%
|
|============================================================================= | 62%
|
|============================================================================= | 63%
|
|============================================================================== | 63%
|
|============================================================================== | 64%
|
|=============================================================================== | 64%
|
|=============================================================================== | 65%
|
|================================================================================ | 65%
|
|================================================================================= | 65%
|
|================================================================================= | 66%
|
|================================================================================== | 66%
|
|================================================================================== | 67%
|
|=================================================================================== | 67%
|
|=================================================================================== | 68%
|
|==================================================================================== | 68%
|
|==================================================================================== | 69%
|
|===================================================================================== | 69%
|
|====================================================================================== | 70%
|
|======================================================================================= | 70%
|
|======================================================================================= | 71%
|
|======================================================================================== | 71%
|
|======================================================================================== | 72%
|
|========================================================================================= | 72%
|
|========================================================================================= | 73%
|
|========================================================================================== | 73%
|
|=========================================================================================== | 74%
|
|============================================================================================ | 74%
|
|============================================================================================ | 75%
|
|============================================================================================= | 75%
|
|============================================================================================= | 76%
|
|============================================================================================== | 76%
|
|============================================================================================== | 77%
|
|=============================================================================================== | 77%
|
|=============================================================================================== | 78%
|
|================================================================================================ | 78%
|
|================================================================================================= | 79%
|
|================================================================================================== | 79%
|
|================================================================================================== | 80%
|
|=================================================================================================== | 80%
|
|=================================================================================================== | 81%
|
|==================================================================================================== | 81%
|
|==================================================================================================== | 82%
|
|===================================================================================================== | 82%
|
|====================================================================================================== | 83%
|
|======================================================================================================= | 83%
|
|======================================================================================================= | 84%
|
|======================================================================================================== | 84%
|
|======================================================================================================== | 85%
|
|========================================================================================================= | 85%
|
|========================================================================================================= | 86%
|
|========================================================================================================== | 86%
|
|========================================================================================================== | 87%
|
|=========================================================================================================== | 87%
|
|============================================================================================================ | 87%
|
|============================================================================================================ | 88%
|
|============================================================================================================= | 88%
|
|============================================================================================================= | 89%
|
|============================================================================================================== | 89%
|
|============================================================================================================== | 90%
|
|=============================================================================================================== | 90%
|
|=============================================================================================================== | 91%
|
|================================================================================================================ | 91%
|
|================================================================================================================= | 92%
|
|================================================================================================================== | 92%
|
|================================================================================================================== | 93%
|
|=================================================================================================================== | 93%
|
|=================================================================================================================== | 94%
|
|==================================================================================================================== | 94%
|
|==================================================================================================================== | 95%
|
|===================================================================================================================== | 95%
|
|====================================================================================================================== | 96%
|
|======================================================================================================================= | 96%
|
|======================================================================================================================= | 97%
|
|======================================================================================================================== | 97%
|
|======================================================================================================================== | 98%
|
|========================================================================================================================= | 98%
|
|========================================================================================================================= | 99%
|
|========================================================================================================================== | 99%
|
|========================================================================================================================== | 100%
|
|===========================================================================================================================| 100%
house_counties[25, "County"] <- "Lewis and Clark" # Changes "&" "and"
mt_counties <- mt_counties %>%
mutate(County = gsub(" County, Montana", "", NAME)) %>% # Removes unnecessary words
rename(Population = estimate) # Renames the 'estimate' to 'Population'
house_election <- mt_counties %>%
full_join(house_counties)
Joining, by = "County"
house_election %>%
as_tibble() %>%
select(County, Population, Democrat, Republican, Libertarian, total_votes, Repub_advantage) %>%
datatable()
NA
vote_colors <- colorNumeric(palette = "viridis", domain = house_election$Repub_advantage)
house_election %>%
leaflet() %>%
addTiles() %>%
addPolygons(weight = 1,
fillColor = ~vote_colors(Repub_advantage),
label = ~paste0(County, ", Republican advantage = ", Repub_advantage),
highlight = highlightOptions(weight = 2)) %>%
setView(-110, 47, zoom = 6) %>%
addLegend(pal = vote_colors, values = ~Repub_advantage)
sf layer has inconsistent datum (+proj=longlat +datum=NAD83 +no_defs).
Need '+proj=longlat +datum=WGS84'
house_election %>%
plot_ly(x = ~Population, y = ~Repub_advantage) %>%
add_markers()
house_election %>%
plot_ly(x = ~Population,
y = ~Repub_advantage,
hoverinfo = "text",
text = ~paste("County:",
County, "<br>",
"Population: ", Population, "<br>",
"Republican advantage: ", Repub_advantage)) %>%
add_markers(marker = list(opacity = 0.7)) %>%
layout(title = "Predicting Republican Vote Advantage from Population, by County",
xaxis = list(title = "County population"),
yaxis = list(title = "Republican vote advantage"))
NA
pop_model <- lm(Repub_advantage ~ Population, data = house_election)
summary(pop_model)
Call:
lm(formula = Repub_advantage ~ Population, data = house_election)
Residuals:
Min 1Q Median 3Q Max
-73.973 -10.320 3.959 15.305 47.232
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 34.1220288 4.0462304 8.433 1.99e-11 ***
Population -0.0003977 0.0001090 -3.648 0.000597 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 26.2 on 54 degrees of freedom
Multiple R-squared: 0.1977, Adjusted R-squared: 0.1828
F-statistic: 13.31 on 1 and 54 DF, p-value: 0.0005968
tidy(pop_model)
glance(pop_model)
NA
NA
tidy(pop_model)
glance(pop_model) %>%
add_lines(y = ~fitted(pop_model)) %>%
add_markers(showlengend = F)
Unknown or uninitialised column: 'x'.Unknown or uninitialised column: 'x'.Error: Must supply `x` and `y` attributes
house_election <- house_election %>%
mutate(Longitude = as_tibble(st_coordinates(st_centroid(house_election$geometry)))$X) %>%
mutate(Latitude = as_tibble(st_coordinates(st_centroid(house_election$geometry)))$Y)
st_centroid does not give correct centroids for longitude/latitude datast_centroid does not give correct centroids for longitude/latitude data
house_election %>%
leaflet() %>%
addTiles() %>%
addPolygons(weight = 1) %>%
setView(-110, 47, zoom = 6) %>%
addCircleMarkers(~Longitude, ~Latitude)
sf layer has inconsistent datum (+proj=longlat +datum=NAD83 +no_defs).
Need '+proj=longlat +datum=WGS84'