将衰退带添加到联邦利率、泰勒规则和年份的ggplot中

问题描述 投票:0回答:1

我正在尝试将衰退带添加到该图表中: The Taylor (1993) rule for the US and the Fed Funds rate, 1960-2023

我不知道从哪里开始。这是我目前所拥有的:

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 it's own column and the ones with a "0" are also in it's own column.

REC2
   Peak                Trough             
   <dttm>              <dttm>             
 1 1969-04-01 00:00:00 1967-10-01 00:00:00
 2 1969-07-01 00:00:00 1968-01-01 00:00:00
 3 1969-10-01 00:00:00 1968-04-01 00:00:00
 4 1970-01-01 00:00:00 1968-07-01 00:00:00
 5 1970-04-01 00:00:00 1968-10-01 00:00:00
 6 1970-07-01 00:00:00 1969-01-01 00:00:00
 7 1970-10-01 00:00:00 1971-01-01 00:00:00
 8 1973-10-01 00:00:00 1971-04-01 00:00:00
 9 1974-01-01 00:00:00 1971-07-01 00:00:00
10 1974-04-01 00:00:00 1971-10-01 00:00:00

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

这可能会有所帮助:https://www.r-bloggers.com/2011/08/use-geom_rect-to-add-recession-bars-to-your-time-series-plots-rstats-ggplot/ 。 这是我开始的地方,但我还没有得到任何结果。我复制了这一行:

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

但它给出了这个错误:

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.
r ggplot2
1个回答
0
投票

根据您的最后评论,我怀疑问题可能在于以下事实:

Peak
Trough
都是日期时间类(
dttm
)的对象,而在内部
ggplot
期望它们只是
Date
物体。由于
date
df
列似乎缺少任何
hh:mm:ss
,我决定也从
hh:mm:ss
data.frame 中删除
REC2
。另请记住,衰退带从 1969 年开始,而您向我们提供的样本数据仅持续到 1964 年,因此最终的图看起来很奇怪,但在您的实际数据上应该看起来是正确的。我还运行 R 版本 4.3.3 和
ggplot2
版本 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

创建于 2024-03-31,使用 reprex v2.1.0

© www.soinside.com 2019 - 2024. All rights reserved.