Skip to content

Instantly share code, notes, and snippets.

@arvi1000
Created June 5, 2019 19:39
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 arvi1000/6403102bee189439041b3eef4f36bd2f to your computer and use it in GitHub Desktop.
Save arvi1000/6403102bee189439041b3eef4f36bd2f to your computer and use it in GitHub Desktop.
library(tidyverse)
# Prep data
# data from https://poll.qu.edu/texas/release-detail?ReleaseID=2625
dat <-
"challenger, d_pct, trump_pct
Biden, 48, 44
Sanders, 44, 47
Warren, 45, 46
Harris, 43, 47
Buttigieg, 44, 46
O'Rourke, 45, 48
Castro, 43, 46" %>%
read_csv
margin_err <- 3.4
dat <- dat %>%
mutate(delta = d_pct - trump_pct,
range_hi = delta + margin_err,
range_low = delta - margin_err)
# per https://en.wikipedia.org/wiki/2016_United_States_presidential_election_in_Texas
hillary_margin_2016 <- 43.24 - 52.23
# Plot
ggplot(dat, aes(x=fct_reorder(challenger, -delta))) +
geom_point(aes(y=delta),
color='dodgerblue') +
geom_linerange(aes(ymin=range_low, ymax=range_hi),
color='dodgerblue', size=1) +
geom_hline(yintercept = 0) +
geom_hline(yintercept = hillary_margin_2016,
linetype = 'dotted', color='dodgerblue') +
annotate('text',
x = 0.5, y = hillary_margin_2016,
hjust=0, vjust = -1,
size=3, color='dodgerblue',
fontface = 'italic',
label = paste0('Hillary-Trump popular vote margin 2016: ',
round(hillary_margin_2016, 1), '%')
) +
labs(title = 'Texas Voter Preference',
subtitle = paste0('Challenger lead over Trump w/ margin of error',
'\nQunnipiac Poll, Jun 5 \'19'),
x = 'challenger', y = '(% for challenger) - (% for Trump)',
caption = paste('Question: If the election for president',
'were being held today, and the candidates were\n',
'{challenger} the Democrat and Donald Trump',
'the Republican, for whom would you vote?')) +
theme_light() +
theme(plot.caption = element_text(face='italic', color='grey30'),
axis.title = element_text(color='grey70', size = 9))
@arvi1000
Copy link
Author

arvi1000 commented Jun 5, 2019

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment