library(tidyverse)
## ── Attaching packages ────────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 3.2.1     ✔ purrr   0.3.3
## ✔ tibble  2.1.3     ✔ dplyr   0.8.3
## ✔ tidyr   1.0.0     ✔ stringr 1.4.0
## ✔ readr   1.3.1     ✔ forcats 0.4.0
## ── Conflicts ───────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
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
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following object is masked from 'package:base':
## 
##     date
library(fredr)

This creates a function which plots economic indicators for all US presidents beginning with Ronald Reagan. There is code for the function and a few examples of usage.

FRED API

Supply an API key to get access to the FRED data. Read the vignette for the fredr package.

Define the Function

presPerf <- function(
# The default arguments are for the unemployment rate.
series_id = "UNRATE",
frequency = "m",
gtitle = "Unemployment Rate",
units = ""
){
  
# The following code is generic and uses the values in the lines above.


df = fredr_series_observations(
  series_id = series_id,
  observation_start = as.Date("1980-01-01"),
  frequency = frequency,
  units = units)

df = df %>% 
  mutate(yr = year(date),
         President = case_when(
           yr < 1989 ~ "Reagan",
           yr < 1993 ~ "Bush 41",
           yr < 2001 ~ "Clinton",
           yr < 2009 ~ "Bush 43",
           yr < 2017 ~ "Obama",
           TRUE ~ "Trump")
  )

graph = ggplot(df,aes(x=date,
                      y=value,
                      color = President)) +
  geom_point(size=.5) + 
  ggtitle(gtitle)
ggplotly(graph)
}

Real GDP Growth Rate

presPerf(series_id = "A191RL1Q225SBEA",
         frequency = 'q',
         gtitle = "Real GDP Growth Rate")
presPerf(series_id = "ICSA",
         frequency = 'w',
         gtitle = "Initial Unemployment Claims")