我使用ggplot2
绘制了一条线图,希望对其中的某些部分进行着色以显示经济衰退期。
这是我的数据集的可重现性示例:
structure(list(Date = structure(c(10957, 10988, 11017, 11048,
11078, 11109, 11139, 11170, 11201, 11231, 11262, 11292, 11323,
11354, 11382, 11413, 11443, 11474, 11504, 11535, 11566, 11596,
11627, 11657, 11688, 11719, 11747, 11778, 11808, 11839, 11869,
11900, 11931, 11961, 11992, 12022, 12053, 12084, 12112, 12143,
12173, 12204, 12234, 12265, 12296, 12326, 12357, 12387, 12418,
12449, 12478, 12509, 12539, 12570, 12600, 12631, 12662, 12692,
12723, 12753, 12784, 12815, 12843, 12874, 12904, 12935, 12965,
12996, 13027, 13057, 13088, 13118, 13149, 13180, 13208, 13239,
13269, 13300, 13330, 13361, 13392, 13422, 13453, 13483, 13514,
13545, 13573, 13604, 13634, 13665, 13695, 13726, 13757, 13787,
13818, 13848, 13879, 13910, 13939, 13970, 14000, 14031, 14061,
14092, 14123, 14153, 14184, 14214, 14245, 14276, 14304, 14335,
14365, 14396, 14426, 14457, 14488, 14518, 14549, 14579, 14610,
14641, 14669, 14700, 14730, 14761, 14791, 14822, 14853, 14883,
14914, 14944, 14975, 15006, 15034, 15065, 15095, 15126, 15156,
15187, 15218, 15248, 15279, 15309, 15340, 15371, 15400, 15431,
15461, 15492, 15522, 15553, 15584, 15614, 15645, 15675, 15706,
15737, 15765, 15796, 15826, 15857, 15887, 15918, 15949, 15979,
16010, 16040, 16071, 16102, 16130, 16161, 16191, 16222, 16252,
16283, 16314, 16344, 16375, 16405, 16436, 16467, 16495, 16526,
16556, 16587, 16617, 16648, 16679, 16709, 16740, 16770, 16801,
16832, 16861, 16892, 16922, 16953, 16983, 17014, 17045, 17075,
17106, 17136, 17167, 17198, 17226, 17257, 17287, 17318, 17348,
17379, 17410, 17440, 17471, 17501, 17532, 17563, 17591, 17622,
17652, 17683, 17713, 17744, 17775, 17805, 17836, 17866, 17897,
17928, 17956, 17987, 18017, 18048, 18078, 18109, 18140, 18170,
18201, 18231, 18262, 18293, 18322, 18353, 18383, 10957, 10988,
11017, 11048, 11078, 11109, 11139, 11170, 11201, 11231, 11262,
11292, 11323, 11354, 11382, 11413, 11443, 11474, 11504, 11535,
11566, 11596, 11627, 11657, 11688, 11719, 11747, 11778, 11808,
11839, 11869, 11900, 11931, 11961, 11992, 12022, 12053, 12084,
12112, 12143, 12173, 12204, 12234, 12265, 12296, 12326, 12357,
12387, 12418, 12449, 12478, 12509, 12539, 12570, 12600, 12631,
12662, 12692, 12723, 12753, 12784, 12815, 12843, 12874, 12904,
12935, 12965, 12996, 13027, 13057, 13088, 13118, 13149, 13180,
13208, 13239, 13269, 13300, 13330, 13361, 13392, 13422, 13453,
13483, 13514, 13545, 13573, 13604, 13634, 13665, 13695, 13726,
13757, 13787, 13818, 13848, 13879, 13910, 13939, 13970, 14000,
14031, 14061, 14092, 14123, 14153, 14184, 14214, 14245, 14276,
14304, 14335, 14365, 14396, 14426, 14457, 14488, 14518, 14549,
14579, 14610, 14641, 14669, 14700, 14730, 14761, 14791, 14822,
14853, 14883, 14914, 14944, 14975, 15006, 15034, 15065, 15095,
15126, 15156, 15187, 15218, 15248, 15279, 15309, 15340, 15371,
15400, 15431, 15461, 15492, 15522, 15553, 15584, 15614, 15645,
15675, 15706, 15737, 15765, 15796, 15826, 15857, 15887, 15918,
15949, 15979, 16010, 16040, 16071, 16102, 16130, 16161, 16191,
16222, 16252, 16283, 16314, 16344, 16375, 16405, 16436, 16467,
16495, 16526, 16556, 16587, 16617, 16648, 16679, 16709, 16740,
16770, 16801, 16832, 16861, 16892, 16922, 16953, 16983, 17014,
17045, 17075, 17106, 17136, 17167, 17198, 17226, 17257, 17287,
17318, 17348, 17379, 17410, 17440, 17471, 17501, 17532, 17563,
17591, 17622, 17652, 17683, 17713, 17744, 17775, 17805, 17836,
17866, 17897, 17928, 17956, 17987, 18017, 18048, 18078, 18109,
18140, 18170, 18201, 18231, 18262, 18293, 18322, 18353, 18383
), class = "Date"), Key = c("Nominal", "Nominal", "Nominal",
"Nominal", "Nominal", "Nominal", "Nominal", "Nominal", "Nominal",
"Nominal", "Nominal", "Nominal", "Nominal", "Nominal", "Nominal",
"Nominal", "Nominal", "Nominal", "Nominal", "Nominal", "Nominal",
"Nominal", "Nominal", "Nominal", "Nominal", "Nominal", "Nominal",
"Nominal", "Nominal", "Nominal", "Nominal", "Nominal", "Nominal",
"Nominal", "Nominal", "Nominal", "Nominal", "Nominal", "Nominal",
"Nominal", "Nominal", "Nominal", "Nominal", "Nominal", "Nominal",
"Nominal", "Nominal", "Nominal", "Nominal", "Nominal", "Nominal",
"Nominal", "Nominal", "Nominal", "Nominal", "Nominal", "Nominal",
"Nominal", "Nominal", "Nominal", "Nominal", "Nominal", "Nominal",
"Nominal", "Nominal", "Nominal", "Nominal", "Nominal", "Nominal",
"Nominal", "Nominal", "Nominal", "Nominal", "Nominal", "Nominal",
"Nominal", "Nominal", "Nominal", "Nominal", "Nominal", "Nominal",
"Nominal", "Nominal", "Nominal", "Nominal", "Nominal", "Nominal",
"Nominal", "Nominal", "Nominal", "Nominal", "Nominal", "Nominal",
"Nominal", "Nominal", "Nominal", "Nominal", "Nominal", "Nominal",
"Nominal", "Nominal", "Nominal", "Nominal", "Nominal", "Nominal",
"Nominal", "Nominal", "Nominal", "Nominal", "Nominal", "Nominal",
"Nominal", "Nominal", "Nominal", "Nominal", "Nominal", "Nominal",
"Nominal", "Nominal", "Nominal", "Nominal", "Nominal", "Nominal",
"Nominal", "Nominal", "Nominal", "Nominal", "Nominal", "Nominal",
"Nominal", "Nominal", "Nominal", "Nominal", "Nominal", "Nominal",
"Nominal", "Nominal", "Nominal", "Nominal", "Nominal", "Nominal",
"Nominal", "Nominal", "Nominal", "Nominal", "Nominal", "Nominal",
"Nominal", "Nominal", "Nominal", "Nominal", "Nominal", "Nominal",
"Nominal", "Nominal", "Nominal", "Nominal", "Nominal", "Nominal",
"Nominal", "Nominal", "Nominal", "Nominal", "Nominal", "Nominal",
"Nominal", "Nominal", "Nominal", "Nominal", "Nominal", "Nominal",
"Nominal", "Nominal", "Nominal", "Nominal", "Nominal", "Nominal",
"Nominal", "Nominal", "Nominal", "Nominal", "Nominal", "Nominal",
"Nominal", "Nominal", "Nominal", "Nominal", "Nominal", "Nominal",
"Nominal", "Nominal", "Nominal", "Nominal", "Nominal", "Nominal",
"Nominal", "Nominal", "Nominal", "Nominal", "Nominal", "Nominal",
"Nominal", "Nominal", "Nominal", "Nominal", "Nominal", "Nominal",
"Nominal", "Nominal", "Nominal", "Nominal", "Nominal", "Nominal",
"Nominal", "Nominal", "Nominal", "Nominal", "Nominal", "Nominal",
"Nominal", "Nominal", "Nominal", "Nominal", "Nominal", "Nominal",
"Nominal", "Nominal", "Nominal", "Nominal", "Nominal", "Nominal",
"Nominal", "Nominal", "Nominal", "Nominal", "Nominal", "Nominal",
"Nominal", "Nominal", "Nominal", "Nominal", "Nominal", "Nominal",
"Nominal", "Nominal", "Real", "Real", "Real", "Real", "Real",
"Real", "Real", "Real", "Real", "Real", "Real", "Real", "Real",
"Real", "Real", "Real", "Real", "Real", "Real", "Real", "Real",
"Real", "Real", "Real", "Real", "Real", "Real", "Real", "Real",
"Real", "Real", "Real", "Real", "Real", "Real", "Real", "Real",
"Real", "Real", "Real", "Real", "Real", "Real", "Real", "Real",
"Real", "Real", "Real", "Real", "Real", "Real", "Real", "Real",
"Real", "Real", "Real", "Real", "Real", "Real", "Real", "Real",
"Real", "Real", "Real", "Real", "Real", "Real", "Real", "Real",
"Real", "Real", "Real", "Real", "Real", "Real", "Real", "Real",
"Real", "Real", "Real", "Real", "Real", "Real", "Real", "Real",
"Real", "Real", "Real", "Real", "Real", "Real", "Real", "Real",
"Real", "Real", "Real", "Real", "Real", "Real", "Real", "Real",
"Real", "Real", "Real", "Real", "Real", "Real", "Real", "Real",
"Real", "Real", "Real", "Real", "Real", "Real", "Real", "Real",
"Real", "Real", "Real", "Real", "Real", "Real", "Real", "Real",
"Real", "Real", "Real", "Real", "Real", "Real", "Real", "Real",
"Real", "Real", "Real", "Real", "Real", "Real", "Real", "Real",
"Real", "Real", "Real", "Real", "Real", "Real", "Real", "Real",
"Real", "Real", "Real", "Real", "Real", "Real", "Real", "Real",
"Real", "Real", "Real", "Real", "Real", "Real", "Real", "Real",
"Real", "Real", "Real", "Real", "Real", "Real", "Real", "Real",
"Real", "Real", "Real", "Real", "Real", "Real", "Real", "Real",
"Real", "Real", "Real", "Real", "Real", "Real", "Real", "Real",
"Real", "Real", "Real", "Real", "Real", "Real", "Real", "Real",
"Real", "Real", "Real", "Real", "Real", "Real", "Real", "Real",
"Real", "Real", "Real", "Real", "Real", "Real", "Real", "Real",
"Real", "Real", "Real", "Real", "Real", "Real", "Real", "Real",
"Real", "Real", "Real", "Real", "Real", "Real", "Real", "Real",
"Real", "Real", "Real", "Real", "Real", "Real", "Real", "Real",
"Real", "Real", "Real", "Real", "Real", "Real", "Real", "Real"
), Value = c(27.18, 29.35, 29.89, 25.74, 28.78, 31.83, 29.77,
31.22, 33.88, 33.08, 34.4, 28.46, 29.58, 29.61, 27.24, 27.41,
28.64, 27.6, 26.45, 27.47, 25.88, 22.21, 19.67, 19.33, 19.67,
20.74, 24.42, 26.27, 27.02, 25.52, 26.94, 28.38, 29.67, 28.85,
26.27, 29.42, 32.94, 35.87, 33.55, 28.25, 28.14, 30.72, 30.76,
31.59, 28.29, 30.33, 31.09, 32.15, 34.27, 34.74, 36.76, 36.69,
40.28, 38.02, 40.69, 44.94, 45.95, 53.13, 48.46, 43.33, 46.84,
47.97, 54.31, 53.04, 49.83, 56.26, 58.7, 64.97, 65.57, 62.37,
58.3, 59.43, 65.51, 61.63, 62.9, 69.69, 70.94, 70.96, 74.41,
73.05, 63.87, 58.88, 59.37, 62.03, 54.57, 59.26, 60.56, 63.97,
63.46, 67.48, 74.18, 72.39, 79.93, 86.2, 94.62, 91.73, 92.95,
95.35, 105.56, 112.57, 125.39, 133.93, 133.44, 116.61, 103.9,
76.65, 57.44, 41.02, 41.74, 39.16, 47.98, 49.79, 59.16, 69.68,
64.09, 71.06, 69.46, 75.82, 78.08, 74.3, 78.22, 76.42, 81.24,
84.48, 73.84, 75.35, 76.37, 76.82, 75.31, 81.9, 84.14, 89.04,
89.42, 89.58, 102.94, 110.04, 101.33, 96.29, 97.19, 86.33, 85.61,
86.41, 97.21, 98.57, 100.24, 102.25, 106.19, 103.33, 94.7, 82.41,
87.93, 94.16, 94.72, 89.57, 86.66, 88.25, 94.69, 95.32, 93.05,
92.07, 94.8, 95.8, 104.67, 106.57, 106.29, 100.54, 93.86, 97.63,
94.62, 100.82, 100.8, 102.07, 102.18, 105.79, 103.59, 96.54,
93.21, 84.4, 75.79, 59.29, 47.22, 50.58, 47.82, 54.45, 59.27,
59.82, 50.9, 42.87, 45.48, 46.22, 42.44, 37.19, 31.68, 30.32,
37.55, 40.75, 46.71, 48.76, 44.65, 44.72, 45.18, 49.78, 45.66,
51.97, 52.5, 53.47, 49.33, 51.06, 48.48, 45.18, 46.63, 48.04,
49.82, 51.58, 56.64, 57.88, 63.7, 62.23, 62.73, 66.25, 69.98,
67.87, 70.98, 68.06, 70.23, 70.75, 56.96, 49.52, 51.38, 54.95,
58.15, 63.86, 60.83, 54.66, 57.35, 54.81, 56.95, 53.96, 57.03,
59.88, 57.52, 50.54, 29.21, 20.28, 12.18, 35.1110623098635, 37.6909750982726,
38.0705649192927, 32.7655999388986, 36.5926379884475, 40.2593394584531,
37.5666122789949, 39.3963599378643, 42.5315031898627, 41.4556081617922,
43.0850390959156, 35.6658587752299, 36.8365406528212, 36.7270846434645,
33.710743505051, 33.7869070496425, 35.144122808094, 33.8108503428861,
32.4933385584751, 33.7463897996715, 31.6504460169112, 27.2538745659136,
24.1778664968767, 23.8540669147917, 24.2187990578432, 25.4357122796604,
29.7813957742614, 31.8593772928868, 32.768952206083, 30.9326209456994,
32.6174998819523, 34.2469059525419, 35.7442317700804, 34.698835754835,
31.5957856249399, 35.4626599405741, 39.5308145939991, 42.7179424885732,
39.7164094471784, 33.5150461795813, 33.4391328165755, 36.4652452501407,
36.4730252572143, 37.3151517943521, 33.3088040502752, 35.7493092945847,
36.744414175378, 38.0384241325701, 40.3496894592765, 40.6833967474362,
42.7733315957152, 42.5556099405451, 46.4478038768003, 43.7030582945002,
46.8462535661408, 51.7119366661105, 52.7627833487775, 60.6877487963539,
55.3244345399829, 49.649713422725, 53.5590956699918, 54.5365868077622,
61.2653304334486, 59.4330330680286, 55.8935565934879, 63.0735166344348,
65.5059357082118, 72.1337466470076, 71.9209823460697, 68.2736946500877,
64.3352229276168, 65.848737372898, 72.036349161317, 67.6334122005715,
68.6471384293011, 75.4157932726385, 76.3893899078568, 76.2603210972201,
79.732204374181, 78.1214074044764, 68.6407371544454, 63.6229647782921,
64.2478927621832, 67.0267069496851, 58.7863320969926, 63.4989348980545,
64.3063599159855, 67.4888696642964, 66.5441901238607, 70.6227106227106,
77.6544950730536, 75.919895543285, 83.5971564625424, 89.9624078198414,
98.1668670742601, 95.2323605229526, 96.0217353126517, 98.2157388275091,
107.798214324009, 114.26384727196, 126.21392449913, 133.465140914196,
132.282265611369, 116.061493382275, 103.554542047729, 77.1747108591312,
58.9624699362232, 42.5473211887112, 43.1065197836619, 40.2419035776861,
49.1859913212046, 50.9143934652874, 60.3217363197827, 70.4431814275865,
64.894891337256, 71.79141089419, 70.1310843461079, 76.4788654256419,
78.7027750590422, 75.0247389785326, 78.7140091212449, 76.8834534574415,
81.3984828461013, 84.4979980735897, 73.7985252288214, 75.3812832325415,
76.3855826588624, 76.7297657954246, 75.177762316086, 81.6545464334211,
83.8525534467844, 88.5836172041642, 88.539913262174, 88.2630273686326,
100.447201793101, 106.688280965197, 97.7835849413472, 93.0196163291015,
93.8058598017906, 83.0948675227373, 82.2769603367579, 83.2174459052895,
93.6974692719337, 95.2432485706749, 96.4325534905333, 97.9349845794303,
100.942120117226, 97.9276266221366, 89.854250143985, 78.3079924324129,
83.6896146070881, 89.123202226187, 89.2549211763708, 84.4349213860638,
82.0806390658757, 83.8123060208101, 89.663381187261, 89.5266415017784,
87.1667662769065, 86.3385047037465, 88.7405303650812, 89.4618932489889,
97.7065517624425, 99.360686658145, 98.9844524843896, 93.8714649980066,
87.8138413924472, 91.3488528762304, 88.2043671509113, 93.63770696283,
93.0200796023022, 93.8825929513092, 93.6565943206597, 96.7852901728943,
94.8095023668054, 88.5049051739707, 85.387873108179, 77.511904597907,
69.9824374460981, 55.0589917769038, 44.057631337833, 46.9885696449009,
44.1616897802905, 50.1824817518248, 54.3476765841014, 54.6605372302952,
46.5067832290509, 39.2254102335591, 41.6783890954378, 42.3756007488618,
38.9923007662483, 34.2859460809937, 29.1580073152728, 27.8833314327545,
34.384211282966, 37.1383679897343, 42.39861231003, 44.1144805148981,
40.4615425045559, 40.4877726383415, 40.8061518118907, 44.9048590701862,
41.2525003568712, 46.9380529694618, 47.1419835730391, 47.8623940506337,
44.1207116913178, 45.5330199111098, 43.1953511537917, 40.2185940227585,
41.5380202496735, 42.6663066734047, 44.0141389457986, 45.5978373294059,
50.0697918443876, 51.196048669858, 56.0386728423572, 54.4983229264277,
54.8122594918791, 57.6587835867854, 60.6528107611503, 58.7304293196979,
61.4174822639498, 58.8581722142036, 60.664327509627, 61.0056840914421,
49.2800492800493, 42.9804400808227, 44.5099744617741, 47.4022366609502,
49.881322768615, 54.4908442411728, 51.7951185037912, 46.5322571720204,
48.7408127604893, 46.5844844174432, 48.3654269151265, 45.7216065942148,
48.3488236645577, 50.8112144457267, 48.6200099403915, 42.6032669810351,
24.6766105268849, 17.1334431630972, 10.2885959614304)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -490L))
这是我的绘图代码
ggplot(wti_dollars, aes(Date,Value, col = Key)) +
geom_line(size = 1.2) +
scale_color_manual(values = c('maroon','#c7af76')) +
theme_bw() +labs(title = 'Spot Crude Oil Price (Jan 2000 - May 2020)', subtitle = 'West Texas Intermediate (WTI) - NAD per Barrel',
x = 'Date', y = 'US Dollars per Barrel')
为参考,我也添加了一张目前为止我的绘图是什么样子的图片
基于此,我该如何在 2007-2009 年之间填充区域?
TIA!