Using Presidents

Harold Nelson

2025-11-04

Setup

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.2     ✔ tibble    3.2.1
## ✔ lubridate 1.9.4     ✔ tidyr     1.3.1
## ✔ purrr     1.0.4     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(plotly)
## 
## Attaching package: 'plotly'
## 
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## 
## The following object is masked from 'package:stats':
## 
##     filter
## 
## The following object is masked from 'package:graphics':
## 
##     layout
load("Presidents.Rdata")

Graph of Inflation Rate

Produce a time-series graph of infcpi using geom_point().

Solution

Presidents %>% 
  ggplot(aes(x = Date, y = infcpi)) +
  geom_point() +
  ggtitle("Annualized Rate of Inflation")

Color by President

Use color to identify the presidents.

Solution

Presidents %>% 
  ggplot(aes(x = Date, 
             y = infcpi, 
             color = President)) +
  geom_point() +
  ggtitle("Annualized Rate of Inflation")

Add Smoothing

Solution

Presidents %>% 
  ggplot(aes(x = Date, 
             y = infcpi, 
             color = President)) +
  geom_point() + 
  geom_smooth() +
  ggtitle("Annualized Rate of Inflation")
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

Use ggplotly

Solution

g = Presidents %>% 
  ggplot(aes(x = Date, 
             y = infcpi, 
             color = President)) +
  geom_point() +
  ggtitle("Annualized Rate of Inflation")

ggplotly(g)

Add a Faint Line

Also add vertical lines at October, 2008 for the great recession. and April 2020 to mark Covid.

Solution

Presidents %>% 
  ggplot(aes(x = Date, 
             y = infcpi, 
             color = President)) +
  geom_point() +
  geom_vline(xintercept = as.Date("2008-10-1")) +
  geom_vline(xintercept = as.Date("2020-04-01")) +
  geom_line(linewidth = .2,color = "grey") +
  ggtitle("Annualized Rate of Inflation")

## Repeat with Unemployment Rate

Repeat the previous graph with unrate.

Solution

Presidents %>% 
  ggplot(aes(x = Date, 
             y = unrate, 
             color = President)) +
  geom_point() +
  geom_vline(xintercept = as.Date("2008-10-1")) +
  geom_vline(xintercept = as.Date("2020-04-01")) +
  geom_line(linewidth = .2,color = "grey") +
  ggtitle("Unemployment Rate")

The Attributes Approach

Create a dataframe with one record per presidential term. The variables are the name of the president and the mean value of infcpi for the term.

Solution

mean_inflation = Presidents %>% 
  group_by(President) %>% 
  summarize(mean_infcpi = mean(infcpi))
str(mean_inflation)
## tibble [7 × 2] (S3: tbl_df/tbl/data.frame)
##  $ President  : chr [1:7] "Biden" "Bush 41" "Bush 43" "Clinton" ...
##  $ mean_infcpi: num [1:7] 4.96 4.25 2.63 2.59 1.58 ...

Cleveland Plot

Create a graphic with President on the vertical axis and mean_infcpi on the horizontal axis. Order the vertical axis by the values on the horizontal axis. Use geom_point()

Solution

mean_inflation %>% 
  ggplot(aes(x = mean_infcpi,
             y = reorder(President,mean_infcpi))) +
  geom_point() +
  ggtitle("Presidential Terms Ranked by Mean Inflation")

Repeat for Unemployment Rate

Repeat the previous graph for unrate.

Solution

mean_unrate = Presidents %>% 
  group_by(President) %>% 
  summarize(mean_unrate = mean(unrate))

mean_unrate %>% 
  ggplot(aes(x = mean_unrate,
             y = reorder(President,mean_unrate))) +
  geom_point() +
  ggtitle("Presidential Terms Ranked by Mean Unemployment Rate")

Evaluation by Change

Using the summarize functions first() and last() get the values of the unemployment rate at the beginning and end of a presidential term. Use these to calculate delta, the change within each term.

Solution

delta_unrate = Presidents %>% 
  group_by(President) %>% 
  summarize(first_value = first(unrate),
            last_value = last(unrate)) %>% 
  ungroup() %>% 
  mutate(delta = last_value - first_value)
head(delta_unrate)
## # A tibble: 6 × 4
##   President first_value last_value delta
##   <chr>           <dbl>      <dbl> <dbl>
## 1 Biden             6.2        4.1  -2.1
## 2 Bush 41           5.2        7.4   2.2
## 3 Bush 43           4.2        6.9   2.7
## 4 Clinton           7.1        3.9  -3.2
## 5 Obama             8.3        4.8  -3.5
## 6 Reagan            7.4        5.3  -2.1

Graph

Do a Cleveland plot to rank presidential terms by the value of delta.

Solution

delta_unrate %>% 
  ggplot(aes(x = delta,
             y = reorder(President,delta))) +
  geom_point() +
  ggtitle("Presidentail Terms Ranked by \n Change in the Unempyment Rate")