How to create a function in R to add NBER recession shades to different facets in ggplot?

huangapple go评论55阅读模式
英文:

How to create a function in R to add NBER recession shades to different facets in ggplot?

问题

让我们假设我想使用 ggplot 绘制不同股票的相同时间序列(例如股价),并且所有这些时间序列都具有自由刻度,因为这些股价的精确时间和幅度在不同股票之间允许不同。这很容易做到。

除此之外,我想添加NBER经济衰退期的阴影,因为在金融/经济实证工作中这是常见的。问题在于,并不是所有的面板都应该接收相同的阴影,因为时间段可能不同。

让我展示我的解决方案以及我遇到困难的地方。考虑主要数据集:

structure(list(date = structure(c(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, 18414, 18444, 
18475, 18506, 185

<details>
<summary>英文:</summary>

Let&#39;s say I want to plot the same time series (for example, stock prices) for different stocks using `ggplot`, all of them with free scales since the precise timing and magnitude of these stock prices are allowed to be different across stocks. That is easy to do.

On top of that, I&#39;d like to add NBER recession shades, as it&#39;s common in empirical work in Finance/Economics. The problem is that not all facets should receive the same shades since time periods might be different.

Let me show my solution and where I am stuck at. Consider the main dataset:

    structure(list(date = structure(c(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, 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, 18414, 18444, 18475, 18506, 18536, 18567, 18597, 18628, 
    18659, 18687, 18718, 18748, 18779, 18809, 18840, 18871, 18901, 
    18932, 18962, 18993, 19024, 19052, 19083, 19113, 19144, 19174, 
    19205, 19236, 19266, 19297, 19327, 19358, 19389, 19417, 19448, 
    19478, 19509, 19539, 19545), class = &quot;Date&quot;), close = c(14.7728071212769, 
    14.7541389465332, 15.1200866699219, 15.9694652557373, 15.3699064254761, 
    16.0131816864014, 16.4940738677979, 16.5627689361572, 17.3621864318848, 
    16.3254470825195, 16.1469821929932, 17.1895370483398, 17.3653869628906, 
    16.6619873046875, 15.6571092605591, 16.4107532501221, 16.4735546112061, 
    17.9369068145752, 17.8929424285889, 17.1455764770508, 17.4166088104248, 
    17.6181774139404, 16.8875007629395, 18.7586135864258, 18.4497222900391, 
    17.6634349822998, 17.0207576751709, 17.8165168762207, 18.1686210632324, 
    17.5480690002441, 18.0920276641846, 19.3424339294434, 18.2307147979736, 
    18.2094573974609, 19.612377166748, 18.5826187133789, 20.003849029541, 
    19.0942687988281, 19.4013919830322, 17.2195377349854, 16.1499938964844, 
    16.6782550811768, 17.222261428833, 18.396183013916, 19.6493511199951, 
    20.6264305114746, 21.0934143066406, 21.5259799957275, 22.2468776702881, 
    20.3076705932617, 20.1610584259033, 21.6584911346436, 22.2010383605957, 
    21.3875560760498, 21.0392036437988, 20.8505077362061, 21.4552383422852, 
    26.8081226348877, 24.4703216552734, 26.0126209259033, 23.8205490112305, 
    19.874813079834, 20.8176021575928, 20.9202938079834, 20.7735958099365, 
    20.2537212371826, 18.9358673095703, 20.0917453765869, 19.728385925293, 
    16.5056133270264, 14.945969581604, 14.4667568206787, 12.7253923416138, 
    12.0184240341187, 13.7642221450806, 15.1803569793701, 15.6524019241333, 
    17.923433303833, 17.7349262237549, 18.5869884490967, 19.5028495788574, 
    21.0269870758057, 22.3008842468262, 23.2144012451172, 21.4626636505127, 
    21.8358612060547, 22.4123878479004, 23.36887550354, 19.7418727874756, 
    17.6864433288574, 19.8386478424072, 18.0400218963623, 18.9244499206543, 
    20.6090278625488, 19.5194606781006, 21.699743270874, 21.5598011016846, 
    20.6656856536865, 19.8571510314941, 20.2716541290283, 19.5599575042725, 
    20.4675045013428, 21.5695953369141, 20.9398326873779, 19.7173652648926, 
    21.0957660675049, 20.2639751434326, 20.7198619842529, 23.569242477417, 
    25.3331413269043, 25.9176807403564, 25.7248687744141, 23.4512462615967, 
    24.7372703552246, 23.8315563201904, 24.9232635498047, 24.2255020141602, 
    23.2323818206787, 21.6694526672363, 21.9213695526123, 22.5287055969238, 
    22.8159599304199, 23.6751480102539, 27.3906841278076, 28.8802032470703, 
    28.7827205657959, 26.532772064209, 27.8327484130859, 27.9281558990479, 
    29.7156314849854, 31.9982166290283, 31.6320953369141, 31.9956893920898, 
    32.3930892944336, 34.919075012207, 34.4164581298828, 34.8764877319336, 
    35.774528503418, 37.0270729064941, 38.974494934082, 40.0207672119141, 
    40.5300941467285, 41.2724876403809, 40.3513679504395, 35.0956993103027, 
    38.0927314758301, 35.5729370117188, 42.5545501708984, 40.9972457885742, 
    38.8773231506348, 41.1227836608887, 38.3225593566895, 39.2311897277832, 
    46.659049987793, 48.1747665405273, 49.5078468322754, 0.78733503818512, 
    0.86986368894577, 1.03065097332001, 0.941482424736023, 0.637457311153412, 
    0.794924318790436, 0.771209299564362, 0.924882233142853, 0.390821516513824, 
    0.296910524368286, 0.250429689884186, 0.225766018033028, 0.328214704990387, 
    0.276990354061127, 0.334968358278275, 0.386876046657562, 0.302792072296143, 
    0.352878391742706, 0.285186380147934, 0.281543552875519, 0.235403582453728, 
    0.26651731133461, 0.323281586170197, 0.332387953996658, 0.375189363956451, 
    0.329352825880051, 0.3592529296875, 0.368359237909317, 0.353636562824249, 
    0.268946498632431, 0.231609389185905, 0.223869040608406, 0.220074817538261, 
    0.243903011083603, 0.235252320766449, 0.217494413256645, 0.21794992685318, 
    0.227815210819244, 0.214610561728477, 0.215825095772743, 0.272437214851379, 
    0.289283901453018, 0.319943130016327, 0.343164354562759, 0.314478784799576, 
    0.347414046525955, 0.317362546920776, 0.324344098567963, 0.342405378818512, 
    0.363047122955322, 0.410400837659836, 0.391277074813843, 0.425881683826447, 
    0.493877232074738, 0.490842133760452, 0.523473560810089, 0.588129639625549, 
    0.795303344726562, 1.01765418052673, 0.97743409872055, 1.16715276241302, 
    1.3617297410965, 1.26489639282227, 1.09460484981537, 1.20691883563995, 
    1.11737096309662, 1.29464447498322, 1.42335081100464, 1.62733662128448, 
    1.74815022945404, 2.05868339538574, 2.18222856521606, 2.29211401939392, 
    2.07902050018311, 1.90387177467346, 2.13669514656067, 1.81432402133942, 
    1.73843598365784, 2.06293249130249, 2.05959391593933, 2.33673524856567, 
    2.46119093894958, 2.7823474407196, 2.57532644271851, 2.60234189033508, 
    2.56834530830383, 2.82029151916504, 3.02943897247314, 3.67873430252075, 
    3.7045361995697, 3.99958634376526, 4.20357418060303, 4.65859699249268, 
    5.76595067977905, 5.53130626678467, 6.0127387046814, 4.10886716842651, 
    3.79499387741089, 4.35595560073853, 5.28026962280273, 5.72952508926392, 
    5.08265733718872, 4.82494163513184, 5.14610004425049, 3.45016074180603, 
    3.26590514183044, 2.81300663948059, 2.59080696105957, 2.73590493202209, 
    2.71101403236389, 3.19092869758606, 3.81958174705505, 4.12252616882324, 
    4.32347631454468, 4.95972108840942, 5.10603046417236, 5.62631750106812, 
    5.72193479537964, 6.06828880310059, 6.39672994613647, 5.82999801635742, 
    6.21125841140747, 7.13344812393188, 7.92541122436523, 7.79761648178101, 
    7.63521718978882, 7.80884838104248, 7.37932395935059, 8.61326122283936, 
    9.1362771987915, 9.44498920440674, 9.79133987426758, 10.3000926971436, 
    10.7217226028442, 10.5790567398071, 10.6282291412354, 10.5584144592285, 
    10.1892957687378, 11.8530559539795, 11.6815519332886, 11.5750017166138, 
    12.2871351242065, 11.6017208099365, 12.293815612793, 13.8564977645874, 
    16.4658203125, 18.1993980407715, 17.726770401001, 17.537052154541, 
    17.7273750305176, 18.5396785736084, 20.1934204101562, 20.3368301391602, 
    18.1485805511475, 17.84250831604, 16.2975273132324, 13.9492321014404, 
    13.5177249908447, 13.6353235244751, 13.6390151977539, 13.8531007766724, 
    12.2952213287354, 14.0316133499146, 15.1072492599487, 14.8802118301392, 
    16.3143939971924, 17.3559341430664, 17.6126575469971, 15.7158336639404, 
    16.5207767486572, 16.9512786865234, 18.636173248291, 19.9913597106934, 
    20.6591033935547, 21.2526664733887, 22.7865867614746, 22.5089588165283, 
    24.1287078857422, 26.5706195831299, 24.767370223999, 26.2886848449707, 
    28.8242073059082, 28.0301322937012, 28.192325592041, 29.3479480743408, 
    28.3734188079834, 27.4391841888428, 25.5073509216309, 25.0638179779053, 
    27.1543598175049, 26.8816833496094, 24.0209465026855, 22.2135562896729, 
    22.0652236938477, 25.007116317749, 21.508092880249, 22.9122905731201, 
    22.0684146881104, 24.0559520721436, 24.4922351837158, 26.2379589080811, 
    26.3516845703125, 25.6507663726807, 27.0188655853271, 28.3089199066162, 
    31.9574718475342, 33.6587829589844, 33.656421661377, 35.7908515930176, 
    33.8823890686035, 34.9904670715332, 38.5829238891602, 36.4009246826172, 
    39.9248161315918, 40.5884857177734, 40.1133613586426, 39.686710357666, 
    42.2206153869629, 39.931827545166, 39.3320617675781, 44.4752655029297, 
    44.226261138916, 45.4638671875, 54.3850936889648, 54.1226844787598, 
    52.4731559753418, 42.8157653808594, 37.9511871337891, 40.0443458557129, 
    41.6587333679199, 45.8966827392578, 48.4869155883789, 42.3013229370117, 
    48.0066032409668, 51.6740493774414, 50.6310615539551, 54.5315895080566, 
    60.5673980712891, 65.0692901611328, 71.7117385864258, 75.5848770141602, 
    66.7567596435547, 62.2470817565918, 71.9186401367188, 77.8278274536133, 
    89.5403137207031, 104.326248168945, 126.691680908203, 113.907455444336, 
    107.071632385254, 117.09423828125, 130.735336303711, 130.016052246094, 
    119.473693847656, 120.530426025391, 129.716995239258, 122.957809448242, 
    135.373596191406, 144.170501708984, 150.071395874023, 140.070587158203, 
    148.286712646484, 163.630157470703, 176.032745361328, 173.266876220703, 
    163.690521240234, 173.318908691406, 156.484298706055, 147.739440917969, 
    135.908462524414, 161.545379638672, 156.286773681641, 137.570495605469, 
    152.641540527344, 147.355712890625, 129.552719116211, 143.871017456055, 
    146.981964111328, 164.672225952148, 169.445617675781, 177.005172729492, 
    193.970001220703, 190.679992675781, 190.679992675781), stock = c(&quot;Microsoft&quot;, 
    &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, 
    &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, 
    &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, 
    &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, 
    &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, 
    &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, 
    &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, 
    &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, 
    &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, 
    &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, 
    &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, 
    &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, 
    &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, 
    &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, 
    &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, 
    &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, 
    &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, 
    &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, 
    &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, 
    &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, 
    &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, 
    &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, 
    &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, 
    &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, 
    &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, 
    &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, 
    &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, 
    &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, 
    &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, 
    &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, 
    &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, 
    &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, 
    &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, 
    &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, 
    &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, 
    &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, 
    &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, 
    &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, 
    &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, 
    &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, 
    &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, 
    &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, 
    &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, 
    &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, 
    &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, 
    &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, 
    &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, 
    &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, 
    &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, 
    &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, 
    &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, 
    &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, 
    &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, 
    &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, 
    &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, 
    &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, 
    &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, 
    &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, 
    &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, 
    &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, 
    &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, 
    &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, 
    &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, 
    &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, 
    &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, 
    &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, 
    &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, 
    &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, 
    &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, 
    &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, 
    &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, 
    &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;)), row.names = c(NA, -440L
    ), class = c(&quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;))

Now the secondary dataset:

    structure(list(Start = structure(c(11413, 13879, 18262), class = &quot;Date&quot;), 
        End = structure(c(11656, 14425, 18382), class = &quot;Date&quot;)), row.names = c(NA, 
    -3L), spec = structure(list(cols = list(Start = structure(list(
        format = &quot;&quot;), class = c(&quot;collector_date&quot;, &quot;collector&quot;)), 
        End = structure(list(format = &quot;&quot;), class = c(&quot;collector_date&quot;, 
        &quot;collector&quot;))), default = structure(list(), class = c(&quot;collector_guess&quot;, 
    &quot;collector&quot;)), delim = &quot;,&quot;), class = &quot;col_spec&quot;), problems = &lt;pointer: 0x123acf780&gt;, class = c(&quot;spec_tbl_df&quot;, 
    &quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;))

----------------

My code: 

    p1 &lt;- final_df %&gt;% 
      ggplot() +
      geom_line(aes(x = date, y = close)) +
      facet_wrap(~stock, scales = &quot;free&quot;)
    
    
    p1 + geom_rect(data = nber_date,
                   aes(xmin = Start, xmax = End, ymin = -Inf, ymax = Inf),
                   alpha = 0.2)

The output is almost what I want but there is a problem:
[![enter image description here][1]][1]


  [1]: https://i.stack.imgur.com/KBPjK.png

For Microsoft, there is empty space on the plot that shouldn&#39;t be there. It has shaded areas that shouldn&#39;t be in the plot since there is not actual data (the black line) for that area. I&#39;d like to keep the plot for Apple exactly as it is but have a &quot;trimmed&quot; version for Microsoft in which I get the shading but the plot respects that there is no Microsoft data at that area. 

How to do some &quot;adaptive&quot; shading here? In a real application, I&#39;d have many firms and sometimes very different values for the x axis.

Thanks!

</details>


# 答案1
**得分**: 1

你需要告诉 ggplot 在哪些"geom_rect"中显示哪些图例。一种方法是遍历每个股票,创建一个经过唯一过滤的 "nber_date" 的副本,并添加一个列来标识正确的股票。将这些数据框绑定在一起,然后将结果用作 "geom_rect" 图层的数据源。这比听起来复杂一些:

```r
library(tidyverse)

nber_labelled <- lapply(split(final_df, final_df$stock), function(x) {

  nber_date %>%
    filter(End > min(x$date) & Start < max(x$date)) %>%
    mutate(stock = first(x$stock))

}) %>% bind_rows()

这将得到以下结果:

nber_labelled
#> # A tibble: 4 x 3
#>   Start      End        stock    
#>   <date>     <date>     <chr>    
#> 1 2001-04-01 2001-11-30 Apple    
#> 2 2008-01-01 2009-06-30 Apple    
#> 3 2020-01-01 2020-04-30 Apple    
#> 4 2008-01-01 2009-06-30 Microsoft

因此,现在当我们绘图时,会得到以下结果:

p1 + geom_rect(data = nber_labelled,
               aes(xmin = Start, xmax = End, ymin = -Inf, ymax = Inf),
               alpha = 0.2)

How to create a function in R to add NBER recession shades to different facets in ggplot?

英文:

You have to tell ggplot which facets you want each geom_rect to appear in. One way to do this is to iterate through each stock and create a uniquely filtered copy of nber_date, and add a column to label it with the correct stock. Bind these data frames together and use the result as the source for your geom_rect layer. This is less complicated than it sounds:

library(tidyverse)

nber_labelled &lt;- lapply(split(final_df, final_df$stock), function(x) {

  nber_date %&gt;% 
    filter(End &gt; min(x$date) &amp; Start &lt; max(x$date)) %&gt;%
    mutate(stock = first(x$stock))

    }) %&gt;% bind_rows()

This results in

nber_labelled
#&gt; # A tibble: 4 x 3
#&gt;   Start      End        stock    
#&gt;   &lt;date&gt;     &lt;date&gt;     &lt;chr&gt;    
#&gt; 1 2001-04-01 2001-11-30 Apple    
#&gt; 2 2008-01-01 2009-06-30 Apple    
#&gt; 3 2020-01-01 2020-04-30 Apple    
#&gt; 4 2008-01-01 2009-06-30 Microsoft

So now when we plot, we get:

p1 + geom_rect(data = nber_labelled,
               aes(xmin = Start, xmax = End, ymin = -Inf, ymax = Inf),
               alpha = 0.2)

How to create a function in R to add NBER recession shades to different facets in ggplot?

huangapple
  • 本文由 发表于 2023年7月10日 22:03:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76654540.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定