Last active
February 14, 2021 12:39
-
-
Save resulumit/f7f42c2c89222574bcf0c138a5d60f89 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# r code for the graph at | |
# https://twitter.com/ResulUmit/status/1341728281251463174?s=20 | |
# load the packages ------------------------------------------------------- | |
library(dataverse) | |
library(rio) | |
library(tidyverse) | |
# specify which installation ---------------------------------------------- | |
Sys.setenv("DATAVERSE_SERVER" = "dataverse.harvard.edu") | |
# get the data from harvard dataverse ------------------------------------- | |
jw2018 <- get_file("LONG_MI_NATURE_20180111.tab", "doi:10.7910/DVN/8421DX") | |
tmp <- tempfile(fileext = ".dta") | |
writeBin(as.vector(jw2018), tmp) | |
df_jw2018 <- import(tmp) | |
# tidy and graph the data ------------------------------------------------- | |
df_jw2018 %>% | |
filter(daysbeforeED <= 30 & elecdate > as.Date("1999-12-31")) %>% | |
select(polldate, country, electionid, npolls) %>% | |
distinct() %>% | |
group_by(country, electionid) %>% | |
summarise(n_polls = sum(npolls, na.rm = TRUE)) %>% | |
group_by(country) %>% | |
summarise(mean_polls = mean(n_polls, na.rm = TRUE)) %>% | |
arrange(desc(mean_polls), country) %>% | |
mutate(sorting = row_number()) %>% | |
ggplot(aes(x = mean_polls, y = reorder(country, sorting))) + | |
geom_bar(stat = "identity") + | |
coord_flip() + | |
theme_minimal() + | |
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) + | |
labs(x = "Average number of opinion polls,\nthe month before election day, 2000 - 2017\n", y = "", | |
caption = "Data: Jennings & Wlezien (2018)") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment