CSC 530 Lecture 7

Harold Nelson

10/15/2018

Setup

Load all of our usual packages. Add fredr.

library(tidyverse)
## ── Attaching packages ── tidyverse 1.2.1 ──
## ✔ ggplot2 3.0.0     ✔ purrr   0.2.5
## ✔ tibble  1.4.2     ✔ dplyr   0.7.6
## ✔ tidyr   0.8.1     ✔ stringr 1.3.1
## ✔ readr   1.1.1     ✔ forcats 0.3.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(fredr)
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following object is masked from 'package:base':
## 
##     date
# Note: the key is a quoted string
fredr_set_key("2923ccf83015d6b33cfda99a3c83093e")
unratef = fredr_series_observations(
  series_id = "UNRATE",
  observation_start = as.Date("1980-01-01"),
  frequency = "q")

unrate = unratef$value

ung = ggplot(unratef,aes(x=date,y=value)) +
  geom_point(size=.5)
ggplotly(ung)
realgdpf = fredr_series_observations(
  series_id = "A191RL1Q225SBEA",
  observation_start = as.Date("1980-01-01"),
  frequency = "q")

realgdp = realgdpf$value

rg = ggplot(realgdpf,aes(x=date,y=value)) +
  geom_point(size=.5)
ggplotly(rg)
newjobsf = fredr_series_observations(
  series_id = "PAYEMS",
  observation_start = as.Date("1980-01-01"),
  frequency = "q",
  units = "chg")

newjobs = newjobsf$value

rg = ggplot(newjobsf,aes(x=date,y=value)) +
  geom_point(size=.5)
ggplotly(rg)
infcpif = fredr_series_observations(
  series_id = "CPIAUCSL",
  observation_start = as.Date("1980-01-01"),
  frequency = "q",
  units = "chg")

infcpi = infcpif$value

rg = ggplot(infcpif,aes(x=date,y=value)) +
  geom_point(size=.5)
ggplotly(rg)
lfpr25_54f = fredr_series_observations(
  series_id = "LNS11300060",
  observation_start = as.Date("1980-01-01"),
  frequency = "q",
  units = "chg")

lfpr25_54 = lfpr25_54f$value

rg = ggplot(lfpr25_54f,aes(x=date,y=value)) +
  geom_point(size=.5)
ggplotly(rg)
Presidents <- read_csv("Presidents.csv",col_types = cols(Date = col_date(format = "%m/%d/%y")))
Presidents = filter(Presidents,Date <= ymd("2018-04-01"))
View(Presidents)

Make the dataframe.

indicators = data.frame(Presidents,realgdp,
                        infcpi = infcpi[1:154],
                        lfpr25_54 = lfpr25_54[1:154],
                        unrate = unrate[1:154],
                        newjobs = newjobs[1:154])
glimpse(indicators)
## Observations: 154
## Variables: 7
## $ Date      <date> 1980-01-01, 1980-04-01, 1980-07-01, 1980-10-01, 198...
## $ President <chr> "Reagan", "Reagan", "Reagan", "Reagan", "Reagan", "R...
## $ realgdp   <dbl> 1.3, -8.0, -0.5, 7.7, 8.1, -2.9, 4.9, -4.3, -6.1, 1....
## $ infcpi    <dbl> 3.00000000, 2.66666667, 1.53333333, 2.33333333, 2.36...
## $ lfpr25_54 <dbl> 0.43333333, -0.06666667, 0.03333333, 0.10000000, 0.3...
## $ unrate    <dbl> 6.3, 7.3, 7.7, 7.4, 7.4, 7.4, 7.4, 8.2, 8.8, 9.4, 9....
## $ newjobs   <dbl> 315.6667, -436.6667, -408.0000, 679.3333, 390.0000, ...

One Indicator for One President.

indicators %>% filter(President == "Reagan") %>% 
  ggplot(aes(x=Date,y=realgdp)) +
  geom_point() +
  geom_smooth() +
  ggtitle("Reagan - Real GDP Growth Rate")
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

Compare the Presidents on Economic Performance

Produce a time series plot of the data for each variable. Map the presidents’ names to color.

indicators %>% ggplot(aes(x=Date,y=realgdp,col=President)) +
  geom_point() +
  geom_smooth(se=FALSE) + ggtitle("Growth Rate of Real GDP") 
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

indicators %>% ggplot(aes(x=Date,y=lfpr25_54,col=President)) +
  geom_point() +
  geom_smooth(se=FALSE) + ggtitle("Labor Force Participation Rate 25 to 54") 
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

indicators %>% ggplot(aes(x=Date,y=unrate,col=President)) +
  geom_point() +
  geom_smooth(se=FALSE) + ggtitle("Unemployment Rate")
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

indicators %>% ggplot(aes(x=Date,y=infcpi,col=President)) +
  geom_point() +
  geom_smooth(se=FALSE) + ggtitle("Inflation (CPI)") 
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

indicators %>% ggplot(aes(x=Date,y=newjobs,col=President)) +
  geom_point() +
  geom_smooth(se=FALSE) + ggtitle("New Jobs") 
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

Restructure the Data

We need to create a narrow version of our dataframe in which the indicator names are the contents of a variable rather than variable names. The name of the variable will be “indicator” and the values will be in a variable called “value.” Call the resulting dataframe “indg.”

 indicators %>% gather(indicator,value,c(realgdp,lfpr25_54,unrate,infcpi,newjobs)) -> indg
glimpse(indg)
## Observations: 770
## Variables: 4
## $ Date      <date> 1980-01-01, 1980-04-01, 1980-07-01, 1980-10-01, 198...
## $ President <chr> "Reagan", "Reagan", "Reagan", "Reagan", "Reagan", "R...
## $ indicator <chr> "realgdp", "realgdp", "realgdp", "realgdp", "realgdp...
## $ value     <dbl> 1.3, -8.0, -0.5, 7.7, 8.1, -2.9, 4.9, -4.3, -6.1, 1....
glimpse(indicators)
## Observations: 154
## Variables: 7
## $ Date      <date> 1980-01-01, 1980-04-01, 1980-07-01, 1980-10-01, 198...
## $ President <chr> "Reagan", "Reagan", "Reagan", "Reagan", "Reagan", "R...
## $ realgdp   <dbl> 1.3, -8.0, -0.5, 7.7, 8.1, -2.9, 4.9, -4.3, -6.1, 1....
## $ infcpi    <dbl> 3.00000000, 2.66666667, 1.53333333, 2.33333333, 2.36...
## $ lfpr25_54 <dbl> 0.43333333, -0.06666667, 0.03333333, 0.10000000, 0.3...
## $ unrate    <dbl> 6.3, 7.3, 7.7, 7.4, 7.4, 7.4, 7.4, 8.2, 8.8, 9.4, 9....
## $ newjobs   <dbl> 315.6667, -436.6667, -408.0000, 679.3333, 390.0000, ...

First attempt at Facetting

ggplot(indg,aes(x=Date,y=value)) + 
  geom_point() +
  facet_grid(indicator~President)

Fix the Scales

ggplot(indg,aes(x=Date,y=value)) + 
  geom_point() +
  facet_grid(indicator~President,scales = "free")

Fix the Order of the Presidents

The next problem is that the names of the presidents are in alphabetical order. Fix this and redo the graph

indg$President = factor(indg$President,levels = c("Reagan","Bush 41","Clinton","Bush 43","Obama","Trump"))

ggplot(indg,aes(x=Date,y=value)) + 
  geom_point() +
  facet_grid(indicator~President,scales = "free")

Fix the Indicator Labels

indg$indicator=factor(indg$indicator,labels =
  c("Inflation", "Labor Force Participation \n 25-54"," New Jobs","Real GDP \n Growth Rate","Unemployment \n Rate") )
levels(indg$indicator)
## [1] "Inflation"                         
## [2] "Labor Force Participation \n 25-54"
## [3] " New Jobs"                         
## [4] "Real GDP \n Growth Rate"           
## [5] "Unemployment \n Rate"
ggplot(indg,aes(x=Date,y=value)) + 
  geom_point() +
  facet_grid(indicator~President,scales = "free")

Make Indicator Labels Horizontal

ggplot(indg,aes(x=Date,y=value)) + 
  geom_point() +
  facet_grid(indicator~President,scales = "free") +
  theme(strip.text.y = element_text(angle = 0))

Task 10:

Enhance the appearance of the graph by mapping col to the name of the president. The default colors are a bit wimpy, so pick something better using scale_color_brewer.

ggplot(indg,aes(x=Date,y=value,col=President)) + 
  geom_point() +
  facet_grid(indicator~President,scales = "free") +
  theme(strip.text.y = element_text(angle = 0)) +
  scale_color_brewer(palette="Dark2")

Task 11: Eliminate the Legend

ggplot(indg,aes(x=Date,y=value,col=President)) + 
  geom_point() +
  facet_grid(indicator~President,scales = "free") +
  theme(strip.text.y = element_text(angle = 0)) +
  scale_color_brewer(palette="Dark2") +
  theme(legend.position = "none") 

Task 12: Pretty it up and Save.

Play with the parameters to get something you like. Also change the angle of the years on the x-axis to avoid the overprinting. Finally save the result to both a pdf file and a jpg file.

indg %>% ggplot(aes(x=Date,y=value,col=President)) + 
  geom_point(size=.5) +
  geom_smooth(size=.2) +
  facet_grid(indicator~President,scales="free") +
  theme(axis.text.x = element_text(angle=45)) +
  theme(strip.text.y = element_text(angle = 0)) +
  scale_color_brewer(palette="Dark2") +
  theme(legend.position = "none") +
  ggsave("Compare Presidents.jpg" ) +
  ggsave("Compare Presidents.pdf" )
## Saving 8 x 6 in image
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
## Saving 8 x 6 in image
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'