Skip to content

Instantly share code, notes, and snippets.

@yeedle
Last active August 2, 2017 14:30
Show Gist options
  • Save yeedle/3b7c5442550f1aa3783254b426c9d33c to your computer and use it in GitHub Desktop.
Save yeedle/3b7c5442550f1aa3783254b426c9d33c to your computer and use it in GitHub Desktop.
comparing the rasmussen approval index of obama and trump
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)))
@yeedle
Copy link
Author

yeedle commented Jun 19, 2017

@yeedle
Copy link
Author

yeedle commented Aug 2, 2017

Changed geom_path() + geom_point() to ggalt::geom_xspline():
approval

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