Skip to content

Instantly share code, notes, and snippets.

@sysilviakim
Created September 21, 2020 09:07
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 sysilviakim/5aa7321c40fbd3e81d715f1ddb7444b5 to your computer and use it in GitHub Desktop.
Save sysilviakim/5aa7321c40fbd3e81d715f1ddb7444b5 to your computer and use it in GitHub Desktop.
Comparison of State-Level Two-Party Presidential Vote Share: 2012 and 2016
# Library and data import ======================================================
library(tidyverse)
library(scales)
library(ggrepel)
library(Kmisc) # devtools::install_github("sysilviakim/Kmisc")
options(digits = 5, scipen = 999)
# MEDSL Data: U.S. President 1976–2016
# https://doi.org/10.7910/DVN/42MVDX
# This data file contains constituency (state_po-level) returns for elections
# to the U.S. presidency from 1976 to 2016.
url <- 'https://dataverse.harvard.edu/api/access/datafile/3444051?gbrecs=false'
pres_raw <- read.csv(url, sep = "\t")
# Filter data ==================================================================
pres_df <- pres_raw %>%
filter(year == 2012 | year == 2016) %>%
filter(candidate != "" & writein == FALSE) %>%
mutate(
party = ifelse(party == "democratic-farmer-labor", "democrat", party)
) %>%
filter(party == "democrat" | party == "republican") %>%
group_by(year, state_po) %>%
# Since the unit of analysis is candidate-state_po-year, create vote shares
summarise(
vote_share = candidatevotes[party == "democrat"] / (
candidatevotes[party == "democrat"] +
candidatevotes[party == "republican"]
)
) %>%
# Now since we have years 2012 and 2016, but want to make it scatterplot,
# pivot_wider by each state_po
pivot_wider(names_from = year, values_from = vote_share)
# Scatterplot ==================================================================
p <- pres_df %>%
mutate(
type = case_when(
`2012` < 0.5 & `2016` < 0.5 ~ 1,
`2012` < 0.5 & `2016` >= 0.5 ~ 2,
`2012` >= 0.5 & `2016` < 0.5 ~ 3,
`2012` >= 0.5 & `2016` >= 0.5 ~ 4
),
type = factor(
type,
levels = seq(4),
labels = c(
"Consistently Republican", "Republican --> Democrat",
"Democrat --> Republican", "Consistently Democrat"
)
)
) %>%
ggplot(aes(x = `2012`, y = `2016`, colour = type, label = state_po)) +
geom_point() +
theme_bw() +
scale_x_continuous(labels = scales::percent) +
scale_y_continuous(labels = scales::percent) +
# geom_abline(intercept = 0, slope = 1) +
geom_vline(xintercept = .5, linetype = "dotted") +
geom_hline(yintercept = .5, linetype = "dotted") +
# scale_colour_brewer(type = "div", palette = 5) +
scale_colour_manual(
values = c(
"Consistently Republican" = "#ca0020",
"Republican --> Democrat" = "#92c5de",
"Democrat --> Republican" = "#f4a582",
"Consistently Democrat" = "#0571b0"
)
) +
# stat_summary(fun.data = mean_cl_normal) +
# geom_smooth(method = 'lm', formula = y ~ x)
xlab("2012 Two-Party Vote Share") +
ylab("2016 Two-Party Vote Share") +
theme(legend.title = element_blank())
# Export, with labels or without ===============================================
pdf("pres_scatter_nolabel.pdf", width = 6, height = 4)
Kmisc::pdf_default(p) +
theme(legend.position = "botton")
dev.off()
pdf("pres_scatter_labeled.pdf", width = 6, height = 4)
Kmisc::pdf_default(p) +
geom_text_repel(show.legend = FALSE) +
theme(legend.position = "botton")
dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment