我不知道為什么我在這個 ggplot 的灰色條中看到這些斷斷續續的線條,以及為什么每個圖表的圖例順序不同。你能幫我解決這個問題嗎?這是我的代碼和資料的子樣本。用于 ggplot 的變數中沒有使用缺失值。
Forecast_plot_vanilla <- ggplot(Forecast_df, aes(date))
geom_tile(aes(alpha = Recession, y = 1),
fill = "grey", height = Inf)
scale_alpha_continuous(range = c(0, 1), breaks = c(0, 1), labels = c("0" = "Expansion", "1"
= "Recession"))
geom_line(aes(y = ERVOLY, col="Actual Returns"), size = .8)
ylab('Compounded Returns Next 12 Months (%)')
labs(x = NULL, alpha = "Economic Cylcle", color = "Model Forecast",
title = "Rising Inflation Expectations in 2021",
subtitle = '"Actual Returns vs. AR(2) Model"')
theme(plot.background = element_blank(),
panel.grid.minor = element_blank(),
panel.grid.major= element_blank())
scale_color_manual(values=c(
"Actual Returns" = "black"))
Forecast_plot_vanilla
這是資料的一個子集
dput(head(Forecast_df, 10))
structure(list(INFEX = c(2.9961298, 3.0273917, 2.9937871, 3.1838587,
3.3951941, 3.3770226, 3.4259232, 3.5322081, 3.3882517, 3.4201416
), ERVOLY = c(-8.77748883028093, -14.8416404279317, -9.02974052805671,
-16.8303572233488, -18.7945822035285, -14.8777818020646, -14.519413479622,
-15.6444594874336, -16.4584536211307, -19.3396158494671), date = structure(c(10926,
10957, 10988, 11017, 11048, 11078, 11109, 11139, 11170, 11201
), class = "Date"), Expansion = c(1, 1, 1, 1, 1, 1, 1, 1, 1,
1), Recession = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), ERVOLY_lag1 = c(-1.73631437680128,
-8.77748883028093, -14.8416404279317, -9.02974052805671, -16.8303572233488,
-18.7945822035285, -14.8777818020646, -14.519413479622, -15.6444594874336,
-16.4584536211307), ERVOLY_lag2 = c(0.826035972924055, -1.73631437680128,
-8.77748883028093, -14.8416404279317, -9.02974052805671, -16.8303572233488,
-18.7945822035285, -14.8777818020646, -14.519413479622, -15.6444594874336
), ERVOL_vanilla = c(-0.86668297866284, -7.49039800107863, -12.8520494772519,
-6.48762453981438, -14.3713769094631, -15.6549081560956, -11.6216205302654,
-11.4929227320897, -12.7635844039787, -13.4320398169549), ERVOL_twofactor =
c(-0.827189244172975,
-7.46356815233837, -12.8829528152664, -6.60151291973358, -14.4185578850936,
-15.7770171682792, -11.7743725647707, -11.6110549705504, -12.8692222977468,
-13.5488959720254), ERVOL_threefactor = c(-0.923203527477895,
-7.4933399582189, -12.8706459303638, -6.71159009749288, -14.4375623857157,
-15.8077186223044, -11.8668964436481, -11.7032745502137, -12.9273687852697,
-13.6070975934917), ERVOL_AR2 = c(-1.83716914438012, -8.676457359149,
-14.2220911732806, -7.99373494394322, -16.1873977312507, -17.5901523147866,
-13.5440099520562, -13.4658503981688, -14.6133744990458, -15.3449421615658
)), row.names = 140:149, class = "data.frame")


uj5u.com熱心網友回復:
問題在于geom_tile為每個圖塊選擇相同的寬度。但是,由于月份的天數不同,因此寬度基于 2 月的天數,即寬度為 28 天。對于所有其他月份,您最終會有一些長達 3 天的空白空間,這是您的灰線。
擺脫灰線的一種選擇是切換到geom_rect我lubridate::ceiling_date用來獲取每個“瓷磚”的結束日期的位置:
library(ggplot2)
Forecast_df$Recession[3:5] <- 1
ggplot(Forecast_df, aes(date))
geom_rect(aes(xmin = date, xmax = lubridate::ceiling_date(date, "month"),
ymin = -Inf, ymax = Inf, alpha = Recession),
fill = "grey")
geom_line(aes(y = ERVOLY, col="Actual Returns"), size = .8)
scale_alpha_continuous(range = c(0, 1), breaks = c(0, 1), labels = c("0" = "Expansion", "1" = "Recession"))
ylab('Compounded Returns Next 12 Months (%)')
labs(x = NULL, alpha = "Economic Cylcle", color = "Model Forecast",
title = "Rising Inflation Expectations in 2021",
subtitle = '"Actual Returns vs. AR(2) Model"')
theme(plot.background = element_blank(),
panel.grid.minor = element_blank(),
panel.grid.major= element_blank())
scale_color_manual(values=c("Actual Returns" = "black"))

轉載請註明出處,本文鏈接:https://www.uj5u.com/net/376433.html
上一篇:在ggplot2/plotly中,當我使用`geom_bar(stat='identity',position='fill')`時,如何將數字提示更改為百分比格
