我正在使用一种解决方法,而不是创建一个数组以在时间序列的geom_rect
上使用facets
。您可以在下图中看到不同的阴影。
alphas
不合适,并给我带来各种深浅不一的红色。我相信这是因为aes
正在多次工作,而不是一次,但是我不确定如何解决该问题。
任何帮助将不胜感激。这是我的代码段。
longterm <- gather(US, value = "Rate", key = "Country",
US,GB,KR,AUS,DE,CN,CAN,
NZ,SE,IE,CH,IT,ES,DK,NL,LU,MX,
data=interest)
ggplot(data = longterm, mapping = aes(x = date, y = Rate)) +
geom_line() +
facet_wrap(~ Country) +
labs(x = "Date", y = "Yield") +
scale_y_continuous(labels = scales::percent) +
geom_rect(data = data.frame(Country = "US"),
aes(xmin = as.Date("2008-01-01"),
xmax = as.Date("2009-04-01"),
ymin = -Inf, ymax = Inf),
alpha = 0.3, fill="red", inherit.aes = FALSE) +
geom_rect(data = data.frame(Country = "CAN"),
aes(xmin = as.Date("2008-07-01"),
xmax = as.Date("2009-06-30"),
ymin = -Inf, ymax = Inf),
alpha = 0.3, fill="red", inherit.aes = FALSE) +
geom_rect(data = data.frame(Country = "GB"),
aes(xmin = as.Date("2008-04-01"),
xmax = as.Date("2009-06-30"),
ymin = -Inf, ymax = Inf),
alpha = 0.3, fill="red", inherit.aes = FALSE)
这里是dput(head(interest))
structure(list(date = structure(c(10592, 10623, 10651, 10682,
10712, 10743), class = "Date"), US = c(0.0472, 0.05, 0.0523,
0.0518, 0.0554, 0.059), GB = c(0.042868, 0.044319, 0.046554,
0.04592, 0.049068, 0.051716), KR = c(0.0753, 0.0856, 0.0823,
0.0728, 0.0815, 0.0819), AUS = c(0.05127894737, 0.053, 0.05526521739,
0.053575, 0.05875714286, 0.06206666667), DE = c(0.037, 0.0385,
0.0404, 0.0385, 0.0401, 0.0436), CN = c(0.045161, 0.062767, 0.043972,
0.043383, 0.042458, 0.034632), CAN = c(0.049465, 0.05124,
0.05220434783,
0.0502952381, 0.053595, 0.05580909091), NZ = c(0.0553, 0.0562,
0.0584, 0.057, 0.0612, 0.0651), SE = c(0.0402, 0.0418, 0.0444,
0.0424, 0.045, 0.0487), MX = c(0.3127, 0.2829, 0.251, 0.2326,
0.2274, 0.2355), IE = c(0.0384, 0.0418, 0.041, 0.0389, 0.0434,
0.0478), CH = c(0.02555, 0.0255, 0.02604, 0.02534, 0.0272, 0.02937
), IT = c(0.042867, 0.0428, 0.0427, 0.0411, 0.04279, 0.04615),
ES = c(0.03880105263, 0.0402225, 0.04254391304, 0.0408635,
0.04274238095, 0.04600090909), DK = c(0.041045, 0.042185,
0.044317, 0.042172, 0.044605, 0.048118), NL = c(0.03801,
0.03932, 0.04139, 0.03995, 0.04193, 0.04513), LU = c(0.0391,
0.0393, 0.0417, 0.0401, 0.0416, 0.0437)), row.names = c(NA,
-6L), class = c("tbl_df", "tbl", "data.frame"))