challenge_6
Introduction to Visualization
Author

Daniel Manning

Published

January 13, 2023

Code
library(tidyverse)
library(ggplot2)
library(here)
library(lubridate)

knitr::opts_chunk$set(echo = TRUE, warning=FALSE, message=FALSE)

Read in data

Code
fedfunds <- here("posts","_data","FedFundsRate.csv")%>%
  read_csv()
fedfunds
# A tibble: 904 × 10
    Year Month   Day Federal F…¹ Feder…² Feder…³ Effec…⁴ Real …⁵ Unemp…⁶ Infla…⁷
   <dbl> <dbl> <dbl>       <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
 1  1954     7     1          NA      NA      NA    0.8      4.6     5.8      NA
 2  1954     8     1          NA      NA      NA    1.22    NA       6        NA
 3  1954     9     1          NA      NA      NA    1.06    NA       6.1      NA
 4  1954    10     1          NA      NA      NA    0.85     8       5.7      NA
 5  1954    11     1          NA      NA      NA    0.83    NA       5.3      NA
 6  1954    12     1          NA      NA      NA    1.28    NA       5        NA
 7  1955     1     1          NA      NA      NA    1.39    11.9     4.9      NA
 8  1955     2     1          NA      NA      NA    1.29    NA       4.7      NA
 9  1955     3     1          NA      NA      NA    1.35    NA       4.6      NA
10  1955     4     1          NA      NA      NA    1.43     6.7     4.7      NA
# … with 894 more rows, and abbreviated variable names
#   ¹​`Federal Funds Target Rate`, ²​`Federal Funds Upper Target`,
#   ³​`Federal Funds Lower Target`, ⁴​`Effective Federal Funds Rate`,
#   ⁵​`Real GDP (Percent Change)`, ⁶​`Unemployment Rate`, ⁷​`Inflation Rate`

Time Dependent Visualization

I chose to use a scatterplot to display the Effective Federal Funds Rate over time because these are numeric variables and the scatterplot can be used to reveal relationships between them. The scatterplot reveals peaks in Effective Federal Funds Rate during various years, such as 1970, 1975, and 1982.

Code
fedfunds <- fedfunds %>%
  mutate(Date=str_c(Day, 
                    Month, 
                    Year, sep="/"),
         Date=dmy(Date))
fedfunds
# A tibble: 904 × 11
    Year Month   Day Federal F…¹ Feder…² Feder…³ Effec…⁴ Real …⁵ Unemp…⁶ Infla…⁷
   <dbl> <dbl> <dbl>       <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
 1  1954     7     1          NA      NA      NA    0.8      4.6     5.8      NA
 2  1954     8     1          NA      NA      NA    1.22    NA       6        NA
 3  1954     9     1          NA      NA      NA    1.06    NA       6.1      NA
 4  1954    10     1          NA      NA      NA    0.85     8       5.7      NA
 5  1954    11     1          NA      NA      NA    0.83    NA       5.3      NA
 6  1954    12     1          NA      NA      NA    1.28    NA       5        NA
 7  1955     1     1          NA      NA      NA    1.39    11.9     4.9      NA
 8  1955     2     1          NA      NA      NA    1.29    NA       4.7      NA
 9  1955     3     1          NA      NA      NA    1.35    NA       4.6      NA
10  1955     4     1          NA      NA      NA    1.43     6.7     4.7      NA
# … with 894 more rows, 1 more variable: Date <date>, and abbreviated variable
#   names ¹​`Federal Funds Target Rate`, ²​`Federal Funds Upper Target`,
#   ³​`Federal Funds Lower Target`, ⁴​`Effective Federal Funds Rate`,
#   ⁵​`Real GDP (Percent Change)`, ⁶​`Unemployment Rate`, ⁷​`Inflation Rate`
Code
ggplot(fedfunds, aes(Date, `Effective Federal Funds Rate`)) + geom_point()

Code
  labs(title = "Effective Federal Funds Rate by Date", x = "Date", y = "Effective Federal Funds Rate")
$x
[1] "Date"

$y
[1] "Effective Federal Funds Rate"

$title
[1] "Effective Federal Funds Rate by Date"

attr(,"class")
[1] "labels"

Visualizing Part-Whole Relationships

I chose to use a bar chart with the Effective Federal Funds Rate values grouped by month of the year into Seasons in order to investigate any seasonal differences in this variable. Based on the plot, the averages of Effective Federal Funds Rate by season are relatively equal and all are just below 5.

Code
fedfunds <- fedfunds %>%
      mutate(Season = 
              case_when(Month <= 2 | Month == 12 ~ "Winter",
                        Month >= 3 & Month <= 5 ~ "Spring",
                        Month >= 6 & Month <= 8 ~ "Summer",
                        Month >= 9 & Month <= 11 ~ "Fall"))
fedfunds
# A tibble: 904 × 12
    Year Month   Day Federal F…¹ Feder…² Feder…³ Effec…⁴ Real …⁵ Unemp…⁶ Infla…⁷
   <dbl> <dbl> <dbl>       <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
 1  1954     7     1          NA      NA      NA    0.8      4.6     5.8      NA
 2  1954     8     1          NA      NA      NA    1.22    NA       6        NA
 3  1954     9     1          NA      NA      NA    1.06    NA       6.1      NA
 4  1954    10     1          NA      NA      NA    0.85     8       5.7      NA
 5  1954    11     1          NA      NA      NA    0.83    NA       5.3      NA
 6  1954    12     1          NA      NA      NA    1.28    NA       5        NA
 7  1955     1     1          NA      NA      NA    1.39    11.9     4.9      NA
 8  1955     2     1          NA      NA      NA    1.29    NA       4.7      NA
 9  1955     3     1          NA      NA      NA    1.35    NA       4.6      NA
10  1955     4     1          NA      NA      NA    1.43     6.7     4.7      NA
# … with 894 more rows, 2 more variables: Date <date>, Season <chr>, and
#   abbreviated variable names ¹​`Federal Funds Target Rate`,
#   ²​`Federal Funds Upper Target`, ³​`Federal Funds Lower Target`,
#   ⁴​`Effective Federal Funds Rate`, ⁵​`Real GDP (Percent Change)`,
#   ⁶​`Unemployment Rate`, ⁷​`Inflation Rate`
Code
SeasonAverage <- fedfunds %>% 
  group_by(Season) %>% 
  summarise (`Average Effective Federal Funds Rate` = mean(`Effective Federal Funds Rate`, na.rm = TRUE))
ggplot(SeasonAverage, aes(x=Season,y=`Average Effective Federal Funds Rate`)) + 
    geom_bar(stat = "identity")