Quantcast
Channel: Plotly – Modern Data
Viewing all articles
Browse latest Browse all 48

Macroeconomic charts by the Fed using R and Plotly

$
0
0

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 %

  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)

 


Monitary Policy Transmission

library(plotly)
library(zoo)

df %

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

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

  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 %

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

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

  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:




Viewing all articles
Browse latest Browse all 48

Trending Articles