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

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

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

问题

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

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

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

  1. structure(list(date = structure(c(12053, 12084, 12112, 12143,
  2. 12173, 12204, 12234, 12265, 12296, 12326, 12357, 12387, 12418,
  3. 12449, 12478, 12509, 12539, 12570, 12600, 12631, 12662, 12692,
  4. 12723, 12753, 12784, 12815, 12843, 12874, 12904, 12935, 12965,
  5. 12996, 13027, 13057, 13088, 13118, 13149, 13180, 13208, 13239,
  6. 13269, 13300, 13330, 13361, 13392, 13422, 13453, 13483, 13514,
  7. 13545, 13573, 13604, 13634, 13665, 13695, 13726, 13757, 13787,
  8. 13818, 13848, 13879, 13910, 13939, 13970, 14000, 14031, 14061,
  9. 14092, 14123, 14153, 14184, 14214, 14245, 14276, 14304, 14335,
  10. 14365, 14396, 14426, 14457, 14488, 14518, 14549, 14579, 14610,
  11. 14641, 14669, 14700, 14730, 14761, 14791, 14822, 14853, 14883,
  12. 14914, 14944, 14975, 15006, 15034, 15065, 15095, 15126, 15156,
  13. 15187, 15218, 15248, 15279, 15309, 15340, 15371, 15400, 15431,
  14. 15461, 15492, 15522, 15553, 15584, 15614, 15645, 15675, 15706,
  15. 15737, 15765, 15796, 15826, 15857, 15887, 15918, 15949, 15979,
  16. 16010, 16040, 16071, 16102, 16130, 16161, 16191, 16222, 16252,
  17. 16283, 16314, 16344, 16375, 16405, 16436, 16467, 16495, 16526,
  18. 16556, 16587, 16617, 16648, 16679, 16709, 16740, 16770, 16801,
  19. 16832, 16861, 16892, 16922, 16953, 16983, 17014, 17045, 17075,
  20. 17106, 17136, 17167, 17198, 17226, 17257, 17287, 17318, 17348,
  21. 17379, 17410, 17440, 17471, 17501, 17532, 17563, 17591, 17622,
  22. 17652, 17683, 17713, 17744, 17775, 17805, 17836, 17866, 17897,
  23. 17928, 17956, 17987, 18017, 18048, 18078, 18109, 18140, 18170,
  24. 18201, 18231, 18262, 18293, 18322, 18353, 18383, 18414, 18444,
  25. 18475, 18506, 185
  26. <details>
  27. <summary>英文:</summary>
  28. 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.
  29. 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.
  30. Let me show my solution and where I am stuck at. Consider the main dataset:
  31. structure(list(date = structure(c(12053, 12084, 12112, 12143,
  32. 12173, 12204, 12234, 12265, 12296, 12326, 12357, 12387, 12418,
  33. 12449, 12478, 12509, 12539, 12570, 12600, 12631, 12662, 12692,
  34. 12723, 12753, 12784, 12815, 12843, 12874, 12904, 12935, 12965,
  35. 12996, 13027, 13057, 13088, 13118, 13149, 13180, 13208, 13239,
  36. 13269, 13300, 13330, 13361, 13392, 13422, 13453, 13483, 13514,
  37. 13545, 13573, 13604, 13634, 13665, 13695, 13726, 13757, 13787,
  38. 13818, 13848, 13879, 13910, 13939, 13970, 14000, 14031, 14061,
  39. 14092, 14123, 14153, 14184, 14214, 14245, 14276, 14304, 14335,
  40. 14365, 14396, 14426, 14457, 14488, 14518, 14549, 14579, 14610,
  41. 14641, 14669, 14700, 14730, 14761, 14791, 14822, 14853, 14883,
  42. 14914, 14944, 14975, 15006, 15034, 15065, 15095, 15126, 15156,
  43. 15187, 15218, 15248, 15279, 15309, 15340, 15371, 15400, 15431,
  44. 15461, 15492, 15522, 15553, 15584, 15614, 15645, 15675, 15706,
  45. 15737, 15765, 15796, 15826, 15857, 15887, 15918, 15949, 15979,
  46. 16010, 16040, 16071, 16102, 16130, 16161, 16191, 16222, 16252,
  47. 16283, 16314, 16344, 16375, 16405, 16436, 16467, 16495, 16526,
  48. 16556, 16587, 16617, 16648, 16679, 16709, 16740, 16770, 10957,
  49. 10988, 11017, 11048, 11078, 11109, 11139, 11170, 11201, 11231,
  50. 11262, 11292, 11323, 11354, 11382, 11413, 11443, 11474, 11504,
  51. 11535, 11566, 11596, 11627, 11657, 11688, 11719, 11747, 11778,
  52. 11808, 11839, 11869, 11900, 11931, 11961, 11992, 12022, 12053,
  53. 12084, 12112, 12143, 12173, 12204, 12234, 12265, 12296, 12326,
  54. 12357, 12387, 12418, 12449, 12478, 12509, 12539, 12570, 12600,
  55. 12631, 12662, 12692, 12723, 12753, 12784, 12815, 12843, 12874,
  56. 12904, 12935, 12965, 12996, 13027, 13057, 13088, 13118, 13149,
  57. 13180, 13208, 13239, 13269, 13300, 13330, 13361, 13392, 13422,
  58. 13453, 13483, 13514, 13545, 13573, 13604, 13634, 13665, 13695,
  59. 13726, 13757, 13787, 13818, 13848, 13879, 13910, 13939, 13970,
  60. 14000, 14031, 14061, 14092, 14123, 14153, 14184, 14214, 14245,
  61. 14276, 14304, 14335, 14365, 14396, 14426, 14457, 14488, 14518,
  62. 14549, 14579, 14610, 14641, 14669, 14700, 14730, 14761, 14791,
  63. 14822, 14853, 14883, 14914, 14944, 14975, 15006, 15034, 15065,
  64. 15095, 15126, 15156, 15187, 15218, 15248, 15279, 15309, 15340,
  65. 15371, 15400, 15431, 15461, 15492, 15522, 15553, 15584, 15614,
  66. 15645, 15675, 15706, 15737, 15765, 15796, 15826, 15857, 15887,
  67. 15918, 15949, 15979, 16010, 16040, 16071, 16102, 16130, 16161,
  68. 16191, 16222, 16252, 16283, 16314, 16344, 16375, 16405, 16436,
  69. 16467, 16495, 16526, 16556, 16587, 16617, 16648, 16679, 16709,
  70. 16740, 16770, 16801, 16832, 16861, 16892, 16922, 16953, 16983,
  71. 17014, 17045, 17075, 17106, 17136, 17167, 17198, 17226, 17257,
  72. 17287, 17318, 17348, 17379, 17410, 17440, 17471, 17501, 17532,
  73. 17563, 17591, 17622, 17652, 17683, 17713, 17744, 17775, 17805,
  74. 17836, 17866, 17897, 17928, 17956, 17987, 18017, 18048, 18078,
  75. 18109, 18140, 18170, 18201, 18231, 18262, 18293, 18322, 18353,
  76. 18383, 18414, 18444, 18475, 18506, 18536, 18567, 18597, 18628,
  77. 18659, 18687, 18718, 18748, 18779, 18809, 18840, 18871, 18901,
  78. 18932, 18962, 18993, 19024, 19052, 19083, 19113, 19144, 19174,
  79. 19205, 19236, 19266, 19297, 19327, 19358, 19389, 19417, 19448,
  80. 19478, 19509, 19539, 19545), class = &quot;Date&quot;), close = c(14.7728071212769,
  81. 14.7541389465332, 15.1200866699219, 15.9694652557373, 15.3699064254761,
  82. 16.0131816864014, 16.4940738677979, 16.5627689361572, 17.3621864318848,
  83. 16.3254470825195, 16.1469821929932, 17.1895370483398, 17.3653869628906,
  84. 16.6619873046875, 15.6571092605591, 16.4107532501221, 16.4735546112061,
  85. 17.9369068145752, 17.8929424285889, 17.1455764770508, 17.4166088104248,
  86. 17.6181774139404, 16.8875007629395, 18.7586135864258, 18.4497222900391,
  87. 17.6634349822998, 17.0207576751709, 17.8165168762207, 18.1686210632324,
  88. 17.5480690002441, 18.0920276641846, 19.3424339294434, 18.2307147979736,
  89. 18.2094573974609, 19.612377166748, 18.5826187133789, 20.003849029541,
  90. 19.0942687988281, 19.4013919830322, 17.2195377349854, 16.1499938964844,
  91. 16.6782550811768, 17.222261428833, 18.396183013916, 19.6493511199951,
  92. 20.6264305114746, 21.0934143066406, 21.5259799957275, 22.2468776702881,
  93. 20.3076705932617, 20.1610584259033, 21.6584911346436, 22.2010383605957,
  94. 21.3875560760498, 21.0392036437988, 20.8505077362061, 21.4552383422852,
  95. 26.8081226348877, 24.4703216552734, 26.0126209259033, 23.8205490112305,
  96. 19.874813079834, 20.8176021575928, 20.9202938079834, 20.7735958099365,
  97. 20.2537212371826, 18.9358673095703, 20.0917453765869, 19.728385925293,
  98. 16.5056133270264, 14.945969581604, 14.4667568206787, 12.7253923416138,
  99. 12.0184240341187, 13.7642221450806, 15.1803569793701, 15.6524019241333,
  100. 17.923433303833, 17.7349262237549, 18.5869884490967, 19.5028495788574,
  101. 21.0269870758057, 22.3008842468262, 23.2144012451172, 21.4626636505127,
  102. 21.8358612060547, 22.4123878479004, 23.36887550354, 19.7418727874756,
  103. 17.6864433288574, 19.8386478424072, 18.0400218963623, 18.9244499206543,
  104. 20.6090278625488, 19.5194606781006, 21.699743270874, 21.5598011016846,
  105. 20.6656856536865, 19.8571510314941, 20.2716541290283, 19.5599575042725,
  106. 20.4675045013428, 21.5695953369141, 20.9398326873779, 19.7173652648926,
  107. 21.0957660675049, 20.2639751434326, 20.7198619842529, 23.569242477417,
  108. 25.3331413269043, 25.9176807403564, 25.7248687744141, 23.4512462615967,
  109. 24.7372703552246, 23.8315563201904, 24.9232635498047, 24.2255020141602,
  110. 23.2323818206787, 21.6694526672363, 21.9213695526123, 22.5287055969238,
  111. 22.8159599304199, 23.6751480102539, 27.3906841278076, 28.8802032470703,
  112. 28.7827205657959, 26.532772064209, 27.8327484130859, 27.9281558990479,
  113. 29.7156314849854, 31.9982166290283, 31.6320953369141, 31.9956893920898,
  114. 32.3930892944336, 34.919075012207, 34.4164581298828, 34.8764877319336,
  115. 35.774528503418, 37.0270729064941, 38.974494934082, 40.0207672119141,
  116. 40.5300941467285, 41.2724876403809, 40.3513679504395, 35.0956993103027,
  117. 38.0927314758301, 35.5729370117188, 42.5545501708984, 40.9972457885742,
  118. 38.8773231506348, 41.1227836608887, 38.3225593566895, 39.2311897277832,
  119. 46.659049987793, 48.1747665405273, 49.5078468322754, 0.78733503818512,
  120. 0.86986368894577, 1.03065097332001, 0.941482424736023, 0.637457311153412,
  121. 0.794924318790436, 0.771209299564362, 0.924882233142853, 0.390821516513824,
  122. 0.296910524368286, 0.250429689884186, 0.225766018033028, 0.328214704990387,
  123. 0.276990354061127, 0.334968358278275, 0.386876046657562, 0.302792072296143,
  124. 0.352878391742706, 0.285186380147934, 0.281543552875519, 0.235403582453728,
  125. 0.26651731133461, 0.323281586170197, 0.332387953996658, 0.375189363956451,
  126. 0.329352825880051, 0.3592529296875, 0.368359237909317, 0.353636562824249,
  127. 0.268946498632431, 0.231609389185905, 0.223869040608406, 0.220074817538261,
  128. 0.243903011083603, 0.235252320766449, 0.217494413256645, 0.21794992685318,
  129. 0.227815210819244, 0.214610561728477, 0.215825095772743, 0.272437214851379,
  130. 0.289283901453018, 0.319943130016327, 0.343164354562759, 0.314478784799576,
  131. 0.347414046525955, 0.317362546920776, 0.324344098567963, 0.342405378818512,
  132. 0.363047122955322, 0.410400837659836, 0.391277074813843, 0.425881683826447,
  133. 0.493877232074738, 0.490842133760452, 0.523473560810089, 0.588129639625549,
  134. 0.795303344726562, 1.01765418052673, 0.97743409872055, 1.16715276241302,
  135. 1.3617297410965, 1.26489639282227, 1.09460484981537, 1.20691883563995,
  136. 1.11737096309662, 1.29464447498322, 1.42335081100464, 1.62733662128448,
  137. 1.74815022945404, 2.05868339538574, 2.18222856521606, 2.29211401939392,
  138. 2.07902050018311, 1.90387177467346, 2.13669514656067, 1.81432402133942,
  139. 1.73843598365784, 2.06293249130249, 2.05959391593933, 2.33673524856567,
  140. 2.46119093894958, 2.7823474407196, 2.57532644271851, 2.60234189033508,
  141. 2.56834530830383, 2.82029151916504, 3.02943897247314, 3.67873430252075,
  142. 3.7045361995697, 3.99958634376526, 4.20357418060303, 4.65859699249268,
  143. 5.76595067977905, 5.53130626678467, 6.0127387046814, 4.10886716842651,
  144. 3.79499387741089, 4.35595560073853, 5.28026962280273, 5.72952508926392,
  145. 5.08265733718872, 4.82494163513184, 5.14610004425049, 3.45016074180603,
  146. 3.26590514183044, 2.81300663948059, 2.59080696105957, 2.73590493202209,
  147. 2.71101403236389, 3.19092869758606, 3.81958174705505, 4.12252616882324,
  148. 4.32347631454468, 4.95972108840942, 5.10603046417236, 5.62631750106812,
  149. 5.72193479537964, 6.06828880310059, 6.39672994613647, 5.82999801635742,
  150. 6.21125841140747, 7.13344812393188, 7.92541122436523, 7.79761648178101,
  151. 7.63521718978882, 7.80884838104248, 7.37932395935059, 8.61326122283936,
  152. 9.1362771987915, 9.44498920440674, 9.79133987426758, 10.3000926971436,
  153. 10.7217226028442, 10.5790567398071, 10.6282291412354, 10.5584144592285,
  154. 10.1892957687378, 11.8530559539795, 11.6815519332886, 11.5750017166138,
  155. 12.2871351242065, 11.6017208099365, 12.293815612793, 13.8564977645874,
  156. 16.4658203125, 18.1993980407715, 17.726770401001, 17.537052154541,
  157. 17.7273750305176, 18.5396785736084, 20.1934204101562, 20.3368301391602,
  158. 18.1485805511475, 17.84250831604, 16.2975273132324, 13.9492321014404,
  159. 13.5177249908447, 13.6353235244751, 13.6390151977539, 13.8531007766724,
  160. 12.2952213287354, 14.0316133499146, 15.1072492599487, 14.8802118301392,
  161. 16.3143939971924, 17.3559341430664, 17.6126575469971, 15.7158336639404,
  162. 16.5207767486572, 16.9512786865234, 18.636173248291, 19.9913597106934,
  163. 20.6591033935547, 21.2526664733887, 22.7865867614746, 22.5089588165283,
  164. 24.1287078857422, 26.5706195831299, 24.767370223999, 26.2886848449707,
  165. 28.8242073059082, 28.0301322937012, 28.192325592041, 29.3479480743408,
  166. 28.3734188079834, 27.4391841888428, 25.5073509216309, 25.0638179779053,
  167. 27.1543598175049, 26.8816833496094, 24.0209465026855, 22.2135562896729,
  168. 22.0652236938477, 25.007116317749, 21.508092880249, 22.9122905731201,
  169. 22.0684146881104, 24.0559520721436, 24.4922351837158, 26.2379589080811,
  170. 26.3516845703125, 25.6507663726807, 27.0188655853271, 28.3089199066162,
  171. 31.9574718475342, 33.6587829589844, 33.656421661377, 35.7908515930176,
  172. 33.8823890686035, 34.9904670715332, 38.5829238891602, 36.4009246826172,
  173. 39.9248161315918, 40.5884857177734, 40.1133613586426, 39.686710357666,
  174. 42.2206153869629, 39.931827545166, 39.3320617675781, 44.4752655029297,
  175. 44.226261138916, 45.4638671875, 54.3850936889648, 54.1226844787598,
  176. 52.4731559753418, 42.8157653808594, 37.9511871337891, 40.0443458557129,
  177. 41.6587333679199, 45.8966827392578, 48.4869155883789, 42.3013229370117,
  178. 48.0066032409668, 51.6740493774414, 50.6310615539551, 54.5315895080566,
  179. 60.5673980712891, 65.0692901611328, 71.7117385864258, 75.5848770141602,
  180. 66.7567596435547, 62.2470817565918, 71.9186401367188, 77.8278274536133,
  181. 89.5403137207031, 104.326248168945, 126.691680908203, 113.907455444336,
  182. 107.071632385254, 117.09423828125, 130.735336303711, 130.016052246094,
  183. 119.473693847656, 120.530426025391, 129.716995239258, 122.957809448242,
  184. 135.373596191406, 144.170501708984, 150.071395874023, 140.070587158203,
  185. 148.286712646484, 163.630157470703, 176.032745361328, 173.266876220703,
  186. 163.690521240234, 173.318908691406, 156.484298706055, 147.739440917969,
  187. 135.908462524414, 161.545379638672, 156.286773681641, 137.570495605469,
  188. 152.641540527344, 147.355712890625, 129.552719116211, 143.871017456055,
  189. 146.981964111328, 164.672225952148, 169.445617675781, 177.005172729492,
  190. 193.970001220703, 190.679992675781, 190.679992675781), stock = c(&quot;Microsoft&quot;,
  191. &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;,
  192. &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;,
  193. &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;,
  194. &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;,
  195. &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;,
  196. &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;,
  197. &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;,
  198. &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;,
  199. &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;,
  200. &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;,
  201. &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;,
  202. &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;,
  203. &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;,
  204. &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;,
  205. &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;,
  206. &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;,
  207. &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;,
  208. &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;,
  209. &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;,
  210. &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;,
  211. &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;,
  212. &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;,
  213. &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;,
  214. &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;,
  215. &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;,
  216. &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;,
  217. &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;,
  218. &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;,
  219. &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;,
  220. &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;,
  221. &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;, &quot;Microsoft&quot;,
  222. &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;,
  223. &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;,
  224. &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;,
  225. &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;,
  226. &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;,
  227. &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;,
  228. &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;,
  229. &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;,
  230. &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;,
  231. &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;,
  232. &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;,
  233. &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;,
  234. &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;,
  235. &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;,
  236. &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;,
  237. &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;,
  238. &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;,
  239. &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;,
  240. &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;,
  241. &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;,
  242. &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;,
  243. &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;,
  244. &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;,
  245. &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;,
  246. &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;,
  247. &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;,
  248. &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;,
  249. &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;,
  250. &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;,
  251. &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;,
  252. &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;,
  253. &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;,
  254. &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;,
  255. &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;,
  256. &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;,
  257. &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;,
  258. &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;,
  259. &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;,
  260. &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;,
  261. &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;,
  262. &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;, &quot;Apple&quot;)), row.names = c(NA, -440L
  263. ), class = c(&quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;))
  264. Now the secondary dataset:
  265. structure(list(Start = structure(c(11413, 13879, 18262), class = &quot;Date&quot;),
  266. End = structure(c(11656, 14425, 18382), class = &quot;Date&quot;)), row.names = c(NA,
  267. -3L), spec = structure(list(cols = list(Start = structure(list(
  268. format = &quot;&quot;), class = c(&quot;collector_date&quot;, &quot;collector&quot;)),
  269. End = structure(list(format = &quot;&quot;), class = c(&quot;collector_date&quot;,
  270. &quot;collector&quot;))), default = structure(list(), class = c(&quot;collector_guess&quot;,
  271. &quot;collector&quot;)), delim = &quot;,&quot;), class = &quot;col_spec&quot;), problems = &lt;pointer: 0x123acf780&gt;, class = c(&quot;spec_tbl_df&quot;,
  272. &quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;))
  273. ----------------
  274. My code:
  275. p1 &lt;- final_df %&gt;%
  276. ggplot() +
  277. geom_line(aes(x = date, y = close)) +
  278. facet_wrap(~stock, scales = &quot;free&quot;)
  279. p1 + geom_rect(data = nber_date,
  280. aes(xmin = Start, xmax = End, ymin = -Inf, ymax = Inf),
  281. alpha = 0.2)
  282. The output is almost what I want but there is a problem:
  283. [![enter image description here][1]][1]
  284. [1]: https://i.stack.imgur.com/KBPjK.png
  285. 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.
  286. 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.
  287. Thanks!
  288. </details>
  289. # 答案1
  290. **得分**: 1
  291. 你需要告诉 ggplot 在哪些"geom_rect"中显示哪些图例。一种方法是遍历每个股票,创建一个经过唯一过滤的 "nber_date" 的副本,并添加一个列来标识正确的股票。将这些数据框绑定在一起,然后将结果用作 "geom_rect" 图层的数据源。这比听起来复杂一些:
  292. ```r
  293. library(tidyverse)
  294. nber_labelled <- lapply(split(final_df, final_df$stock), function(x) {
  295. nber_date %>%
  296. filter(End > min(x$date) & Start < max(x$date)) %>%
  297. mutate(stock = first(x$stock))
  298. }) %>% bind_rows()

这将得到以下结果:

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

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

  1. p1 + geom_rect(data = nber_labelled,
  2. aes(xmin = Start, xmax = End, ymin = -Inf, ymax = Inf),
  3. 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:

  1. library(tidyverse)
  2. nber_labelled &lt;- lapply(split(final_df, final_df$stock), function(x) {
  3. nber_date %&gt;%
  4. filter(End &gt; min(x$date) &amp; Start &lt; max(x$date)) %&gt;%
  5. mutate(stock = first(x$stock))
  6. }) %&gt;% bind_rows()

This results in

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

So now when we plot, we get:

  1. p1 + geom_rect(data = nber_labelled,
  2. aes(xmin = Start, xmax = End, ymin = -Inf, ymax = Inf),
  3. 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:

确定