Harold Nelson
9/23/2019
## ── Attaching packages ───────────────────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 3.1.0 ✔ purrr 0.2.5
## ✔ tibble 1.4.2 ✔ dplyr 0.7.8
## ✔ tidyr 0.8.2 ✔ 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()
This data came from the Federal Reserve Economic Data system (FRED). It is all quarterly beginning with the first quarter of 1990. The first step is to import the data.
## Classes 'tbl_df', 'tbl' and 'data.frame': 148 obs. of 7 variables:
## $ Date : Date, format: "1980-01-01" "1980-04-01" ...
## $ President: chr "Reagan" "Reagan" "Reagan" "Reagan" ...
## $ realgdp : num 1.3 -7.9 -0.6 7.6 8.5 -2.9 4.7 -4.6 -6.5 2.2 ...
## $ lfpr25_54: num 78.7 78.6 78.6 78.7 79.1 79.4 79.1 79.5 79.5 79.8 ...
## $ unrate : num 6.3 7.3 7.7 7.4 7.4 7.4 7.4 8.2 8.8 9.4 ...
## $ infcpi : num 16.7 14.2 7.7 11.7 11.5 8.6 11.6 6.7 3.6 5.9 ...
## $ newjobs : int 316 -437 -408 679 390 238 194 -401 -628 -479 ...
## - attr(*, "spec")=List of 2
## ..$ cols :List of 7
## .. ..$ Date :List of 1
## .. .. ..$ format: chr "%m/%d/%y"
## .. .. ..- attr(*, "class")= chr "collector_date" "collector"
## .. ..$ President: list()
## .. .. ..- attr(*, "class")= chr "collector_character" "collector"
## .. ..$ realgdp : list()
## .. .. ..- attr(*, "class")= chr "collector_double" "collector"
## .. ..$ lfpr25_54: list()
## .. .. ..- attr(*, "class")= chr "collector_double" "collector"
## .. ..$ unrate : list()
## .. .. ..- attr(*, "class")= chr "collector_double" "collector"
## .. ..$ infcpi : list()
## .. .. ..- attr(*, "class")= chr "collector_double" "collector"
## .. ..$ newjobs : list()
## .. .. ..- attr(*, "class")= chr "collector_integer" "collector"
## ..$ default: list()
## .. ..- attr(*, "class")= chr "collector_guess" "collector"
## ..- attr(*, "class")= chr "col_spec"
## Observations: 148
## 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, -7.9, -0.6, 7.6, 8.5, -2.9, 4.7, -4.6, -6.5, 2....
## $ lfpr25_54 <dbl> 78.7, 78.6, 78.6, 78.7, 79.1, 79.4, 79.1, 79.5, 79.5...
## $ unrate <dbl> 6.3, 7.3, 7.7, 7.4, 7.4, 7.4, 7.4, 8.2, 8.8, 9.4, 9....
## $ infcpi <dbl> 16.7, 14.2, 7.7, 11.7, 11.5, 8.6, 11.6, 6.7, 3.6, 5....
## $ newjobs <int> 316, -437, -408, 679, 390, 238, 194, -401, -628, -47...
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'
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'
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: 740
## 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, -7.9, -0.6, 7.6, 8.5, -2.9, 4.7, -4.6, -6.5, 2....
## Observations: 148
## 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, -7.9, -0.6, 7.6, 8.5, -2.9, 4.7, -4.6, -6.5, 2....
## $ lfpr25_54 <dbl> 78.7, 78.6, 78.6, 78.7, 79.1, 79.4, 79.1, 79.5, 79.5...
## $ unrate <dbl> 6.3, 7.3, 7.7, 7.4, 7.4, 7.4, 7.4, 8.2, 8.8, 9.4, 9....
## $ infcpi <dbl> 16.7, 14.2, 7.7, 11.7, 11.5, 8.6, 11.6, 6.7, 3.6, 5....
## $ newjobs <int> 316, -437, -408, 679, 390, 238, 194, -401, -628, -47...
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")
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") +
theme(strip.text.y = element_text(angle = 0))
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")
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")
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'