---
title: "Case Study: US Presidential Popularity"
author: "Keith VanderLinden"
date: "Spring, 2022"
output: html_document
---

```{r warning=FALSE, message=FALSE}
library(tidyverse)
```

This document presents an analysis of recent presidential popularity as inspired by examples from [FiveThirtyEight](https://projects.fivethirtyeight.com/biden-approval-rating/) &amp; [Data Science in a Box](https://datasciencebox.org/). Here&rsquo;s the target plot, which appeared on the website on October 4, 2021.

![Biden Approval](images/biden-approval.png)

This web plot has a pull-down box for the poll type, and the plotted values are animated. We won&rsquo;t be able to reproduce that directly in a static ggplot.

We will base our analysis on the assumption that the approval ratings in the FiveThirtyEight datasets are accurate and useful. For a discussion of how the data were collected and processed, see this article by N. Rakich:  [How We’re Tracking Joe Biden’s Approval Rating ](https://fivethirtyeight.com/features/how-were-tracking-joe-bidens-approval-rating/).

## Loading the Dataset

The dataset was downloaded directly from the FiveThirtyEight website. 

```{r warning=FALSE, message=FALSE}
approval_raw_biden <- read_csv("data/approval_topline_biden.csv")
approval_raw_biden
```

We&rsquo;ll focus on the approval estimates over time, renaming some columns, processing the date character string, and ensuring a consistent spelling of Biden&rsquo;s name.

```{r}
approval_biden <- approval_raw_biden %>% 
  select(president,
         subgroup, 
         date = modeldate, 
         approval = approve_estimate, 
         disapproval = disapprove_estimate) %>% 
  mutate(date = lubridate::mdy(date),
         president = "Joe Biden",
         ) %>% 
  filter(subgroup != "All polls")
approval_biden

approval_biden %>% 
  distinct(president)
```

## Restructuring the Dataset

We note that the ratings values are split between two columns, which doesn&rsquo;t allow us to easily plot both approval and disapproval ratings in a single, 2D graph. To do this, we need all the rating values to be in a single column with an additional column indicating the rating type, approval or disapproval. 

```{r output.lines=11}
approval_longer_biden <- approval_biden %>%
  pivot_longer(
    cols = c(approval, disapproval),
    names_to = "rating_type",
    values_to = "rating_value"
  )
approval_longer_biden
```

One should ask which dataset is properly *tidy*, the original dataset or this restructured dataset. In some sense, the approval/disapproval values are all ratings, but adding them up or averaging them makes little sense, even if we represent disapprovals as negative ratings. Also, it may be easier to add additional rating types to the new structure because the original requires adding more columns, but, again, it&rsquo;s not clear that this modification makes the dataset more tidy (n.b., the original dataset had additional rating types in separate columns). Consequently, we acknowledge that for the purposes of this example, we&rsquo;ve pivoted the data to present it, not to tidy it.

## Plotting the Dataset

We&rsquo;re now ready to re-engineer an approximation of FiveThirtyEight&rsquo;s original plot.

```{r}
approval_longer_biden %>%
  ggplot() +
  aes(x = date, 
      y = rating_value, 
      color = rating_type,
      ) +
  geom_line() +
  facet_wrap(vars(subgroup)) +
  scale_color_manual(values = c("darkgreen", "orange")) +
  labs(
    x = "Date", y = "Rating",
    color = NULL,
    title = "How (un)popular is Joe Biden?",
    subtitle = "Estimates based on polls of all adults and polls of likely/registered voters",
    caption = "Source: FiveThirtyEight modeling estimates"
  ) +
  theme_minimal()
```

## Comparing Presidential Approval Ratings

FiveThirtyEight also presents plots of approval data for some previous presidents, back through H. Truman (see: [How Biden compares with past presidents](https://projects.fivethirtyeight.com/trump-approval-ratings/)). We&rsquo;ve downloaded the available data, which only goes back through D. Trump, and processed it in a manner similar to what we did for J. Biden&rsquo;s approval data.

```{r warning=FALSE, message=FALSE}
approval_raw_trump <- read_csv("data/approval_topline_trump.csv")

approval_longer_trump <- approval_raw_trump %>% 
  select(president,
         subgroup, 
         date = modeldate, 
         approval = approve_estimate, 
         disapproval = disapprove_estimate) %>% 
  mutate(date = lubridate::mdy(date)) %>% 
  filter(subgroup != "All polls") %>%
  pivot_longer(
    cols = c(approval, disapproval),
    names_to = "rating_type",
    values_to = "rating_value"
  )

approval_longer_trump
```

To compare the presidents, we combine these datasets into one using the union function, which produces the set union of the records in the two datasets. Here, it is important that the two datasets have exactly the same columns.

```{r}
approval_trump_biden <- bind_rows(approval_longer_trump,
                              approval_longer_biden
                              )
approval_trump_biden
```

Now, we can reproduce the approval chart for the past two presidents as a time series.

```{r}
approval_trump_biden %>%
  ggplot() +
  aes(x = date, 
      y = rating_value, 
      color = rating_type,
      ) +
  geom_line() +
  facet_grid(vars(subgroup)) +
  scale_color_manual(values = c("darkgreen", "orange")) +
  labs(
    x = "Date", y = "Rating",
    color = NULL,
    title = "How (un)popular are Trump (2017-2021) & Biden (2021-present)?",
    subtitle = "Estimates based on polls of all adults and polls of likely/registered voters",
    caption = "Source: FiveThirtyEight modeling estimates"
  ) +
  theme_minimal()
```

We see here that the approval ratings of republican D. Trump and democrat J. Biden reversed when Biden took office in January 2021, but have seen another reversal in 2022. 
