### 1 - load libraries and data
library(tidyverse)── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
✔ ggplot2 3.3.6 ✔ purrr 0.3.4
✔ tibble 3.1.7 ✔ dplyr 1.0.9
✔ tidyr 1.2.0 ✔ stringr 1.4.0
✔ readr 2.1.2 ✔ forcats 0.5.1
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
library(sf)Linking to GEOS 3.10.2, GDAL 3.4.2, PROJ 8.2.1; sf_use_s2() is TRUE
library(sp)
library(tigris)To enable caching of data, set `options(tigris_use_cache = TRUE)`
in your R script or .Rprofile.
library(ggthemes)
library(here)here() starts at /Users/haydenbharper/Downloads/I'm Doing Data Science??/political-analysis-in-R-main
library(lubridate)
Attaching package: 'lubridate'
The following objects are masked from 'package:base':
date, intersect, setdiff, union
library(leaflet)
library(countrycode)
# 1.) load data and a map
house_districts <- read_csv("/Users/haydenbharper/Downloads/I'm Doing Data Science??/political-analysis-in-R-main/data/raw/election-forecasts-2022/house_district_toplines_2022.csv")Rows: 173565 Columns: 88
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (13): branch, district, forecastdate, expression, name_D1, name_D2, name...
dbl (55): cycle, winner_D1, winner_D2, winner_D3, winner_D4, winner_R1, winn...
lgl (20): name_D3, name_D4, wonrunoff_D1, lostrunoff_D1, wonrunoff_D2, lostr...
ℹ 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.
ga_house_map <- congressional_districts(state = 13, cb = FALSE,
resolution = "500k", year = 2020)
|
| | 0%
|
| | 1%
|
|= | 1%
|
|= | 2%
|
|== | 2%
|
|== | 3%
|
|=== | 4%
|
|=== | 5%
|
|==== | 5%
|
|==== | 6%
|
|===== | 6%
|
|===== | 7%
|
|===== | 8%
|
|====== | 8%
|
|====== | 9%
|
|======= | 9%
|
|======= | 10%
|
|======= | 11%
|
|======== | 11%
|
|======== | 12%
|
|========= | 12%
|
|========= | 13%
|
|========== | 14%
|
|========== | 15%
|
|=========== | 15%
|
|=========== | 16%
|
|============ | 16%
|
|============ | 17%
|
|============ | 18%
|
|============= | 18%
|
|============= | 19%
|
|============== | 19%
|
|============== | 20%
|
|============== | 21%
|
|=============== | 21%
|
|=============== | 22%
|
|================ | 22%
|
|================ | 23%
|
|================= | 24%
|
|================= | 25%
|
|================== | 25%
|
|================== | 26%
|
|=================== | 26%
|
|=================== | 27%
|
|=================== | 28%
|
|==================== | 28%
|
|==================== | 29%
|
|===================== | 29%
|
|===================== | 30%
|
|===================== | 31%
|
|====================== | 31%
|
|====================== | 32%
|
|======================= | 32%
|
|======================= | 33%
|
|======================== | 34%
|
|======================== | 35%
|
|========================= | 35%
|
|========================= | 36%
|
|========================== | 36%
|
|========================== | 37%
|
|========================== | 38%
|
|=========================== | 38%
|
|=========================== | 39%
|
|============================ | 39%
|
|============================ | 40%
|
|============================ | 41%
|
|============================= | 41%
|
|============================= | 42%
|
|============================== | 42%
|
|============================== | 43%
|
|============================== | 44%
|
|=============================== | 44%
|
|================================ | 45%
|
|================================ | 46%
|
|================================= | 46%
|
|================================= | 47%
|
|================================= | 48%
|
|================================== | 48%
|
|================================== | 49%
|
|=================================== | 49%
|
|=================================== | 50%
|
|=================================== | 51%
|
|==================================== | 51%
|
|==================================== | 52%
|
|===================================== | 52%
|
|===================================== | 53%
|
|===================================== | 54%
|
|====================================== | 54%
|
|====================================== | 55%
|
|======================================= | 56%
|
|======================================== | 56%
|
|======================================== | 57%
|
|======================================== | 58%
|
|========================================= | 58%
|
|========================================= | 59%
|
|========================================== | 59%
|
|========================================== | 60%
|
|========================================== | 61%
|
|=========================================== | 61%
|
|=========================================== | 62%
|
|============================================ | 62%
|
|============================================ | 63%
|
|============================================ | 64%
|
|============================================= | 64%
|
|============================================= | 65%
|
|============================================== | 65%
|
|============================================== | 66%
|
|=============================================== | 66%
|
|=============================================== | 67%
|
|=============================================== | 68%
|
|================================================ | 68%
|
|================================================ | 69%
|
|================================================= | 69%
|
|================================================= | 70%
|
|================================================= | 71%
|
|================================================== | 71%
|
|================================================== | 72%
|
|=================================================== | 72%
|
|=================================================== | 73%
|
|=================================================== | 74%
|
|==================================================== | 74%
|
|==================================================== | 75%
|
|===================================================== | 75%
|
|===================================================== | 76%
|
|====================================================== | 76%
|
|====================================================== | 77%
|
|====================================================== | 78%
|
|======================================================= | 78%
|
|======================================================= | 79%
|
|======================================================== | 79%
|
|======================================================== | 80%
|
|======================================================== | 81%
|
|========================================================= | 81%
|
|========================================================= | 82%
|
|========================================================== | 82%
|
|========================================================== | 83%
|
|========================================================== | 84%
|
|=========================================================== | 84%
|
|=========================================================== | 85%
|
|============================================================ | 85%
|
|============================================================ | 86%
|
|============================================================= | 86%
|
|============================================================= | 87%
|
|============================================================= | 88%
|
|============================================================== | 88%
|
|============================================================== | 89%
|
|=============================================================== | 89%
|
|=============================================================== | 90%
|
|=============================================================== | 91%
|
|================================================================ | 91%
|
|================================================================ | 92%
|
|================================================================= | 92%
|
|================================================================= | 93%
|
|================================================================= | 94%
|
|================================================================== | 94%
|
|================================================================== | 95%
|
|=================================================================== | 95%
|
|=================================================================== | 96%
|
|==================================================================== | 97%
|
|==================================================================== | 98%
|
|===================================================================== | 98%
|
|===================================================================== | 99%
|
|======================================================================| 99%
|
|======================================================================| 100%
# i just want the deluxe 538 forecasts
deluxe_districts <- filter(house_districts, expression == '_deluxe')
# it works better if i store the districts in an array before filtering for them idk why
ga_district_array <- c('GA-1', 'GA-2', 'GA-3', 'GA-4', 'GA-5', 'GA-6', 'GA-7',
'GA-8', 'GA-9', 'GA-10', 'GA-11', 'GA-12', 'GA-13', 'GA-14')
# filter for the district, candidate names, and predicted voteshare per party
# also rename the district col
ga_house_predictions <- filter(deluxe_districts, district == ga_district_array) |>
select(district, name_D1, name_R1, voteshare_mean_D1, voteshare_mean_R1) |>
rename(NAMELSAD = district)Warning in district == ga_district_array: longer object length is not a multiple
of shorter object length
# I tried doing Dvoteshare first, but GA-9 is so red that the district showed grey
# make sure the names are the same before the left_join()
ga_house_map <- mutate(ga_house_map,
NAMELSAD = case_when(NAMELSAD == 'Congressional District 1' ~ 'GA-1',
NAMELSAD == 'Congressional District 2' ~ 'GA-2',
NAMELSAD == 'Congressional District 3' ~ 'GA-3',
NAMELSAD == 'Congressional District 4' ~ 'GA-4',
NAMELSAD == 'Congressional District 5' ~ 'GA-5',
NAMELSAD == 'Congressional District 6' ~ 'GA-6',
NAMELSAD == 'Congressional District 7' ~ 'GA-7',
NAMELSAD == 'Congressional District 8' ~ 'GA-8',
NAMELSAD == 'Congressional District 9' ~ 'GA-9',
NAMELSAD == 'Congressional District 10' ~ 'GA-10',
NAMELSAD == 'Congressional District 11' ~ 'GA-11',
NAMELSAD == 'Congressional District 12' ~ 'GA-12',
NAMELSAD == 'Congressional District 13' ~ 'GA-13',
NAMELSAD == 'Congressional District 14' ~ 'GA-14'))
# join the GA map with the ga district prediction data
ga_house_map_predictions <- left_join(ga_house_map, ga_house_predictions, by = "NAMELSAD")
# that is our data all cleaned up, now we just need to make the map