<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/macroeconomic-charts-by-the-fed-using-r-and-plotly/"></div></div><div><a data-url="http://www.r-bloggers.com/macroeconomic-charts-by-the-fed-using-r-and-plotly/" data-counturl="http://www.r-bloggers.com/macroeconomic-charts-by-the-fed-using-r-and-plotly/" data-count="horizontal" data-text="Macroeconomic charts by the Fed using R and Plotly" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://moderndata.plot.ly/macroeconomic-charts-by-the-fed-using-r-and-plotly/"> R – Modern Data</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>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 <b>Plotly 4.0 syntax</b>.</p>
<h2>Key Macroeconomic Indicators</h2>
<p><code>
<pre>library(plotly)
library(zoo)

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

colnames(df) &lt;- c(&quot;Unemployment Rate&quot;,
                  &quot;Inflation&quot;,
                  &quot;Fed Funds Rate (effective)&quot;,
                  &quot;recession&quot;)
df &lt;- na.fill(df, 0)

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

df &lt;- reshape2::melt(df, &quot;Date&quot;)
head(df)

p &lt;-
  plot_ly(x = ~Date, y = ~value) %>%

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

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

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

                          list(
                            count = 10,
                            label = &quot;10Y&quot;,
                            step = &quot;year&quot;,
                            stepmode = &quot;todate&quot;),

                          list(
                            count = 15,
                            label = &quot;15 Y&quot;,
                            step = &quot;year&quot;,
                            stepmode = &quot;todate&quot;),

                          list(
                            step = &quot;all&quot;)
                        )
                      )),

         annotations = list(
           list(x = 0.9, y = -0.05,
                xref = &quot;paper&quot;, yref = &quot;paper&quot;,
                showarrow = F,
                text = 'source: &lt;a href = &quot;http://www.frbsf.org/education/files/CTF_chart_sources.pdf&quot;>BLS, BEA and Federal Reserve&lt;/a>'),

           list(x = -0.05, y = 0.95,
                xref = &quot;paper&quot;, yref = &quot;paper&quot;,
                showarrow = F,
                text = &quot;&lt;b>Percent&lt;/b>&quot;),

           list(x = 0.05, y = 0.98,
                xref = &quot;paper&quot;, yref = &quot;paper&quot;,
                showarrow = F,
                text = &quot;&lt;b>Zoom&lt;/b>&quot;)
         ),

         width = 1024,
         height = 600)</pre>
<p><br>
<iframe height="600" width="1024" src="https://plot.ly/~riddhiman/353.embed"></iframe></p>
<h2>Monitory Policy Transmission</h2>
<p><code>
<pre>library(plotly)
library(zoo)

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

colnames(df) &lt;- c(&quot;4-Year Auto Loan&quot;,
                  &quot;30-Year Mortgage&quot;,
                  &quot;1-Year Treasury&quot;,
                  &quot;Fed Funds(Effective)&quot;,
                  &quot;recession&quot;)

#df &lt;- na.fill(df, 0)

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

df &lt;- reshape2::melt(df, &quot;Date&quot;)
head(df)

p &lt;-
  plot_ly(x = ~Date, y = ~value) %>%

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

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

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

                                             list(
                                               count = 10,
                                               label = &quot;10Y&quot;,
                                               step = &quot;year&quot;,
                                               stepmode = &quot;todate&quot;),

                                             list(
                                               count = 15,
                                               label = &quot;15 Y&quot;,
                                               step = &quot;year&quot;,
                                               stepmode = &quot;todate&quot;),

                                             list(
                                               step = &quot;all&quot;)
                                           )
                      )),

         annotations = list(
           list(x = 0.9, y = -0.05,
                xref = &quot;paper&quot;, yref = &quot;paper&quot;,
                showarrow = F,
                text = 'source: &lt;a href = &quot;http://www.frbsf.org/education/files/CTF_chart_sources.pdf&quot;>Freddie Mac and Federal Reserve&lt;/a>'),

           list(x = -0.05, y = 0.95,
                xref = &quot;paper&quot;, yref = &quot;paper&quot;,
                showarrow = F,
                text = &quot;&lt;b>Percent&lt;/b>&quot;),

           list(x = 0.05, y = 0.98,
                xref = &quot;paper&quot;, yref = &quot;paper&quot;,
                showarrow = F,
                text = &quot;&lt;b>Zoom&lt;/b>&quot;)
         ),

         width = 1024,
         height = 600)</pre>
<p><br>
<iframe height="600" width="1024" src="https://plot.ly/~riddhiman/355.embed"></iframe></p>
<h2>Nominal and Real Fed Funds Rate </h2>
<p><code>
<pre>library(plotly)
library(zoo)

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

colnames(df) &lt;- c(&quot;Fed Funds(Effective)&quot;,
                  &quot;Inflation&quot;,
                  &quot;Fed Funds(Real)&quot;,
                  &quot;recession&quot;)

df &lt;- na.fill(df, 0)

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

df &lt;- reshape2::melt(df, &quot;Date&quot;)
head(df)

p &lt;-
  plot_ly(x = ~Date, y = ~value) %>%

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

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

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

                                             list(
                                               count = 10,
                                               label = &quot;10Y&quot;,
                                               step = &quot;year&quot;,
                                               stepmode = &quot;todate&quot;),

                                             list(
                                               count = 15,
                                               label = &quot;15 Y&quot;,
                                               step = &quot;year&quot;,
                                               stepmode = &quot;todate&quot;),

                                             list(
                                               step = &quot;all&quot;)
                                           )
                      )),

         annotations = list(
           list(x = 0.9, y = -0.05,
                xref = &quot;paper&quot;, yref = &quot;paper&quot;,
                showarrow = F,
                text = 'source: &lt;a href = &quot;http://www.frbsf.org/education/files/CTF_chart_sources.pdf&quot;>BEA and Federal Reserve&lt;/a>'),

           list(x = -0.05, y = 0.95,
                xref = &quot;paper&quot;, yref = &quot;paper&quot;,
                showarrow = F,
                text = &quot;&lt;b>Percent&lt;/b>&quot;),

           list(x = 0.05, y = 0.98,
                xref = &quot;paper&quot;, yref = &quot;paper&quot;,
                showarrow = F,
                text = &quot;&lt;b>Zoom&lt;/b>&quot;)
         ),

         width = 1024,
         height = 600)</pre>
<p><br>
<iframe height="600" width="1024" src="https://plot.ly/~riddhiman/357.embed"></iframe></p>
<h2>Some other examples:</h2>
<p><iframe height="600" width="1024" src="https://plot.ly/~riddhiman/361.embed"></iframe><br>
<iframe height="600" width="1024" src="https://plot.ly/~riddhiman/363.embed"></iframe><br>
<iframe height="600" width="1024" src="https://plot.ly/~riddhiman/365.embed"></iframe></p>

<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/macroeconomic-charts-by-the-fed-using-r-and-plotly/"></div></div><div><a data-url="http://www.r-bloggers.com/macroeconomic-charts-by-the-fed-using-r-and-plotly/" data-counturl="http://www.r-bloggers.com/macroeconomic-charts-by-the-fed-using-r-and-plotly/" data-count="vertical" data-text="Macroeconomic charts by the Fed using R and Plotly" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>

<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://moderndata.plot.ly/macroeconomic-charts-by-the-fed-using-r-and-plotly/"> R – Modern Data</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/SioNhkO671Q"></a></div></code></code></code>