Macroeconomic charts by the Fed using R and Plotly

July 4, 2016
By

(This article was first published on R – Modern Data, and kindly contributed to R-bloggers)

In this post we’ll try to replicate some of the charts created by the Federal Reserve which visualize some well known macroeconomic indicators. We’ll also showcase the new Plotly 4.0 syntax.

Key Macroeconomic Indicators

 

library(plotly)
library(zoo)

df <- read.csv("https://cdn.rawgit.com/plotly/datasets/master/Key%20Macroeconomic%20Indicators.csv", stringsAsFactors = F, check.names = F)
df <- zoo(df[,-1], order.by = as.Date(df[,1], format = "%d/%m/%Y"))

colnames(df) <- c("Unemployment Rate",
                  "Inflation",
                  "Fed Funds Rate (effective)",
                  "recession")
df <- na.fill(df, 0)

df <- data.frame(Date = index(df), df, check.names = F)
df$recession[df$recession %in% 1] <- 30
df$recession[df$recession %in% 0] <- -30

df <- reshape2::melt(df, "Date")
head(df)

p <-
  plot_ly(x = ~Date, y = ~value) %>%

  add_lines(data = df %>% filter(variable != "recession"),
            color = ~variable, line = list(width = 3),
            hoverinfo = "x + y") %>%

  add_lines(data = df %>% filter(variable == "recession"),
            line = list(width = 0),
            fill = "tozerox",
            fillcolor = "rgba(64, 64, 64, 0.2)",
            showlegend = F,
            hoverinfo = "none") %>%

  layout(title = "Key Macroeconomic Indicators",
         legend = list(x = 0.3, y = 0.05, orientation = "h"),
         yaxis = list(title = "", range = c(-5, 20), showgrid = F, zerolinewidth = 2, zeroliecolor = "#b3b3b3",
                      domain = c(0.1, 0.9),
                      showline = T,
                      ticklen = 4),
         xaxis = list(title = "", showgrid = F,
                      showline = T,
                      ticklen = 4,
                      rangeselector = list(x = 0.1, y = 0.95,
                        buttons = list(
                          list(
                            count = 5,
                            label = "5 Y",
                            step = "year",
                            stepmode = "todate"),

                          list(
                            count = 10,
                            label = "10Y",
                            step = "year",
                            stepmode = "todate"),

                          list(
                            count = 15,
                            label = "15 Y",
                            step = "year",
                            stepmode = "todate"),

                          list(
                            step = "all")
                        )
                      )),

         annotations = list(
           list(x = 0.9, y = -0.05,
                xref = "paper", yref = "paper",
                showarrow = F,
                text = 'source: BLS, BEA and Federal Reserve'),

           list(x = -0.05, y = 0.95,
                xref = "paper", yref = "paper",
                showarrow = F,
                text = "Percent"),

           list(x = 0.05, y = 0.98,
                xref = "paper", yref = "paper",
                showarrow = F,
                text = "Zoom")
         ),

         width = 1024,
         height = 600)

Monitory Policy Transmission

 

library(plotly)
library(zoo)

df <- read.csv("https://cdn.rawgit.com/plotly/datasets/master/Monitory%20Policy%20Transmission.csv", stringsAsFactors = F, check.names = F)
df <- zoo(df[,-1], order.by = as.Date(df[,1], format = "%d/%m/%Y"))

colnames(df) <- c("4-Year Auto Loan",
                  "30-Year Mortgage",
                  "1-Year Treasury",
                  "Fed Funds(Effective)",
                  "recession")

#df <- na.fill(df, 0)

df <- data.frame(Date = index(df), df, check.names = F)
df$recession[df$recession %in% 1] <- 30
df$`4-Year Auto Loan` <- c(NA, spline(df$`4-Year Auto Loan`)$y)

df <- reshape2::melt(df, "Date")
head(df)

p <-
  plot_ly(x = ~Date, y = ~value) %>%

  add_lines(data = df %>% filter(variable != "recession"),
            color = ~variable, line = list(width = 3),
            hoverinfo = "x + y") %>%

  add_lines(data = df %>% filter(variable == "recession"),
            line = list(width = 0),
            fill = "tozerox",
            fillcolor = "rgba(64, 64, 64, 0.2)",
            showlegend = F,
            hoverinfo = "none") %>%

  layout(title = "Monitory Policy Transmission",
         legend = list(x = 0.3, y = 0.05, orientation = "h"),
         yaxis = list(title = "", range = c(0, 20), showgrid = F, zerolinewidth = 2, zerolinecolor = "#b3b3b3",
                      domain = c(0.1, 0.9),
                      showline = T,
                      ticklen = 4),
         xaxis = list(title = "", showgrid = F,
                      showline = T,
                      ticklen = 4,
                      rangeselector = list(x = 0.1, y = 0.95,
                                           buttons = list(
                                             list(
                                               count = 5,
                                               label = "5 Y",
                                               step = "year",
                                               stepmode = "todate"),

                                             list(
                                               count = 10,
                                               label = "10Y",
                                               step = "year",
                                               stepmode = "todate"),

                                             list(
                                               count = 15,
                                               label = "15 Y",
                                               step = "year",
                                               stepmode = "todate"),

                                             list(
                                               step = "all")
                                           )
                      )),

         annotations = list(
           list(x = 0.9, y = -0.05,
                xref = "paper", yref = "paper",
                showarrow = F,
                text = 'source: Freddie Mac and Federal Reserve'),

           list(x = -0.05, y = 0.95,
                xref = "paper", yref = "paper",
                showarrow = F,
                text = "Percent"),

           list(x = 0.05, y = 0.98,
                xref = "paper", yref = "paper",
                showarrow = F,
                text = "Zoom")
         ),

         width = 1024,
         height = 600)

Nominal and Real Fed Funds Rate

 

library(plotly)
library(zoo)

df <- read.csv("https://cdn.rawgit.com/plotly/datasets/master/Nominal%20and%20Real%20Fed%20Funds%20Rate.csv", stringsAsFactors = F, check.names = F)
df <- zoo(df[,-1], order.by = as.Date(df[,1], format = "%d/%m/%Y"))

colnames(df) <- c("Fed Funds(Effective)",
                  "Inflation",
                  "Fed Funds(Real)",
                  "recession")

df <- na.fill(df, 0)

df <- data.frame(Date = index(df), df, check.names = F)
df$recession[df$recession %in% 1] <- 30
df$recession[df$recession %in% 0] <- -30

df <- reshape2::melt(df, "Date")
head(df)

p <-
  plot_ly(x = ~Date, y = ~value) %>%

  add_lines(data = df %>% filter(variable != "recession"),
            color = ~variable, line = list(width = 3),
            hoverinfo = "x + y") %>%

  add_lines(data = df %>% filter(variable == "recession"),
            line = list(width = 0),
            fill = "tozerox",
            fillcolor = "rgba(64, 64, 64, 0.2)",
            showlegend = F,
            hoverinfo = "none") %>%

  layout(title = "Nominal and Read Fed Funds Rate",
         legend = list(x = 0.3, y = 0.05, orientation = "h"),
         yaxis = list(title = "", range = c(-10, 25), showgrid = F, zerolinewidth = 2, zerolinecolor = "#b3b3b3",
                      domain = c(0.1, 0.9),
                      ticklen = 4,
                      showline = T),
         xaxis = list(title = "", showgrid = F,
                      showline = T,
                      ticklen = 4,
                      rangeselector = list(x = 0.1, y = 0.95,
                                           buttons = list(
                                             list(
                                               count = 5,
                                               label = "5 Y",
                                               step = "year",
                                               stepmode = "todate"),

                                             list(
                                               count = 10,
                                               label = "10Y",
                                               step = "year",
                                               stepmode = "todate"),

                                             list(
                                               count = 15,
                                               label = "15 Y",
                                               step = "year",
                                               stepmode = "todate"),

                                             list(
                                               step = "all")
                                           )
                      )),

         annotations = list(
           list(x = 0.9, y = -0.05,
                xref = "paper", yref = "paper",
                showarrow = F,
                text = 'source: BEA and Federal Reserve'),

           list(x = -0.05, y = 0.95,
                xref = "paper", yref = "paper",
                showarrow = F,
                text = "Percent"),

           list(x = 0.05, y = 0.98,
                xref = "paper", yref = "paper",
                showarrow = F,
                text = "Zoom")
         ),

         width = 1024,
         height = 600)

Some other examples:



To leave a comment for the author, please follow the link and comment on their blog: R – Modern Data.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)