Last active
August 2, 2017 14:30
-
-
Save yeedle/3b7c5442550f1aa3783254b426c9d33c to your computer and use it in GitHub Desktop.
comparing the rasmussen approval index of obama and trump
This file contains 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
library(tidyverse) | |
library(rvest) | |
library(lubridate) | |
library(ggalt) | |
obama_url <- "http://www.rasmussenreports.com/public_content/politics/obama_administration/obama_approval_index_history" | |
trump_url <- "http://www.rasmussenreports.com/public_content/politics/trump_administration/trump_approval_index_history" | |
index <- list(obama = obama_url, trump = trump_url) %>% | |
map_df(~ read_html(.x) %>% | |
html_node(".renderedtable") %>% | |
html_table() %>% | |
select(-`Approval Index`), | |
.id = "president") %>% | |
mutate( | |
total_approval = `Total Approve` %>% | |
str_extract("\\d+") %>% | |
as.integer(), | |
date = as.Date(Date, "%d-%b-%y") | |
) %>% | |
select(president, date, total_approval) | |
# split the data frame into two | |
obama_index <- index %>% | |
filter(president == "obama") | |
trump_index <- index %>% | |
filter(president == "trump") | |
# augment obama's dates to match trump's | |
aug_index <- obama_index %>% | |
mutate(date = date + years(8)) %>% | |
filter(date %>% between(min(trump_index$date), max(trump_index$date))) %>% | |
bind_rows(trump_index) | |
ggplot(aug_index, aes(date, total_approval, color = president)) + | |
geom_xspline() + | |
scale_x_date(date_labels = "%b %d", date_breaks = "2 weeks") + | |
scale_color_hue(h = c(360, 0) + 15) + | |
hrbrthemes::theme_ipsum() + | |
labs( | |
title = "Rasmussen Presidential Approval Index", | |
x = "date (obama: '09, trump: '17)", | |
y = "total approval", | |
caption = "source: http://www.rasmussenreports.com" | |
) + | |
guides(color = guide_legend(override.aes = list(linetype = 0))) |
Author
yeedle
commented
Jun 19, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment