Last active
May 29, 2019 21:04
-
-
Save k5cents/eb753c5c561cff4284eda8f3080343ca to your computer and use it in GitHub Desktop.
Past NPVIC Attempts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(janitor) | |
library(rvest) | |
npvic_wiki <- read_html("https://en.wikipedia.org/wiki/National_Popular_Vote_Interstate_Compact") | |
enacted <- npvic_wiki %>% | |
html_node("table.wikitable:nth-child(71)") %>% | |
html_table() %>% | |
as_tibble(.name_repair = make_clean_names) %>% | |
rename(evs = current_electoralvotes_ev) %>% | |
filter(no %>% str_detect("^\\d")) %>% | |
mutate( | |
date_adopted = parse_date(date_adopted, "%b %d, %Y"), | |
method_of_adoption = str_remove(method_of_adoption, "\\[[^\\)]+\\]"), | |
no = parse_number(no), | |
evs = parse_number(evs) | |
) | |
pli <- read_csv( | |
"https://raw.githubusercontent.com/fivethirtyeight/data/master/partisan-lean/fivethirtyeight_partisan_lean_STATES.csv" | |
) | |
past_attempts <- npvic_wiki %>% | |
html_node("table.wikitable:nth-child(85)") %>% | |
html_table(fill = TRUE) %>% | |
as_tibble(.name_repair = make_clean_names) %>% | |
rename(evs = e_vs) %>% | |
left_join(pli, by = "state") %>% | |
mutate(bill = str_remove(bill, "\\[[^\\)]+\\]")) %>% | |
mutate(session = as.numeric(str_remove_all(session, "–(.*)"))) %>% | |
filter(outcome != "Law") %>% | |
select(-outcome) %>% | |
separate( | |
col = pvi_538, | |
into = c("party", "lean"), | |
remove = TRUE, | |
sep = "\\+", | |
convert = TRUE | |
) %>% | |
mutate(lean = if_else( | |
condition = party == "D", | |
true = (lean * -1L), | |
false = lean) | |
) %>% | |
filter(!state %in% enacted$jurisdiction) %>% | |
mutate( | |
state = state.abb[match(state, state.name)], | |
evs = parse_number(evs), | |
lower_house = na_if(lower_house, "—"), | |
upper_house = na_if(upper_house, "—"), | |
executive = na_if(executive, "—"), | |
lower_house = str_detect(lower_house, "Passed"), | |
upper_house = str_detect(upper_house, "Passed"), | |
) | |
past_attempts %>% | |
knitr::kable() | |
past_attempts %>% | |
select(state, evs, lean) %>% | |
distinct() %>% | |
filter(lean < 0) %>% | |
pull(evs) %>% | |
sum() %>% | |
magrittr::add(sum(enacted$evs)) | |
past_attempts %>% | |
group_by(state, evs, lean) %>% | |
count() %>% | |
ggplot(mapping = aes(x = lean, y = evs)) + | |
geom_vline(xintercept = 0) + | |
geom_label(mapping = aes(label = state, fill = lean, size = n)) + | |
scale_fill_gradient(low = "blue", high = "red", guide = FALSE) + | |
scale_size_continuous(range = c(4, 8)) + | |
labs( | |
title = "States with Failed NPVIC Legislation Attempts", | |
subtitle = "With FiveThirtyEight Partisan Lean", | |
caption = "Source: Wikipedia & FiveThirtyEight", | |
size = "Attempts" | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment