Adding Recession Bands to ggplot of Federal Interest Rate, the Taylor Rule and Years

45 Views Asked by At

I'm trying to add recession bands to this graph: The Taylor (1993) rule for the US and the Fed Funds rate, 1960-2023

I'm not sure where to start. Here's what I've got currently:

p <- ggplot(df, aes(date, fedfunds, group = 1, color="red"))+
  geom_line( size=1.2, alpha=1, linetype=2)+
  geom_line( data = df2, aes(x = date, y = taylor_rule, color="blue"), size=1.2, alpha=1, linetype=1) +
  ggtitle("The Taylor (1993) rule for the US and the Fed Funds rate, 1960-2023")+
  xlab("Year") + ylab("Interest Rate and Taylor Rate")+
  ylim(-5,20)+
  scale_color_hue(labels = c("Taylor Rule", "Federal Funds Rate"))+
  theme(legend.position="bottom")

#This renders the graph in the first image. The data used is quarterly:

> df
              FEDFUNDS               PCEPI             UNRATE       date inflationgap unemploymentgap taylor_rule
1   3.9333333333333331  1.6955100000000001 5.1333333333333329 1960-01-01     -0.30449       1.1333333  1.55442167
2   3.6966666666666668             1.81114 5.2333333333333334 1960-04-01     -0.18886       1.2333333  1.54223667
3   2.9366666666666665  1.5806800000000001 5.5333333333333332 1960-07-01     -0.41932       1.5333333  1.21700667
4   2.2966666666666669             1.47695 6.2666666666666666 1960-10-01     -0.52305       2.2666667  0.65180833
5   2.0033333333333334  1.5333399999999999 6.7999999999999998 1961-01-01     -0.46666       2.8000000  0.30667000
6   1.7333333333333334 0.99473999999999996                  7 1961-04-01     -1.00526       3.0000000 -0.10263000
7   1.6833333333333333 0.97816999999999998 6.7666666666666666 1961-07-01     -1.02183       2.7666667  0.05241833
8   2.3999999999999999 0.64354999999999996 6.2000000000000002 1961-10-01     -1.35645       2.2000000  0.28177500
9   2.4566666666666666 0.89122000000000001 5.6333333333333329 1962-01-01     -1.10878       1.6333333  0.80227667
10  2.6066666666666669             1.26149 5.5333333333333332 1962-04-01     -0.73851       1.5333333  1.05741167
11  2.8466666666666667             1.16794 5.5666666666666664 1962-07-01     -0.83206       1.5666667  0.98730333
12  2.9233333333333333  1.3677999999999999 5.5333333333333332 1962-10-01     -0.63220       1.5333333  1.11056667
13  2.9666666666666668  1.2206699999999999 5.7666666666666666 1963-01-01     -0.77933       1.7666667  0.87366833
14  2.9633333333333334  1.0209900000000001 5.7333333333333334 1963-04-01     -0.97901       1.7333333  0.79716167
15  3.3300000000000001  1.2403599999999999                5.5 1963-07-01     -0.75964       1.5000000  1.07018000
16  3.4533333333333331             1.30339 5.5666666666666664 1963-10-01     -0.69661       1.5666667  1.05502833
17  3.4633333333333334             1.49129 5.4666666666666668 1964-01-01     -0.50871       1.4666667  1.21897833
18  3.4900000000000002  1.5534300000000001 5.2000000000000002 1964-04-01     -0.44657       1.2000000  1.43671500
19  3.4566666666666666  1.3948700000000001                  5 1964-07-01     -0.60513       1.0000000  1.49743500
20  3.5766666666666667             1.35467 4.9666666666666668 1964-10-01     -0.64533       0.9666667  1.50066833

#Bit messy but it results in a plot.

#Here is an example of the data I am using for the recession bands. I edited it such that the dates with a "1" are in its own column and the ones with a "0" are also in its own column.

> print(REC2, n=183)
# A tibble: 183 × 2
    Peak       Trough    
    <date>     <date>    
  1 1969-04-01 1967-10-01
  2 1969-07-01 1968-01-01
  3 1969-10-01 1968-04-01
  4 1970-01-01 1968-07-01
  5 1970-04-01 1968-10-01
  6 1970-07-01 1969-01-01
  7 1970-10-01 1971-01-01
  8 1973-10-01 1971-04-01
  9 1974-01-01 1971-07-01
 10 1974-04-01 1971-10-01
 11 1974-07-01 1972-01-01
 12 1974-10-01 1972-04-01
 13 1975-01-01 1972-07-01
 14 1979-04-01 1972-10-01
 15 1979-07-01 1973-01-01
 16 1979-10-01 1973-04-01
 17 1980-01-01 1973-07-01
 18 1980-04-01 1975-04-01
 19 1981-04-01 1975-07-01
 20 1981-07-01 1975-10-01
 21 1981-10-01 1976-01-01
 22 1982-01-01 1976-04-01
 23 1982-04-01 1976-07-01
 24 1989-10-01 1976-10-01
 25 1990-01-01 1977-01-01
 26 1990-04-01 1977-04-01
 27 1990-07-01 1977-07-01
 28 1990-10-01 1977-10-01
 29 1991-01-01 1978-01-01
 30 2001-01-01 1978-04-01
 31 2001-04-01 1978-07-01
 32 2001-07-01 1978-10-01
 33 2007-10-01 1979-01-01
 34 2008-01-01 1980-07-01
 35 2008-04-01 1980-10-01
 36 2008-07-01 1981-01-01
 37 2008-10-01 1982-07-01
 38 2009-01-01 1982-10-01
 39 2009-04-01 1983-01-01
 40 2020-01-01 1983-04-01
 41 2020-04-01 1983-07-01
 42 NA         1983-10-01
 43 NA         1984-01-01
 44 NA         1984-04-01
 45 NA         1984-07-01
 46 NA         1984-10-01
 47 NA         1985-01-01
 48 NA         1985-04-01
 49 NA         1985-07-01
 50 NA         1985-10-01
 51 NA         1986-01-01
 52 NA         1986-04-01
 53 NA         1986-07-01
 54 NA         1986-10-01
 55 NA         1987-01-01
 56 NA         1987-04-01
 57 NA         1987-07-01
 58 NA         1987-10-01
 59 NA         1988-01-01
 60 NA         1988-04-01
 61 NA         1988-07-01
 62 NA         1988-10-01
 63 NA         1989-01-01
 64 NA         1989-04-01
 65 NA         1989-07-01
 66 NA         1991-04-01
 67 NA         1991-07-01
 68 NA         1991-10-01
 69 NA         1992-01-01
 70 NA         1992-04-01
 71 NA         1992-07-01
 72 NA         1992-10-01
 73 NA         1993-01-01
 74 NA         1993-04-01
 75 NA         1993-07-01
 76 NA         1993-10-01
 77 NA         1994-01-01
 78 NA         1994-04-01
 79 NA         1994-07-01
 80 NA         1994-10-01
 81 NA         1995-01-01
 82 NA         1995-04-01
 83 NA         1995-07-01
 84 NA         1995-10-01
 85 NA         1996-01-01
 86 NA         1996-04-01
 87 NA         1996-07-01
 88 NA         1996-10-01
 89 NA         1997-01-01
 90 NA         1997-04-01
 91 NA         1997-07-01
 92 NA         1997-10-01
 93 NA         1998-01-01
 94 NA         1998-04-01
 95 NA         1998-07-01
 96 NA         1998-10-01
 97 NA         1999-01-01
 98 NA         1999-04-01
 99 NA         1999-07-01
100 NA         1999-10-01
101 NA         2000-01-01
102 NA         2000-04-01
103 NA         2000-07-01
104 NA         2000-10-01
105 NA         2001-10-01
106 NA         2002-01-01
107 NA         2002-04-01
108 NA         2002-07-01
109 NA         2002-10-01
110 NA         2003-01-01
111 NA         2003-04-01
112 NA         2003-07-01
113 NA         2003-10-01
114 NA         2004-01-01
115 NA         2004-04-01
116 NA         2004-07-01
117 NA         2004-10-01
118 NA         2005-01-01
119 NA         2005-04-01
120 NA         2005-07-01
121 NA         2005-10-01
122 NA         2006-01-01
123 NA         2006-04-01
124 NA         2006-07-01
125 NA         2006-10-01
126 NA         2007-01-01
127 NA         2007-04-01
128 NA         2007-07-01
129 NA         2009-07-01
130 NA         2009-10-01
131 NA         2010-01-01
132 NA         2010-04-01
133 NA         2010-07-01
134 NA         2010-10-01
135 NA         2011-01-01
136 NA         2011-04-01
137 NA         2011-07-01
138 NA         2011-10-01
139 NA         2012-01-01
140 NA         2012-04-01
141 NA         2012-07-01
142 NA         2012-10-01
143 NA         2013-01-01
144 NA         2013-04-01
145 NA         2013-07-01
146 NA         2013-10-01
147 NA         2014-01-01
148 NA         2014-04-01
149 NA         2014-07-01
150 NA         2014-10-01
151 NA         2015-01-01
152 NA         2015-04-01
153 NA         2015-07-01
154 NA         2015-10-01
155 NA         2016-01-01
156 NA         2016-04-01
157 NA         2016-07-01
158 NA         2016-10-01
159 NA         2017-01-01
160 NA         2017-04-01
161 NA         2017-07-01
162 NA         2017-10-01
163 NA         2018-01-01
164 NA         2018-04-01
165 NA         2018-07-01
166 NA         2018-10-01
167 NA         2019-01-01
168 NA         2019-04-01
169 NA         2019-07-01
170 NA         2019-10-01
171 NA         2020-07-01
172 NA         2020-10-01
173 NA         2021-01-01
174 NA         2021-04-01
175 NA         2021-07-01
176 NA         2021-10-01
177 NA         2022-01-01
178 NA         2022-04-01
179 NA         2022-07-01
180 NA         2022-10-01
181 NA         2023-01-01
182 NA         2023-04-01
183 NA         2023-07-01

#The peaks represent periods of recession, and the troughs are periods not in recession.

This might be helpful: https://www.r-bloggers.com/2011/08/use-geom_rect-to-add-recession-bars-to-your-time-series-plots-rstats-ggplot/. This is where I started but I haven't gotten any results out of it. I copied this line:

pp = p + geom_rect(data=REC2, aes(xmin=Peak, xmax=Trough, ymin=-Inf, ymax=+Inf), fill='pink', alpha=0.2)

But it gives this error:

Error in `geom_rect()`:
! Problem while computing aesthetics.
ℹ Error occurred in the 3rd layer.
Caused by error in `check_aesthetics()`:
! Aesthetics must be either length 1 or the same as the data (183)
✖ Fix the following mappings: `x` and `y`
Run `rlang::last_trace()` to see where the error occurred.
2

There are 2 best solutions below

3
cristian-vargas On

Based on your last comment, I suspect the issue may lie with the fact that Peak and Trough are both objects of the class date-time (dttm), whereas internally ggplot is expecting them to be just Date objects. Since the date column of df appears to lack any hh:mm:ss, I've decided to drop hh:mm:ss from the REC2 data.frame as well. Keep in mind as well that the recession bands start in the year 1969, whereas the sample data you've provided us only goes as far as 1964, so the final plot will seem strange, but should look correct on your real data. I'm also running R version 4.3.3 with ggplot2 version 3.5.0.

library(ggplot2)
library(lubridate)

# Read fed funds rate table
df <- read.table(
  textConnection(
    "FEDFUNDS               PCEPI             UNRATE       date inflationgap unemploymentgap taylor_rule
    3.9333333333333331  1.6955100000000001 5.1333333333333329 1960-01-01     -0.30449       1.1333333  1.55442167
    3.6966666666666668             1.81114 5.2333333333333334 1960-04-01     -0.18886       1.2333333  1.54223667
    2.9366666666666665  1.5806800000000001 5.5333333333333332 1960-07-01     -0.41932       1.5333333  1.21700667
    2.2966666666666669             1.47695 6.2666666666666666 1960-10-01     -0.52305       2.2666667  0.65180833
    2.0033333333333334  1.5333399999999999 6.7999999999999998 1961-01-01     -0.46666       2.8000000  0.30667000
    1.7333333333333334 0.99473999999999996                  7 1961-04-01     -1.00526       3.0000000 -0.10263000
    1.6833333333333333 0.97816999999999998 6.7666666666666666 1961-07-01     -1.02183       2.7666667  0.05241833
    2.3999999999999999 0.64354999999999996 6.2000000000000002 1961-10-01     -1.35645       2.2000000  0.28177500
    2.4566666666666666 0.89122000000000001 5.6333333333333329 1962-01-01     -1.10878       1.6333333  0.80227667
    2.6066666666666669             1.26149 5.5333333333333332 1962-04-01     -0.73851       1.5333333  1.05741167
    2.8466666666666667             1.16794 5.5666666666666664 1962-07-01     -0.83206       1.5666667  0.98730333
    2.9233333333333333  1.3677999999999999 5.5333333333333332 1962-10-01     -0.63220       1.5333333  1.11056667
    2.9666666666666668  1.2206699999999999 5.7666666666666666 1963-01-01     -0.77933       1.7666667  0.87366833
    2.9633333333333334  1.0209900000000001 5.7333333333333334 1963-04-01     -0.97901       1.7333333  0.79716167
    3.3300000000000001  1.2403599999999999                5.5 1963-07-01     -0.75964       1.5000000  1.07018000
    3.4533333333333331             1.30339 5.5666666666666664 1963-10-01     -0.69661       1.5666667  1.05502833
    3.4633333333333334             1.49129 5.4666666666666668 1964-01-01     -0.50871       1.4666667  1.21897833
    3.4900000000000002  1.5534300000000001 5.2000000000000002 1964-04-01     -0.44657       1.2000000  1.43671500
    3.4566666666666666  1.3948700000000001                  5 1964-07-01     -0.60513       1.0000000  1.49743500
    3.5766666666666667             1.35467 4.9666666666666668 1964-10-01     -0.64533       0.9666667  1.50066833"
  ),
  header = TRUE,
  colClasses = c(rep("numeric", 3), "Date", rep("numeric", 3))
)

# read recession timepoints, modified with commas as separators
REC2 <- read.table(
  textConnection(
    "Peak,                Trough           
   1969-04-01 00:00:00, 1967-10-01 00:00:00
   1969-07-01 00:00:00, 1968-01-01 00:00:00
   1969-10-01 00:00:00, 1968-04-01 00:00:00
   1970-01-01 00:00:00, 1968-07-01 00:00:00
   1970-04-01 00:00:00, 1968-10-01 00:00:00
   1970-07-01 00:00:00, 1969-01-01 00:00:00
   1970-10-01 00:00:00, 1971-01-01 00:00:00
   1973-10-01 00:00:00, 1971-04-01 00:00:00
   1974-01-01 00:00:00, 1971-07-01 00:00:00
   1974-04-01 00:00:00, 1971-10-01 00:00:00"
  ),
  sep = ",",
  header = TRUE,
  # Drops h:m:s for now, but alternatively can be read as character class first
  # and then manipulated using lubridate
  colClasses = "Date"
)

# Construct original line graph
p <- ggplot(df, aes(date, FEDFUNDS, group = 1, color="red"))+
  # `Size` aesthetic deprecated since version 3.4.0, use `linewidth` instead
  geom_line(linewidth=1.2, alpha=1, linetype=2)+
  # df2 not provided so we'll silence this for now
  # geom_line(data = df2, aes(x = date, y = taylor_rule, color="blue"), linewidth=1.2, alpha=1, linetype=1) +
  ggtitle("The Taylor (1993) rule for the US and the Fed Funds rate, 1960-2023")+
  xlab("Year") + ylab("Interest Rate and Taylor Rate")+
  ylim(-5,20)+
  scale_color_hue(labels = c("Taylor Rule", "Federal Funds Rate"))+
  theme(legend.position="bottom")

# Add recession bands
pp <- p + geom_rect(
  data = REC2,
  aes(
    xmin = Peak,
    xmax = Trough,
    ymin = -Inf,
    ymax = +Inf
  ),
  fill = "pink",
  alpha = 0.2,
  inherit.aes = FALSE
)

# Call output
pp

Created on 2024-03-31 with reprex v2.1.0

0
ImProograming On

I had to switch to a dataset that specifically marks when a recession starts and ends. Luckily, FRED uses data published by the NBER. I generated this dataset from it, which included the dates I needed, and had the class of data I needed to avoid the error with transform.

recessions.df = read.table(textConnection(
  "Peak, Trough
1960-04-01, 1961-02-01
1969-12-01, 1970-11-01
1973-11-01, 1975-03-01
1980-01-01, 1980-07-01
1981-07-01, 1982-11-01
1990-07-01, 1991-03-01
2001-03-01, 2001-11-01
2007-12-01, 2009-06-01
2020-02-01, 2020-04-01"), sep=',',
colClasses=c('Date', 'Date'), header=TRUE)

Here's the new command, which generates the graph I need:

#Initial plot 
p <- ggplot(df, aes(date, fedfunds, group = 1, color="red"))+
  geom_rect(data = recessions.df, aes(fill = "Recession Bands", xmin = Peak, xmax = Trough, ymin = -Inf, ymax = +Inf),  alpha = 0.5, inherit.aes = FALSE)+  geom_line( size=1.2, alpha=1, linetype=2)+
  geom_line( data = df, aes(x = date, y = taylor_rule, color="blue"), size=1.2, alpha=1, linetype=1)+
  ggtitle("The Taylor (1993) rule for the US and the Fed Funds rate, 1960-2023")+
  xlab("Year") + ylab("Interest Rate and Taylor Rate")+
  ylim(-5,20)+
  scale_color_hue('Graph Legend', labels = c("Taylor Rule", "Federal Funds Rate"))+
  theme(legend.position="bottom")+
  scale_fill_manual('Legend', values = 'pink', guide = guide_legend(override.aes = list(alpha = 1))) 

p

Graph is here: enter image description here