MetricT

Tennessee 2016 Presidential Election results by district

Jun 20th, 2020
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 6.63 KB | None | 0 0
  1. ### We need the following packages for this example.
  2. packages <- c("tidyverse", "politicaldata", "ggplot2", "maps", "cartogram",
  3.               "rgeos", "sf", "dplyr", "tigris", "readxl", "usmap")
  4.  
  5. ### Install packages if needed, then load them quietly
  6. new_packages <- packages[!(packages %in% installed.packages()[, "Package"])]
  7. if (length(new_packages)) install.packages(new_packages, quiet = TRUE)
  8. invisible(lapply(packages, "library",
  9.                  quietly = TRUE,
  10.                  character.only = TRUE,
  11.                  warn.conflicts = FALSE))
  12.  
  13. ### We're trying to map the entire state, so pull the FIPS code for all counties
  14. my_counties <- fips_codes %>% filter(state == "TN") %>% pull(county_code)
  15.  
  16. ### We need to change the CRS when we construct the cartogram later
  17. crs_tn <- "+proj=aea +lon_0=-85.978599 +lat_0=35.830545 +lat_1=34.982972 +lat_2=36.678118"
  18.  
  19. ### Pull a map of county districts, as well as their population
  20. state_map <-
  21.   get_acs(geography   = "county subdivision",
  22.           variables   = c("B01003_001"),   # Census series for population
  23.           state       = "47",
  24.           county      = my_counties,
  25.           year        = 2016,
  26.           geometry    = TRUE,
  27.           cache_table = TRUE) %>%
  28.   rename(population = estimate) %>%
  29.   mutate(state_fips  = substr(GEOID, 1, 2)) %>%
  30.   mutate(county_fips = substr(GEOID, 3, 5)) %>%
  31.   mutate(NAME = gsub(" County, Tennessee", "", NAME)) %>%
  32.   mutate(NAME = gsub("District ", "", NAME)) %>%
  33.   separate(NAME, c("precinct", NA), sep = ",") %>%
  34.   select(state_fips, county_fips, precinct, population) %>%
  35.   mutate(precinct_id = paste(county_fips, ":", precinct, sep = ""))
  36.  
  37. ### Generate a cartogram.   Stop at iteration 21 because the computation starts
  38. ### diverging after that, increasing the error.
  39. cartogram_map <-
  40.   state_map %>%
  41.   st_transform(crs = crs_tn) %>%
  42.   cartogram_cont("population", itermax = 21)
  43.  
  44. ### Read in the election results and start munging.   There's almost certainly
  45. ### a better way of doing this, but it works...
  46. election_2016 <-
  47.   read_xlsx("Data/StateGeneralbyPrecinctNov2016.xlsx") %>%
  48.   filter(OFFICENAME == "United States President") %>%
  49.   select(-OFFICENAME, -ELECTDATE, -ELECTTYPE) %>%
  50.   mutate(fips_code = fips(state = "Tennessee", county = COUNTY)) %>%
  51.   mutate(county_fips = substr(fips_code, 3, 5)) %>%
  52.   filter(county_fips %in% my_counties) %>%
  53.   #filter(!grepl("Absentee|Early Voting|Provisional|Paper Ballots", PRECINCT)) %>%
  54.   rename(OLD_PRECINCT = PRECINCT) %>%
  55.   mutate(PRECINCT = as.integer(PRCTSEQ)) %>%
  56.   select(county_fips, PRECINCT, CANDGROUP,
  57.          RNAME1,  PARTY1, PVTALLY1,
  58.          RNAME2,  PARTY2, PVTALLY2,
  59.          RNAME3,  PARTY3, PVTALLY3,
  60.          RNAME4,  PARTY4, PVTALLY4,
  61.          RNAME5,  PARTY5, PVTALLY5,
  62.          RNAME6,  PARTY6, PVTALLY6,
  63.          RNAME7,  PARTY7, PVTALLY7,
  64.          RNAME8,  PARTY8, PVTALLY8,
  65.          RNAME9,  PARTY9, PVTALLY9,
  66.          RNAME10, PARTY10, PVTALLY10) %>%
  67.   pivot_wider(names_from = "CANDGROUP",
  68.               values_from = c(RNAME1,  PARTY1,  PVTALLY1,
  69.                               RNAME2,  PARTY2,  PVTALLY2,
  70.                               RNAME3,  PARTY3,  PVTALLY3,
  71.                               RNAME4,  PARTY4,  PVTALLY4,
  72.                               RNAME5,  PARTY5,  PVTALLY5,
  73.                               RNAME6,  PARTY6,  PVTALLY6,
  74.                               RNAME7,  PARTY7,  PVTALLY7,
  75.                               RNAME8,  PARTY8,  PVTALLY8,
  76.                               RNAME9,  PARTY9,  PVTALLY9,
  77.                               RNAME10, PARTY10, PVTALLY10)) %>%
  78.   select(county_fips, PRECINCT,
  79.          RNAME1_1,  PVTALLY1_1,
  80.          RNAME2_1,  PVTALLY2_1,
  81.          RNAME3_1,  PVTALLY3_1,
  82.          RNAME4_1,  PVTALLY4_1,
  83.          RNAME5_1,  PVTALLY5_1,
  84.          RNAME6_1,  PVTALLY6_1,
  85.          RNAME7_1,  PVTALLY7_1,
  86.          RNAME8_1,  PVTALLY8_1,
  87.          RNAME9_1,  PVTALLY9_1,
  88.          RNAME10_1, PVTALLY10_1,
  89.          RNAME1_2,  PVTALLY1_2,
  90.          RNAME2_2,  PVTALLY2_2,
  91.          RNAME3_2,  PVTALLY3_2,
  92.          RNAME4_2,  PVTALLY4_2,
  93.          RNAME5_2,  PVTALLY5_2,
  94.          ) %>%
  95.   rename(Donald_Trump = PVTALLY1_1,
  96.          Hillary_Clinton = PVTALLY2_1,
  97.          Roque_De_La_Fuente = PVTALLY3_1,
  98.          Gary_Johnson = PVTALLY4_1,
  99.          Alyson_Kennedy = PVTALLY5_1,
  100.          Mike_Smith = PVTALLY6_1,
  101.          Jill_Stein = PVTALLY7_1,
  102.          Darrell_Castle = PVTALLY8_1,
  103.          Cherunda_Fox = PVTALLY9_1,
  104.          Tom_Hoefling = PVTALLY10_1,
  105.          Kyle_Kenley_Kopitke = PVTALLY1_2,
  106.          Laurence_Kotlikoff = PVTALLY2_2,
  107.          David_Limbaugh = PVTALLY3_2,
  108.          Evan_McMullin = PVTALLY4_2,
  109.          Marshall_Schoenke = PVTALLY5_2) %>%
  110.   select(-starts_with("RNAME")) %>%
  111.   rename(PRECINCT_INT = PRECINCT) %>%
  112.   group_by(PRECINCT_INT, county_fips) %>%
  113.   summarise(across(!matches('PRECINCT_INT|county_fips'), sum)) %>%
  114.   mutate(conservative = Donald_Trump) %>%
  115.   mutate(liberal = Hillary_Clinton) %>%
  116.   #mutate(conservative = Donald_Trump + Gary_Johnson + Evan_McMullin) %>%
  117.   #mutate(liberal = Hillary_Clinton + Jill_Stein) %>%
  118.   select(county_fips, PRECINCT_INT, conservative, liberal) %>%
  119.   mutate(conservative_per = conservative / (conservative + liberal)) %>%
  120.   mutate(liberal_per = liberal / (conservative + liberal)) %>%
  121.   rename(precinct = PRECINCT_INT) %>%
  122.   mutate(precinct_id = paste(county_fips, ":", precinct, sep = ""))
  123.  
  124. map <-
  125.   state_map %>%
  126.   select(precinct_id, geometry, population) %>%
  127.   left_join(election_2016, by = "precinct_id")
  128.  
  129. cartomap <-
  130.   cartogram_map %>%
  131.   select(precinct_id, geometry, population) %>%
  132.   left_join(election_2016, by = "precinct_id")
  133.  
  134. ### Map the vote
  135. g_map <- ggplot(map, col = "black") +
  136.   theme_void() +
  137.   theme(legend.title = element_blank()) +
  138.   theme(plot.title = element_text(hjust = 0.5)) +
  139.   geom_sf(aes(fill = conservative_per), size = 0.1) +
  140.   scale_fill_gradient2(
  141.     midpoint = 0.5,
  142.     labels = scales::percent,
  143.     low  = "steelblue2",
  144.     mid  = "white",
  145.     high = "firebrick1") +
  146.   labs(title = "2016 Presidential Election results by county district")
  147.  
  148. ### Now map it using the cartogram
  149. g_cartogram <- ggplot(cartomap, col = "black") +
  150.   theme_void() +
  151.   theme(legend.title = element_blank()) +
  152.   theme(plot.title = element_text(hjust = 0.5)) +
  153.   geom_sf(aes(fill = conservative_per), size = 0.1) +
  154.   scale_fill_gradient2(
  155.     midpoint = 0.5,
  156.     labels = scales::percent,
  157.     low  = "steelblue2",
  158.     mid  = "white",
  159.     high = "firebrick1") +
  160.   labs(title = "County districts weighted by population")  
  161.  
  162. ### Print the map
  163. plot_grid(g_map, g_cartogram, align = "hv", nrow = 2, ncol = 1)
Add Comment
Please, Sign In to add comment