ggplot标记前`n`行

时间:2018-11-02 20:25:29

标签: r ggplot2

我想知道是否可以在n图中标记顶部的ggplot数据点。

例如,我有一个名为x的数据帧,其中有3个symbols-是否可以标记性能最高的符号或前2个符号?

当我说表现最佳的符号时,是指在该时间段内达到最大值的符号,即在此期间1.0列中adj.y以上的所有符号。

标记所有符号也是一种选择,但在整个数据(100个符号)上,会有些拥挤。

我有以下代码:

ggplot(x, aes(x=date, y=adj.x-1, color= symbol)) + 
  geom_line(size = 1) +
  theme_tq() +
  theme(legend.position="none")

数据

structure(list(symbol = c("ZTS", "ZTS", "ZTS", "ZTS", "ZTS", 
"ZTS", "ZTS", "ZTS", "ZTS", "ZTS", "ZTS", "ZTS", "ZTS", "ZTS", 
"ZTS", "ZTS", "ZTS", "ZTS", "ZTS", "ZTS", "ZTS", "ZTS", "ZTS", 
"ZTS", "ZTS", "ZTS", "ZTS", "ZTS", "ZTS", "ZTS", "ZTS", "ZTS", 
"ZTS", "ZTS", "ZTS", "ZTS", "ZTS", "ZTS", "ZTS", "ZTS", "ZTS", 
"ZTS", "ZTS", "ZTS", "ZTS", "ZTS", "ZTS", "ZTS", "ZTS", "ZTS", 
"ZTS", "ZTS", "ZTS", "ZTS", "ZTS", "ZTS", "ZTS", "ZTS", "ZTS", 
"ZTS", "ZTS", "ZTS", "ZTS", "ZTS", "ZTS", "ZTS", "ZTS", "ZTS", 
"ZTS", "ZTS", "ZTS", "ZTS", "ZTS", "ZTS", "ZTS", "ZTS", "ZTS", 
"ZTS", "ZTS", "ZTS", "ZTS", "ZTS", "ZTS", "ZTS", "ZTS", "ZTS", 
"ZTS", "ZTS", "ZTS", "ZTS", "ZTS", "ZTS", "ZTS", "ZTS", "ZTS", 
"ZTS", "ZTS", "ZTS", "ZTS", "ZTS", "PRAH", "PRAH", "PRAH", "PRAH", 
"PRAH", "PRAH", "PRAH", "PRAH", "PRAH", "PRAH", "PRAH", "PRAH", 
"PRAH", "PRAH", "PRAH", "PRAH", "PRAH", "PRAH", "PRAH", "PRAH", 
"PRAH", "PRAH", "PRAH", "PRAH", "PRAH", "PRAH", "PRAH", "PRAH", 
"PRAH", "PRAH", "PRAH", "PRAH", "PRAH", "PRAH", "PRAH", "PRAH", 
"PRAH", "PRAH", "PRAH", "PRAH", "PRAH", "PRAH", "PRAH", "PRAH", 
"PRAH", "PRAH", "PRAH", "PRAH", "PRAH", "PRAH", "PRAH", "PRAH", 
"PRAH", "PRAH", "PRAH", "PRAH", "PRAH", "PRAH", "PRAH", "PRAH", 
"PRAH", "PRAH", "PRAH", "PRAH", "PRAH", "PRAH", "PRAH", "PRAH", 
"PRAH", "PRAH", "PRAH", "PRAH", "PRAH", "PRAH", "PRAH", "PRAH", 
"PRAH", "PRAH", "PRAH", "PRAH", "PRAH", "PRAH", "PRAH", "PRAH", 
"PRAH", "PRAH", "PRAH", "PRAH", "PRAH", "PRAH", "PRAH", "PRAH", 
"PRAH", "PRAH", "PRAH", "PRAH", "PRAH", "PRAH", "PRAH", "PRAH", 
"ABT", "ABT", "ABT", "ABT", "ABT", "ABT", "ABT", "ABT", "ABT", 
"ABT", "ABT", "ABT", "ABT", "ABT", "ABT", "ABT", "ABT", "ABT", 
"ABT", "ABT", "ABT", "ABT", "ABT", "ABT", "ABT", "ABT", "ABT", 
"ABT", "ABT", "ABT", "ABT", "ABT", "ABT", "ABT", "ABT", "ABT", 
"ABT", "ABT", "ABT", "ABT", "ABT", "ABT", "ABT", "ABT", "ABT", 
"ABT", "ABT", "ABT", "ABT", "ABT", "ABT", "ABT", "ABT", "ABT", 
"ABT", "ABT", "ABT", "ABT", "ABT", "ABT", "ABT", "ABT", "ABT", 
"ABT", "ABT", "ABT", "ABT", "ABT", "ABT", "ABT", "ABT", "ABT", 
"ABT", "ABT", "ABT", "ABT", "ABT", "ABT", "ABT", "ABT", "ABT", 
"ABT", "ABT", "ABT", "ABT", "ABT", "ABT", "ABT", "ABT", "ABT", 
"ABT", "ABT", "ABT", "ABT", "ABT", "ABT", "ABT", "ABT", "ABT", 
"ABT", "ABT"), date = structure(c(17324, 17364, 17415, 17420, 
17423, 17428, 17431, 17436, 17441, 17444, 17449, 17452, 17457, 
17462, 17465, 17470, 17473, 17478, 17483, 17486, 17491, 17497, 
17500, 17505, 17508, 17513, 17518, 17521, 17527, 17533, 17536, 
17541, 17547, 17550, 17555, 17560, 17563, 17568, 17571, 17576, 
17582, 17585, 17590, 17595, 17598, 17603, 17606, 17611, 17616, 
17619, 17625, 17630, 17633, 17638, 17641, 17646, 17651, 17654, 
17659, 17662, 17667, 17672, 17675, 17681, 17686, 17689, 17694, 
17697, 17702, 17707, 17710, 17715, 17721, 17724, 17729, 17732, 
17737, 17742, 17745, 17750, 17753, 17758, 17763, 17766, 17771, 
17774, 17780, 17785, 17788, 17793, 17798, 17801, 17806, 17809, 
17814, 17819, 17822, 17827, 17830, 17835, 17359, 17367, 17372, 
17401, 17409, 17415, 17420, 17436, 17441, 17444, 17449, 17452, 
17457, 17462, 17465, 17470, 17473, 17478, 17483, 17486, 17491, 
17497, 17500, 17505, 17508, 17513, 17518, 17521, 17527, 17533, 
17536, 17541, 17547, 17550, 17555, 17560, 17563, 17568, 17571, 
17576, 17582, 17585, 17590, 17595, 17598, 17603, 17606, 17611, 
17616, 17619, 17625, 17630, 17633, 17638, 17641, 17646, 17651, 
17654, 17659, 17662, 17667, 17672, 17675, 17681, 17686, 17689, 
17694, 17697, 17702, 17707, 17710, 17715, 17721, 17724, 17729, 
17732, 17737, 17742, 17745, 17750, 17753, 17758, 17763, 17766, 
17771, 17774, 17780, 17785, 17788, 17793, 17798, 17801, 17806, 
17809, 17814, 17819, 17822, 17827, 17830, 17835, 17371, 17374, 
17408, 17414, 17417, 17422, 17427, 17430, 17435, 17438, 17443, 
17448, 17451, 17456, 17459, 17464, 17469, 17472, 17477, 17480, 
17485, 17490, 17494, 17499, 17504, 17507, 17512, 17515, 17520, 
17526, 17529, 17535, 17540, 17543, 17549, 17554, 17557, 17562, 
17567, 17570, 17575, 17578, 17584, 17589, 17592, 17597, 17602, 
17605, 17610, 17613, 17618, 17624, 17627, 17632, 17637, 17640, 
17645, 17648, 17653, 17658, 17661, 17666, 17669, 17674, 17680, 
17683, 17688, 17693, 17696, 17701, 17704, 17709, 17714, 17718, 
17723, 17728, 17731, 17736, 17739, 17744, 17749, 17752, 17757, 
17760, 17765, 17770, 17773, 17779, 17784, 17787, 17792, 17795, 
17800, 17805, 17808, 17813, 17816, 17821, 17826, 17829, 17834
), class = "Date"), adjusted.x = c(62.767822, 62.685722, 63.003765, 
64.882256, 64.852432, 64.3853, 63.769089, 62.824867, 63.36158, 
63.36158, 63.600117, 65.041275, 65.160538, 64.882256, 63.351639, 
63.431149, 68.271484, 69.192627, 68.565498, 70.088516, 71.03418, 
71.213356, 71.95993, 70.526505, 71.551804, 72.049515, 72.29837, 
71.949974, 72.119194, 71.442299, 73.02504, 73.57254, 75.195091, 
76.395554, 78.10054, 78.948051, 77.592033, 73.055351, 72.138054, 
74.241875, 80.114616, 82.54747, 80.623123, 81.650108, 82.298203, 
84.342194, 84.352165, 83.85363, 83.484718, 83.265366, 82.6073, 
81.630173, 83.714043, 85.419037, 85.32473, 84.446022, 83.35762, 
80.262161, 81.879791, 84.326195, 82.728546, 85.254829, 84.625755, 
84.326195, 85.264816, 85.534431, 87.02224, 89.069229, 86.872459, 
84.575829, 84.705643, 85.943825, 87.36174, 85.344704, 85.644257, 
85.290001, 85.639999, 84.489998, 91.43, 92.489998, 91.699997, 
90.849998, 92.160004, 89.669998, 89.550003, 90.599998, 89.739998, 
88.239998, 89.010002, 89.080002, 89.650002, 91.199997, 91.830002, 
91.949997, 89.629997, 90.080002, 91.830002, 91.470001, 88.849998, 
90.150002, 76.75, 76.239998, 76.809998, 77.169998, 77.400002, 
77.580002, 77.239998, 75.849998, 76.110001, 79.690002, 81.089996, 
82.110001, 82.400002, 81.559998, 82.239998, 81.43, 83.419998, 
83.5, 79.599998, 79.900002, 83.57, 82.559998, 82.370003, 81, 
82.709999, 87.230003, 89.720001, 91.07, 91.349998, 90.790001, 
90.709999, 92.150002, 90.370003, 93.5, 92.559998, 94.349998, 
92.510002, 88.290001, 85.43, 88.919998, 88.139999, 86.18, 84, 
84.389999, 87.910004, 92.169998, 90.769997, 88.379997, 86.900002, 
82.959999, 83.489998, 82.199997, 86.410004, 88.610001, 87.269997, 
84.760002, 82.169998, 82.459999, 81.470001, 82.489998, 80.160004, 
81.099998, 80.889999, 88.660004, 85.610001, 85.93, 91.720001, 
96, 95.459999, 94.239998, 92.769997, 93.93, 96.540001, 99.480003, 
99.419998, 102.519997, 106.860001, 102.720001, 105.050003, 101.739998, 
101.419998, 101.160004, 102.029999, 104.470001, 105.730003, 105.599998, 
105.919998, 109.339996, 111.629997, 107.529999, 108.440002, 109.510002, 
109.099998, 106.370003, 99.169998, 98.389999, 102.260002, 99.690002, 
96.360001, 96.870003, 49.642918, 48.968636, 48.968636, 49.867672, 
50.835129, 50.815582, 50.893764, 50.258572, 51.646221, 52.144611, 
53.102283, 53.414997, 53.725376, 54.06908, 55.306404, 54.491341, 
53.312935, 53.391499, 54.167282, 53.813759, 53.843224, 54.294945, 
55.119827, 54.579727, 53.725376, 53.705738, 54.697563, 54.501163, 
55.993805, 55.974163, 56.042904, 57.761414, 57.859608, 58.057377, 
58.284313, 58.442188, 62.576454, 61.333218, 57.948837, 55.521557, 
56.873337, 59.369682, 58.205379, 59.744625, 58.669125, 60.474789, 
62.773792, 61.7575, 61.8167, 58.708595, 58.442188, 58.215248, 
56.804268, 57.672562, 58.763157, 59.367939, 57.811367, 59.050678, 
57.355297, 58.812729, 60.042122, 60.022297, 61.182289, 61.31118, 
60.775795, 61.876305, 62.996643, 62.590149, 62.4216, 61.8069, 
61.410324, 59.972725, 60.478359, 61.628441, 62.034939, 61.527023, 
63.518833, 64.136299, 64.992775, 64.51474, 64.972855, 64.375313, 
63.36945, 64.454987, 65.401093, 66.566299, 66.745567, 65.729744, 
66.277496, 68.219505, 68.129883, 68.767258, 72.541733, 73.816498, 
71.525909, 70.998085, 69.330002, 70.230003, 68.519997, 67.669998, 
68), adjusted.y = c(236.954025, 239.936966, 241.275742, 243.533127, 
244.393082, 245.488113, 244.967621, 245.566666, 247.795975, 250.094025, 
250.054718, 250.378815, 251.13501, 251.518005, 251.036789, 252.539352, 
253.816071, 254.464203, 253.698196, 253.983017, 255.328445, 255.564178, 
260.258484, 258.471069, 260.749451, 261.967224, 264.737549, 264.125549, 
263.868896, 265.300171, 269.890198, 270.581116, 273.394348, 276.789948, 
279.52417, 281.004761, 277.944794, 265.655548, 258.124054, 266.109619, 
267.896271, 271.163513, 268.143005, 268.676025, 270.561401, 273.147583, 
271.744141, 268.007874, 262.735504, 260.79306, 261.199402, 258.662323, 
263.548187, 267.77002, 264.222076, 261.268799, 262.1409, 260.267822, 
264.529327, 270.406189, 269.801666, 270.921539, 270.356659, 270.168335, 
272.437836, 274.885712, 276.421844, 275.881134, 274.726379, 269.778778, 
269.669281, 269.679199, 276.647675, 278.111053, 279.206085, 278.419647, 
282.730133, 278.688446, 281.117462, 284.29306, 281.883972, 280.510193, 
284.38266, 284.502136, 288.613525, 289.00174, 286.86145, 287.747406, 
289.569183, 289.907654, 291.019989, 290.690002, 291.559998, 287.820007, 
278.299988, 274.399994, 276.399994, 273.609985, 265.329987, 270.630005, 
238.451569, 241.471191, 241.78392, 238.989059, 241.85231, 241.275742, 
243.533127, 245.566666, 247.795975, 250.094025, 250.054718, 250.378815, 
251.13501, 251.518005, 251.036789, 252.539352, 253.816071, 254.464203, 
253.698196, 253.983017, 255.328445, 255.564178, 260.258484, 258.471069, 
260.749451, 261.967224, 264.737549, 264.125549, 263.868896, 265.300171, 
269.890198, 270.581116, 273.394348, 276.789948, 279.52417, 281.004761, 
277.944794, 265.655548, 258.124054, 266.109619, 267.896271, 271.163513, 
268.143005, 268.676025, 270.561401, 273.147583, 271.744141, 268.007874, 
262.735504, 260.79306, 261.199402, 258.662323, 263.548187, 267.77002, 
264.222076, 261.268799, 262.1409, 260.267822, 264.529327, 270.406189, 
269.801666, 270.921539, 270.356659, 270.168335, 272.437836, 274.885712, 
276.421844, 275.881134, 274.726379, 269.778778, 269.669281, 269.679199, 
276.647675, 278.111053, 279.206085, 278.419647, 282.730133, 278.688446, 
281.117462, 284.29306, 281.883972, 280.510193, 284.38266, 284.502136, 
288.613525, 289.00174, 286.86145, 287.747406, 289.569183, 289.907654, 
291.019989, 290.690002, 291.559998, 287.820007, 278.299988, 274.399994, 
276.399994, 273.609985, 265.329987, 270.630005, 241.197571, 241.568924, 
240.406021, 240.454895, 240.963058, 244.471268, 245.242584, 244.918488, 
244.614059, 246.725494, 248.620911, 249.396759, 250.074371, 250.889496, 
252.500076, 250.712708, 252.14653, 252.971466, 254.03212, 253.462494, 
251.842102, 253.668762, 255.691818, 257.999664, 259.404053, 259.335266, 
261.996704, 263.069366, 263.582672, 263.74057, 263.414856, 268.103546, 
270.995728, 274.332092, 275.536346, 279.632751, 282.880249, 278.260712, 
260.522675, 254.304031, 262.565948, 269.584137, 266.909149, 270.887115, 
265.606201, 269.258453, 274.924316, 271.449738, 268.523254, 255.738739, 
257.502777, 258.434387, 257.393829, 261.397644, 264.935638, 266.481689, 
260.624603, 264.172546, 260.842651, 264.529327, 269.583618, 268.671906, 
268.899811, 270.911621, 266.610474, 271.149475, 274.915466, 276.065063, 
276.233551, 274.258484, 273.501892, 268.1362, 270.634888, 274.178864, 
275.612335, 278.081177, 278.73822, 280.340942, 280.151825, 279.59433, 
283.3573, 283.78537, 282.620636, 283.775391, 284.880402, 288.474121, 
288.991791, 287.727509, 286.801697, 289.519379, 289.59903, 291.98999, 
289.880005, 291.730011, 289.440002, 287.399994, 275.950012, 280.450012, 
275.01001, 270.079987, 267.769989), adj.x = c(1.49919377024561, 
1.49723283222649, 1.50482920994165, 1.5496964988634, 1.54898416006337, 
1.53782682877533, 1.52310878276194, 1.50055627584002, 1.51337553195519, 
1.51337553195519, 1.51907292869413, 1.5534946280091, 1.55634319501244, 
1.5496964988634, 1.5131380936501, 1.51503717016533, 1.63064736415775, 
1.65264863492202, 1.63766981520225, 1.67404672019621, 1.69663366892841, 
1.7009132429907, 1.7187449767384, 1.68450797819934, 1.70899698904058, 
1.72088469211529, 1.72682853171028, 1.71850718016205, 1.72254895764799, 
1.70638148943298, 1.74418486338329, 1.75726176430251, 1.79601599017171, 
1.82468874945608, 1.86541191473584, 1.88565450354853, 1.85326635189432, 
1.74490883405683, 1.72299942404313, 1.77324866380346, 1.91351761755919, 
1.97162572844309, 1.92566318040071, 1.95019246093136, 1.96567204833089, 
2.01449226346657, 2.01473041831418, 2.00282303420502, 1.99401166311477, 
1.98877249531489, 1.97305475306775, 1.94971631842939, 1.99948903353175, 
2.04021238988951, 2.03795989072058, 2.01697216934537, 1.99097595909096, 
1.91704169307723, 1.95567838208904, 2.01411013134272, 1.9759506835314, 
2.03629032277326, 2.0212650472137, 2.01411013134272, 2.03652885977688, 
2.04296854679302, 2.07850449360531, 2.1273963152231, 2.07492701178507, 
2.02007257715837, 2.02317315216463, 2.05274682035453, 2.08661336641276, 
2.03843696472781, 2.04559171340554, 2.03713039721917, 2.04549001213775, 
2.018022525135, 2.18378274163402, 2.20910052943416, 2.19023155262486, 
2.16992953855283, 2.20121870506532, 2.1417455329187, 2.13887948227797, 
2.16395835091848, 2.14341746545632, 2.10759033965023, 2.12598169310303, 
2.12765362564064, 2.14126793344696, 2.17828917735617, 2.19333668961848, 
2.19620274025921, 2.14079011901246, 2.15153837617804, 2.19333668961848, 
2.18473815554027, 2.12216003747804, 2.15321030871566, 1.83315460374506, 
1.82097333320149, 1.8345876410078, 1.84318615120127, 1.84867973936387, 
1.8529789944606, 1.84485808373888, 1.81165828049191, 1.81786838728588, 
1.9033758180945, 1.93681432553835, 1.96117689051025, 1.96810349205084, 
1.94804020606043, 1.96428183642586, 1.94493523626007, 1.99246584205999, 
1.99437666987248, 1.90122609500714, 1.90839161570736, 1.9960486024101, 
1.97192495659783, 1.96738697341947, 1.93466479352899, 1.97550769306319, 
2.08346686103121, 2.14293984209982, 2.17518423144056, 2.18187191382153, 
2.16849652517484, 2.16658569736234, 2.20097980979045, 2.15846497771863, 
2.23322417524643, 2.21077246197177, 2.25352616543371, 2.20957831998391, 
2.10878464883135, 2.04047423840965, 2.12383197001566, 2.10520188848124, 
2.0583878013127, 2.00631904514118, 2.01563407396601, 2.09970851528139, 
2.20145740926219, 2.16801873462509, 2.11093418084071, 2.07558486946912, 
1.98147888069753, 1.9941377745976, 1.96332642251961, 2.0638813894753, 
2.11642776900332, 2.08442210774421, 2.0244715033191, 1.96260990388824, 
1.96953650542884, 1.94589065016632, 1.97025302406021, 1.91460169861658, 
1.93705322081323, 1.93203744708513, 2.11762207818444, 2.04477351739113, 
2.05241661367835, 2.19070934317461, 2.29293605158991, 2.28003826241497, 
2.2508988428746, 2.21578823569988, 2.24349461797751, 2.30583384076486, 
2.3760550551143, 2.3746218506583, 2.44866455343948, 2.55232446631077, 
2.45344159908596, 2.50909311560759, 2.43003447190506, 2.42239135173309, 
2.41618145990187, 2.43696107344566, 2.4952399125264, 2.52533474597302, 
2.5222296089794, 2.52987272915137, 2.61155852821976, 2.66625463083515, 
2.56832720140133, 2.59006239604461, 2.61561907911962, 2.60582623586029, 
2.54062098631695, 2.36865066302396, 2.35002058148954, 2.44245463772351, 
2.3810708288424, 2.30153458566813, 2.3137158562117, 1.18570871237835, 
1.1696036550165, 1.1696036550165, 1.19107690560063, 1.21418437470129, 
1.21371749948254, 1.21558485704905, 1.2004134545857, 1.23355710478417, 
1.2454610256045, 1.26833478242115, 1.2758038783007, 1.28321720328775, 
1.29142648758645, 1.32097966266038, 1.30151208623312, 1.27336615289135, 
1.27524263443257, 1.29377201785872, 1.28532820919449, 1.28603197336908, 
1.29682121676658, 1.31652331755937, 1.30362316379412, 1.28321720328775, 
1.28274815455669, 1.30643764725844, 1.3017466822529, 1.33739806406454, 
1.33692891979448, 1.33857078143117, 1.37961696407719, 1.38196230327146, 
1.38668596650049, 1.39210627624845, 1.39587708123954, 1.49462299330477, 
1.46492861158566, 1.38409351567716, 1.32611853839275, 1.35840546647419, 
1.41803004405447, 1.39022095734953, 1.42698546407521, 1.40129741487224, 
1.4444252490666, 1.49933636220632, 1.47506248131317, 1.47647645854498, 
1.40224014597595, 1.39587708123954, 1.39045667595258, 1.35675577063933, 
1.37749475622244, 1.40354334573477, 1.41798841293428, 1.38081007902079, 
1.41041071309404, 1.3699169608432, 1.40472736058841, 1.43409110570583, 
1.43361759052643, 1.4613237100718, 1.46440223945332, 1.45161470228685, 
1.47790010910076, 1.50465910274833, 1.49495009496338, 1.49092434414505, 
1.47624238798972, 1.4667702691606, 1.43243357567279, 1.44451051762601, 
1.47197993929356, 1.48168904261751, 1.46955759566356, 1.51713148063145, 
1.53187950200677, 1.55233621760804, 1.5409184708849, 1.55186043337733, 
1.53758829177172, 1.51356350494192, 1.53949128538603, 1.56208879117797, 
1.58991944581264, 1.59420121727198, 1.56993853832683, 1.58302145820319, 
1.62940585870957, 1.6272652595969, 1.64248880247068, 1.73264119625531, 
1.76308864027411, 1.70837849342541, 1.69577154885776, 1.65592980252709, 
1.6774261018955, 1.63658303516805, 1.61628102109601, 1.62416303654286
)), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-301L), vars = "symbol", drop = TRUE, indices = list(200:300, 
    100:199, 0:99), group_sizes = c(101L, 100L, 100L), biggest_group_size = 101L, labels = structure(list(
    symbol = c("ABT", "PRAH", "ZTS")), class = "data.frame", row.names = c(NA, 
-3L), vars = "symbol", drop = TRUE))

1 个答案:

答案 0 :(得分:3)

您可以通过选择相关点并将其馈送到annotate来预先创建标签。

labels <- 
x %>% 
  group_by(symbol) %>% 
  top_n(1, adj.x) %>% 
  ungroup %>% 
  top_n(2, adj.x) %>% 
  with(annotate('label', label = symbol, x = date, y = adj.x - 1))


ggplot(x, aes(x=date, y=adj.x-1, color= symbol)) + 
  geom_line(size = 1) +
  labels

enter image description here

如果要标记所有高于1的行,可以使用

labels <- 
x %>% 
  group_by(symbol) %>% 
  filter(any(adj.x - 1 > 1)) %>% 
  top_n(1, adj.x) %>% 
  with(annotate('label', label = symbol, x = date, y = adj.x - 1))

您可能希望将y中的annotate值乘以例如1.05,因此它们不会覆盖所有行。

labels <- 
x %>% 
  group_by(symbol) %>% 
  top_n(1, adj.x) %>% 
  ungroup %>% 
  top_n(2, adj.x) %>% 
  with(annotate('label', label = symbol, x = date, y = 1.05*(adj.x - 1)))


ggplot(x, aes(x=date, y=adj.x-1, color= symbol)) + 
  geom_line(size = 1) +
  labels

enter image description here