Skip to content

Instantly share code, notes, and snippets.

@jonrobinson2
Created May 12, 2021 02:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonrobinson2/d79a43f88ae1ad88affa893ba55b6df9 to your computer and use it in GitHub Desktop.
Save jonrobinson2/d79a43f88ae1ad88affa893ba55b6df9 to your computer and use it in GitHub Desktop.
require(tabulizer)
require(dplyr)
require(datapasta)
require(ggplot2)
require(gridExtra)
jr=theme(panel.background = element_rect(fill = NA, colour = "black", size = 0.25),
panel.border = element_blank(),
panel.grid.major = element_line(colour = "black", size = 0.05),
panel.grid.minor = element_line(colour = "black", size = 0.05),
plot.title=element_text(size=22, family="Helvetica Neue Light"),
axis.title.x=element_text(size=18, family="Helvetica Neue Light"),
axis.text.x=element_text(colour="black", size=18, family="Helvetica Neue Light"),
axis.title.y=element_text(size=18, family="Helvetica Neue Light"),
axis.text.y=element_text(colour="black",size=18, family="Helvetica Neue Light"),
strip.text.x = element_text(size = 22,family="Helvetica Neue Light"),
strip.text.y = element_text(size = 22,family="Helvetica Neue Light"),
legend.title = element_text(size=18, family="Helvetica Neue Light",face="bold"),
legend.text = element_text(size=18, family="Helvetica Neue Light"),
strip.background = element_rect(colour = "grey", fill = "white"))
covi = extract_tables(file = 'https://www.liebertpub.com/doi/suppl/10.1089/elj.2017.0478/suppl_file/Supp_Appendix.pdf')
vars = covi[[1]][1,]
covi = rbind(rbind(data.frame(covi[[1]][2:nrow(covi[[1]]),]),data.frame(covi[[2]])), data.frame(covi[[3]]))
names(covi) = vars
covi[,2:ncol(covi)] = apply(covi[,2:ncol(covi)],2, as.numeric)
covi = merge(covi, data.frame(
stringsAsFactors = FALSE,
State = c("OR","WA","UT","IL",
"MD","HI","CO","ND","VT","CA","MA","VA","MI","ME",
"MN","NJ","NY","ID","RI","CT","MT","NE","NC",
"IA","WI","WY","LA","NV","SD","AZ","NM","PA",
"DE","OH","WV","AK","OK","KS","AL","FL","IN",
"AR","KY","SC","NH","TN","MS","MO","GA","TX"),
'2020 COVI' = c(-1.69,-1.66,-0.93,-0.85,
-0.74,-0.71,-0.69,-0.66,-0.64,-0.62,-0.52,-0.51,
-0.5,-0.39,-0.39,-0.26,-0.25,-0.12,-0.1,-0.07,
-0.07,-0.05,-0.03,0.05,0.05,0.08,0.15,0.17,
0.19,0.21,0.25,0.29,0.32,0.32,0.33,0.34,0.37,0.38,
0.46,0.46,0.51,0.54,0.61,0.74,0.78,0.8,0.86,
0.92,0.98,1.29)
) ,by='State', all=T)
covi$Rank_2016 = rank(-covi$`2016 COVI`, ties.method = 'first')
covi$Rank_2020 = rank(-covi$X2020.COVI, ties.method = 'first')
covi$Rank_Diff = covi$Rank_2020 - covi$Rank_2016
covi$Covi_Diff = covi$X2020.COVI - covi$`2016 COVI`
#######
cvep = data.frame(
stringsAsFactors = FALSE,
check.names = FALSE,
State = c("Alabama","Alaska","Arizona","Arkansas",
"California","Colorado","Connecticut","Delaware",
"District of Columbia","Florida","Georgia","Hawaii",
"Idaho","Illinois","Indiana","Iowa","Kansas","Kentucky",
"Louisiana","Maine","Maryland","Massachusetts",
"Michigan","Minnesota","Mississippi","Missouri","Montana",
"Nebraska","Nevada","New Hampshire","New Jersey",
"New Mexico","New York","North Carolina","North Dakota",
"Ohio","Oklahoma","Oregon","Pennsylvania",
"Rhode Island","South Carolina","South Dakota","Tennessee",
"Texas","Utah","Vermont","Virginia","Washington",
"West Virginia","Wisconsin","Wyoming"),
"VEP_2016" = c("59.10%","61.50%","56.10%","53.20%",
"58.20%","71.90%","64.90%","64.40%","60.70%","65.60%",
"59.80%","43.20%","60.90%","63.10%","57.90%",
"69.10%","59.70%","59.60%","60.60%","72.90%","67.00%",
"68.30%","65.70%","74.70%","55.20%","62.20%","64.30%",
"64.00%","57.40%","72.50%","65.80%","54.90%",
"57.20%","64.90%","61.70%","64.20%","52.30%","68.00%",
"63.60%","59.70%","57.30%","60.00%","51.80%","51.40%",
"57.90%","64.80%","66.10%","65.70%","50.20%",
"69.50%","60.20%"),
'VEP_2020' = c("63.10%","68.80%","65.90%","56.10%",
"68.50%","76.40%","71.50%","70.70%","64.10%","71.70%",
"68.00%","57.50%","68.00%","67.00%","61.40%",
"73.20%","65.90%","64.90%","64.30%","76.30%","71.10%",
"72.10%","73.90%","80.00%","60.20%","65.70%","73.10%",
"69.90%","65.40%","75.50%","75.30%","61.30%",
"63.60%","71.50%","64.50%","67.40%","55.00%","75.50%",
"71.10%","65.30%","64.50%","66.00%","59.80%","60.40%",
"69.20%","74.20%","73.00%","75.70%","57.60%",
"75.80%","64.60%"))
cvep$VEP_2016 = as.numeric(gsub(pattern = '%',replacement = '',cvep$VEP_2016))/100
cvep$VEP_2020 = as.numeric(gsub(pattern = '%',replacement = '',cvep$VEP_2020))/100
cvep = cvep[cvep$State!='District of Columbia',]
cvep = merge(cvep,data.frame(StateName = state.abb, State = state.name),by='State')
cvep$Rank_2016 = rank(-cvep$VEP_2016,ties.method = 'first')
cvep$Rank_2020 = rank(-cvep$VEP_2020, ties.method = 'first')
cvep$Rank_Diff = cvep$Rank_2020 - cvep$Rank_2016
cvep$VEP_Diff = cvep$VEP_2020 - cvep$VEP_2016
cvep$State=NULL
full = merge(cvep, covi, by.x='StateName', by.y='State', all = T, suffixes = c('_Turnout','_COVI'))
grid.arrange(
ggplot(full, aes(Rank_Diff_COVI, Rank_Diff_Turnout)) +
geom_text(aes(label=StateName), alpha=.5, size=4) +
jr +
geom_smooth(method='lm', se=F, col='black', size=.5) +
scale_y_continuous('Change in VEP Turnout Rank \n Source: US Elections Project \n (Positive is Increased Turnout)',
limits=c(-25, 25)) +
scale_x_continuous('Change in Cost of Voting Index Rank \n (Positive Decreased Costs)',
limits=c(-50, 50)) +
geom_hline(yintercept = 0, linetype=2) + geom_vline(xintercept = 0, linetype=2) +
ggtitle('Comparing CVEP Turnout & \n Cost of Voting Index Rank Changes \n (2016 to 2020)')
,
ggplot(full, aes(-Covi_Diff, VEP_Diff)) +
geom_text(aes(label=StateName), alpha=.5, size=4, col='forestgreen') +
jr +
geom_smooth(method='lm', se=F, col='forestgreen', size=.5) +
scale_y_continuous('Change in VEP Turnout \n Source: US Elections Project',
limits=c(0, .2)) +
scale_x_continuous('Change in Cost of Voting Index \n (Positive Decreases Costs)',
limits=c(-2, 2)) +
geom_hline(yintercept = 0, linetype=2, col='forestgreen') + geom_vline(xintercept = 0, linetype=2, col='forestgreen') +
ggtitle('Comparing CVEP Turnout & \n Cost of Voting Index Changes \n (2016 to 2020)')
,
nrow=1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment