Skip to content

Instantly share code, notes, and snippets.

@gka
Last active December 3, 2021 02:02
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save gka/c346926f4a7c011ac64c1107765133ca to your computer and use it in GitHub Desktop.
used this script to prepare the data for this chart https://www.datawrapper.de/_/Wa2Ci/
needs(dplyr, tidyr, readr, jsonlite)
trump_ratings <- read_csv('https://projects.fivethirtyeight.com/trump-approval-data/approval_topline.csv') %>%
filter(subgroup=='All polls') %>%
mutate(date=as.Date(modeldate, format='%m/%d/%Y'),
approve=as.numeric(approve_estimate),
disapprove=as.numeric(disapprove_estimate)) %>%
select(date, approve, disapprove, president)
ratings <- read_json('https://projects.fivethirtyeight.com/trump-approval-data/historical-approval.json', T) %>%
mutate(date=as.Date(date),
approve=as.numeric(approve_estimate),
disapprove=as.numeric(disapprove_estimate))%>%
select(date, approve, disapprove, president) %>%
bind_rows(trump_ratings) %>%
group_by(date=as.Date(paste0(format(date, '%Y-%m'),'-01'))) %>%
summarise(president=last(president), approve=mean(approve), disapprove=mean(disapprove))
ratings %>%
select(date, president, approve) %>%
filter(date>='1961-02-01') %>%
mutate(president=as.factor(as.character(president))) %>%
spread(president, approve) %>%
write_tsv('ratings.csv', na = '')
@bondlavern
Copy link

You did such a great job with that map Greg. I'm trying to duplicate it in Tableau. It's not that easy. Thanks for the inspiration though.

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