Macroeconomic charts by the Fed using R and Plotly

[This article was first published on R – Modern Data, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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 = "<b>Key Macroeconomic Indicators</b>",
         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: <a href = "http://www.frbsf.org/education/files/CTF_chart_sources.pdf">BLS, BEA and Federal Reserve</a>'),

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

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

         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 = "<b>Monitory Policy Transmission</b>",
         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: <a href = "http://www.frbsf.org/education/files/CTF_chart_sources.pdf">Freddie Mac and Federal Reserve</a>'),

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

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

         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 = "<b>Nominal and Read Fed Funds Rate</b>",
         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: <a href = "http://www.frbsf.org/education/files/CTF_chart_sources.pdf">BEA and Federal Reserve</a>'),

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

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

         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 about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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)