如何在ggplot2上的多线图上仅向一行添加值标签

时间:2014-11-07 19:58:31

标签: r ggplot2

该图描绘了油价时间序列的两条线,以及一些区间的标准偏差线。

是否可以将值标签仅附加到标准偏差线?

使用的情节代码:

ggplot(data.final, aes(x=Date, y=Value, colour=Type)) +
geom_line() +
xlab("Year") +
ylab("Price (2013 U.S. Dollars)")

使用的数据是:

         Date      Value               Type

1   2014-09-30  96.990000     Montly Average
2   2014-08-31 101.850000     Montly Average
3   2014-07-31 105.710000     Montly Average
4   2014-06-30 108.010000     Montly Average
5   2014-05-31 105.510000     Montly Average
6   2014-04-30 104.780000     Montly Average
7   2014-03-31 104.150000     Montly Average
8   2014-02-28 104.940000     Montly Average
9   2014-01-31 104.190000     Montly Average
10  2013-12-31 107.940000     Montly Average
11  2013-11-30 105.850000     Montly Average
12  2013-10-31 106.390000     Montly Average
13  2013-09-30 108.400000     Montly Average
14  2013-08-31 106.660000     Montly Average
15  2013-07-31 103.360000     Montly Average
16  2013-06-30 100.330000     Montly Average
17  2013-05-31 100.300000     Montly Average
18  2013-04-30 101.660000     Montly Average
19  2013-03-31 105.480000     Montly Average
20  2013-02-28 111.220000     Montly Average
21  2013-01-31 107.540000     Montly Average
22  2012-12-31 105.670000     Montly Average
23  2012-11-30 107.100000     Montly Average
24  2012-10-31 108.680000     Montly Average
25  2012-09-30 110.960000     Montly Average
26  2012-08-31 108.390000     Montly Average
27  2012-07-31  99.220000     Montly Average
28  2012-06-30  94.240000     Montly Average
29  2012-05-31 107.450000     Montly Average
30  2012-04-30 117.380000     Montly Average
31  2012-03-31 122.280000     Montly Average
32  2012-02-29 116.150000     Montly Average
33  2012-01-31 109.540000     Montly Average
34  2011-12-31 106.210000     Montly Average
35  2011-11-30 108.520000     Montly Average
36  2011-10-31 103.880000     Montly Average
37  2011-09-30 106.000000     Montly Average
38  2011-08-31 104.960000     Montly Average
39  2011-07-31 109.980000     Montly Average
40  2011-06-30 107.520000     Montly Average
41  2011-05-31 108.840000     Montly Average
42  2011-04-30 115.760000     Montly Average
43  2011-03-31 108.580000     Montly Average
44  2011-02-28  99.870000     Montly Average
45  2011-01-31  92.190000     Montly Average
46  2010-12-31  89.180000     Montly Average
47  2010-11-30  83.740000     Montly Average
48  2010-10-31  80.340000     Montly Average
49  2010-09-30  75.290000     Montly Average
50  2010-08-31  74.350000     Montly Average
51  2010-07-31  72.650000     Montly Average
52  2010-06-30  73.980000     Montly Average
53  2010-05-31  76.620000     Montly Average
54  2010-04-30  82.980000     Montly Average
55  2010-03-31  77.370000     Montly Average
56  2010-02-28  73.550000     Montly Average
57  2010-01-31  76.640000     Montly Average
58  2009-12-31  75.490000     Montly Average
59  2009-11-30  77.630000     Montly Average
60  2009-10-31  73.280000     Montly Average
61  2009-09-30  67.910000     Montly Average
62  2009-08-31  71.320000     Montly Average
63  2009-07-31  64.970000     Montly Average
64  2009-06-30  69.210000     Montly Average
65  2009-05-31  57.400000     Montly Average
66  2009-04-30  50.180000     Montly Average
67  2009-03-31  45.580000     Montly Average
68  2009-02-28  43.140000     Montly Average
69  2009-01-31  44.970000     Montly Average
70  2008-12-31  41.000000     Montly Average
71  2008-11-30  51.380000     Montly Average
72  2008-10-31  68.620000     Montly Average
73  2008-09-30  95.970000     Montly Average
74  2008-08-31 113.210000     Montly Average
75  2008-07-31 131.220000     Montly Average
76  2008-06-30 127.590000     Montly Average
77  2008-05-31 118.950000     Montly Average
78  2008-04-30 103.470000     Montly Average
79  2008-03-31  96.780000     Montly Average
80  2008-02-29  89.960000     Montly Average
81  2008-01-31  87.170000     Montly Average
82  2007-12-31  85.750000     Montly Average
83  2007-11-30  86.730000     Montly Average
84  2007-10-31  77.140000     Montly Average
85  2007-09-30  73.250000     Montly Average
86  2007-08-31  67.210000     Montly Average
87  2007-07-31  69.460000     Montly Average
88  2007-06-30  65.760000     Montly Average
89  2007-05-31  64.540000     Montly Average
90  2007-04-30  63.840000     Montly Average
91  2007-03-31  59.050000     Montly Average
92  2007-02-28  55.680000     Montly Average
93  2007-01-31  52.010000     Montly Average
94  2006-12-31  58.670000     Montly Average
95  2006-11-30  56.820000     Montly Average
96  2006-10-31  56.500000     Montly Average
97  2006-09-30  59.770000     Montly Average
98  2006-08-31  68.780000     Montly Average
99  2006-07-31  69.050000     Montly Average
100 2006-06-30  65.080000     Montly Average
101 2006-05-31  64.910000     Montly Average
102 2006-04-30  64.060000     Montly Average
103 2006-03-31  57.650000     Montly Average
104 2006-02-28  57.580000     Montly Average
105 2006-01-31  58.310000     Montly Average
106 2005-12-31  53.130000     Montly Average
107 2005-11-30  51.310000     Montly Average
108 2005-10-31  53.670000     Montly Average
109 2005-09-30  56.540000     Montly Average
110 2005-08-31  56.630000     Montly Average
111 2005-07-31  52.850000     Montly Average
112 2005-06-30  50.980000     Montly Average
113 2005-05-31  45.000000     Montly Average
114 2005-04-30  47.100000     Montly Average
115 2005-03-31  45.580000     Montly Average
116 2005-02-28  39.820000     Montly Average
117 2005-01-31  37.810000     Montly Average
118 2004-12-31  34.260000     Montly Average
119 2004-11-30  34.880000     Montly Average
120 2004-10-31  37.580000     Montly Average
121 2004-09-30  35.480000     Montly Average
122 2004-08-31  38.300000     Montly Average
123 2004-07-31  34.480000     Montly Average
124 2004-06-30  33.410000     Montly Average
125 2004-05-31  34.470000     Montly Average
126 2004-04-30  31.070000     Montly Average
127 2004-03-31  30.460000     Montly Average
128 2004-02-29  28.400000     Montly Average
129 2004-01-31  28.680000     Montly Average
130 2003-12-31  27.860000     Montly Average
131 2003-11-30  27.500000     Montly Average
132 2003-10-31  27.130000     Montly Average
133 2003-09-30  25.270000     Montly Average
134 2003-08-31  27.620000     Montly Average
135 2003-07-31  26.630000     Montly Average
136 2003-06-30  25.470000     Montly Average
137 2003-05-31  24.250000     Montly Average
138 2003-04-30  23.380000     Montly Average
139 2003-03-31  27.420000     Montly Average
140 2003-02-28  30.200000     Montly Average
141 2003-01-31  28.070000     Montly Average
142 2002-12-31  25.710000     Montly Average
143 2002-11-30  23.320000     Montly Average
144 2002-10-31  26.180000     Montly Average
145 2002-09-30  26.830000     Montly Average
146 2002-08-31  25.320000     Montly Average
147 2002-07-31  24.570000     Montly Average
148 2002-06-30  23.830000     Montly Average
149 2002-05-31  24.590000     Montly Average
150 2002-04-30  24.410000     Montly Average
151 2002-03-31  22.850000     Montly Average
152 2002-02-28  18.910000     Montly Average
153 2002-01-31  18.290000     Montly Average
154 2001-12-31  17.640000     Montly Average
155 2001-11-30  17.530000     Montly Average
156 2001-10-31  19.540000     Montly Average
157 2001-09-30  23.960000     Montly Average
158 2001-08-31  24.310000     Montly Average
159 2001-07-31  23.430000     Montly Average
160 2001-06-30  25.610000     Montly Average
161 2001-05-31  25.580000     Montly Average
162 2001-04-30  24.060000     Montly Average
163 2001-03-31  23.430000     Montly Average
164 2001-02-28  24.740000     Montly Average
165 2001-01-31  22.690000     Montly Average
166 2000-12-31  22.160000     Montly Average
167 2000-11-30  30.100000     Montly Average
168 2000-10-31  30.220000     Montly Average
169 2000-09-30  29.740000     Montly Average
170 2000-08-31  27.050000     Montly Average
171 2000-07-31  26.010000     Montly Average
172 2000-06-30  27.320000     Montly Average
173 2000-05-31  25.510000     Montly Average
174 2000-04-30  22.030000     Montly Average
175 2000-03-31  25.080000     Montly Average
176 2000-02-29  24.620000     Montly Average
177 2000-01-31  23.180000     Montly Average
178 1999-12-31  23.530000     Montly Average
179 1999-11-30  23.060000     Montly Average
180 1999-10-31  21.500000     Montly Average
181 1999-09-30  21.480000     Montly Average
182 1999-08-31  19.290000     Montly Average
183 1999-07-31  17.660000     Montly Average
184 1999-06-30  15.400000     Montly Average
185 1999-05-31  15.380000     Montly Average
186 1999-04-30  14.810000     Montly Average
187 1999-03-31  12.080000     Montly Average
188 1999-02-28  10.050000     Montly Average
189 1999-01-31  10.670000     Montly Average
190 1998-12-31  10.120000     Montly Average
191 1998-11-30  11.760000     Montly Average
192 1998-10-31  12.810000     Montly Average
193 1998-09-30  13.070000     Montly Average
194 1998-08-31  12.180000     Montly Average
195 1998-07-31  12.000000     Montly Average
196 1998-06-30  11.680000     Montly Average
197 1998-05-31  12.630000     Montly Average
198 1998-04-30  11.470000     Montly Average
199 1998-03-31  11.530000     Montly Average
200 1998-02-28  12.430000     Montly Average
201 1998-01-31  13.400000     Montly Average
202 1997-12-31  16.310000     Montly Average
203 1997-11-30  18.380000     Montly Average
204 1997-10-31  19.060000     Montly Average
205 1997-09-30  17.950000     Montly Average
206 1997-08-31  17.770000     Montly Average
207 1997-07-31  17.290000     Montly Average
208 1997-06-30  17.340000     Montly Average
209 1997-05-31  18.450000     Montly Average
210 1997-04-30  16.770000     Montly Average
211 1997-03-31  18.070000     Montly Average
212 1997-02-28  18.590000     Montly Average
213 1997-01-31  21.240000     Montly Average
214 1996-12-31  21.640000     Montly Average
215 1996-11-30  20.870000     Montly Average
216 1996-10-31  21.720000     Montly Average
217 1996-09-30  20.350000     Montly Average
218 1996-08-31  18.680000     Montly Average
219 1996-07-31  17.790000     Montly Average
220 1996-06-30  17.000000     Montly Average
221 1996-05-31  16.990000     Montly Average
222 1996-04-30  17.770000     Montly Average
223 1996-03-31  17.090000     Montly Average
224 1996-02-29  15.990000     Montly Average
225 1996-01-31  16.550000     Montly Average
226 1995-12-31  16.810000     Montly Average
227 1995-11-30  15.560000     Montly Average
228 1995-10-31  14.990000     Montly Average
229 1995-09-30  15.430000     Montly Average
230 1995-08-31  15.380000     Montly Average
231 1995-07-31  15.020000     Montly Average
232 1995-06-30  16.240000     Montly Average
233 1995-05-31  17.240000     Montly Average
234 1995-04-30  17.650000     Montly Average
235 1995-03-31  16.490000     Montly Average
236 1995-02-28  16.640000     Montly Average
237 1995-01-31  16.080000     Montly Average
238 1994-12-31  15.340000     Montly Average
239 1994-11-30  16.230000     Montly Average
240 1994-10-31  15.300000     Montly Average
241 1994-09-30  15.080000     Montly Average
242 1994-08-31  15.730000     Montly Average
243 1994-07-31  16.840000     Montly Average
244 1994-06-30  15.880000     Montly Average
245 1994-05-31  15.150000     Montly Average
246 1994-04-30  13.900000     Montly Average
247 1994-03-31  12.530000     Montly Average
248 1994-02-28  12.730000     Montly Average
249 1994-01-31  13.270000     Montly Average
250 1993-12-31  12.030000     Montly Average
251 1993-11-30  13.490000     Montly Average
252 1993-10-31  14.590000     Montly Average
253 1993-09-30  13.900000     Montly Average
254 1993-08-31  14.570000     Montly Average
255 1993-07-31  14.490000     Montly Average
256 1993-06-30  15.400000     Montly Average
257 1993-05-31  16.070000     Montly Average
258 1993-04-30  16.430000     Montly Average
259 1993-03-31  16.440000     Montly Average
260 1993-02-28  16.160000     Montly Average
261 1993-01-31  15.300000     Montly Average
262 1992-12-31  16.220000     Montly Average
263 1992-11-30  17.210000     Montly Average
264 1992-10-31  18.160000     Montly Average
265 1992-09-30  18.360000     Montly Average
266 1992-08-31  17.780000     Montly Average
267 1992-07-31  18.440000     Montly Average
268 1992-06-30  18.980000     Montly Average
269 1992-05-31  17.570000     Montly Average
270 1992-04-30  16.670000     Montly Average
271 1992-03-31  15.560000     Montly Average
272 1992-02-29  15.460000     Montly Average
273 1992-01-31  15.250000     Montly Average
274 1991-12-31  15.110000     Montly Average
275 1991-11-30  17.760000     Montly Average
276 1991-10-31  19.120000     Montly Average
277 1991-09-30  17.500000     Montly Average
278 1991-08-31  16.470000     Montly Average
279 1991-07-31  16.130000     Montly Average
280 1991-06-30  15.030000     Montly Average
281 1991-05-31  15.770000     Montly Average
282 1991-04-30  15.550000     Montly Average
283 1991-03-31  15.650000     Montly Average
284 1991-02-28  15.510000     Montly Average
285 1991-01-31  19.680000     Montly Average
286 1990-12-31  24.180000     Montly Average
287 1990-11-30  28.980000     Montly Average
288 1990-10-31  32.340000     Montly Average
289 1990-09-30  32.030000     Montly Average
290 1990-08-31  25.060000     Montly Average
291 1990-07-31  14.880000     Montly Average
292 1990-06-30  13.050000     Montly Average
293 1990-05-31  14.030000     Montly Average
294 1990-04-30  14.050000     Montly Average
295 1990-03-31  15.590000     Montly Average
296 1990-02-28  17.000000     Montly Average
297 1990-01-31  17.570000     Montly Average
298 1989-12-31  17.120000     Montly Average
299 1989-11-30  16.040000     Montly Average
300 1989-10-31  16.140000     Montly Average
301 1989-09-30  15.710000     Montly Average
302 1989-08-31  14.910000     Montly Average
303 1989-07-31  15.540000     Montly Average
304 1989-06-30  15.600000     Montly Average
305 1989-05-31  15.860000     Montly Average
306 1989-04-30  17.110000     Montly Average
307 1989-03-31  16.130000     Montly Average
308 1989-02-28  14.840000     Montly Average
309 1989-01-31  14.370000     Montly Average
310 1988-12-31  12.570000     Montly Average
311 1988-11-30  10.550000     Montly Average
312 1988-10-31  10.630000     Montly Average
313 1988-09-30  11.770000     Montly Average
314 1988-08-31  13.190000     Montly Average
315 1988-07-31  12.980000     Montly Average
316 1988-06-30  14.410000     Montly Average
317 1988-05-31  15.260000     Montly Average
318 1988-04-30  14.730000     Montly Average
319 1988-03-31  13.590000     Montly Average
320 1988-02-29  15.200000     Montly Average
321 1988-01-31  15.440000     Montly Average
322 1987-12-31  15.610000     Montly Average
323 1987-11-30  16.740000     Montly Average
324 1987-10-31  17.170000     Montly Average
325 1987-09-30  17.010000     Montly Average
326 1987-08-31  17.300000     Montly Average
327 1987-07-31  17.670000     Montly Average
328 1987-06-30  17.200000     Montly Average
329 1987-05-31  17.040000     Montly Average
330 1987-04-30  17.040000     Montly Average
331 1987-03-31  16.930000     Montly Average
332 1987-02-28  16.740000     Montly Average
333 1987-01-31  17.120000     Montly Average
334 1986-12-31  14.500000     Montly Average
335 1986-11-30  13.700000     Montly Average
336 1986-10-31  13.280000     Montly Average
337 1986-09-30  12.920000     Montly Average
338 1986-08-31  11.710000     Montly Average
339 1986-07-31   8.500000     Montly Average
340 1986-06-30  10.380000     Montly Average
341 1986-05-31  11.020000     Montly Average
342 1986-04-30  10.330000     Montly Average
343 1986-03-31  11.970000     Montly Average
344 1986-02-28  16.430000     Montly Average
345 1986-01-31  21.930000     Montly Average
346 1985-12-31  26.390000     Montly Average
347 1985-11-30  26.920000     Montly Average
348 1985-10-31  26.740000     Montly Average
349 1985-09-30  26.580000     Montly Average
350 1985-08-31  26.290000     Montly Average
351 1985-07-31  25.730000     Montly Average
352 1985-06-30  25.430000     Montly Average
353 1985-05-31  25.680000     Montly Average
354 1985-04-30  26.710000     Montly Average
355 1985-03-31  27.130000     Montly Average
356 1985-02-28  27.200000     Montly Average
357 1985-01-31  27.300000     Montly Average
358 1984-12-31  26.720000     Montly Average
359 1984-11-30  27.100000     Montly Average
360 1984-10-31  27.260000     Montly Average
361 1984-09-30  27.430000     Montly Average
362 1984-08-31  27.140000     Montly Average
363 1984-07-31  27.260000     Montly Average
364 1984-06-30  28.000000     Montly Average
365 1984-05-31  28.010000     Montly Average
366 1984-04-30  27.980000     Montly Average
367 1984-03-31  27.830000     Montly Average
368 1984-02-29  27.630000     Montly Average
369 1984-01-31  27.750000     Montly Average
370 1983-12-31  27.540000     Montly Average
371 1983-11-30  27.810000     Montly Average
372 1983-10-31  28.130000     Montly Average
373 1983-09-30  28.390000     Montly Average
374 1983-08-31  28.610000     Montly Average
375 1983-07-31  28.620000     Montly Average
376 1983-06-30  28.160000     Montly Average
377 1983-05-31  28.500000     Montly Average
378 1983-04-30  28.700000     Montly Average
379 1983-03-31  28.100000     Montly Average
380 1983-02-28  29.000000     Montly Average
381 1983-01-31  30.050000     Montly Average
382 1982-12-31  30.100000     Montly Average
383 1982-11-30  31.750000     Montly Average
384 1982-10-31  33.400000     Montly Average
385 1982-09-30  33.050000     Montly Average
386 1982-08-31  31.200000     Montly Average
387 1982-07-31  31.700000     Montly Average
388 1982-06-30  32.650000     Montly Average
389 1982-05-31  33.350000     Montly Average
390 1982-04-30  30.880000     Montly Average
391 1982-03-31  28.600000     Montly Average
392 1982-02-28  30.400000     Montly Average
393 1982-01-31  34.000000     Montly Average
394 1981-12-31  34.120000     Montly Average
395 1981-11-30  34.240000     Montly Average
396 1981-10-31  32.680000     Montly Average
397 1981-09-30  32.060000     Montly Average
398 1981-08-31  32.170000     Montly Average
399 1981-07-31  31.960000     Montly Average
400 1981-06-30  32.170000     Montly Average
401 1981-05-31  33.380000     Montly Average
402 1981-04-30  35.550000     Montly Average
403 1981-03-31  36.850000     Montly Average
404 1981-02-28  37.100000     Montly Average
405 1981-01-31  39.250000     Montly Average
406 1980-12-31  39.350000     Montly Average
407 1980-11-30  39.750000     Montly Average
408 1980-10-31  36.800000     Montly Average
409 1980-09-30  32.250000     Montly Average
410 1980-08-31  32.300000     Montly Average
411 1980-07-31  33.350000     Montly Average
412 1980-06-30  36.000000     Montly Average
413 1980-05-31  35.600000     Montly Average
414 1980-04-30  35.000000     Montly Average
415 1980-03-31  35.750000     Montly Average
416 1980-02-29  36.000000     Montly Average
417 1980-01-31  38.000000     Montly Average
418 1980-01-01  35.845833     Yearly Average
419 1981-01-01  34.294167     Yearly Average
420 1982-01-01  31.756667     Yearly Average
421 1983-01-01  28.467500     Yearly Average
422 1984-01-01  27.509167     Yearly Average
423 1985-01-01  26.508333     Yearly Average
424 1986-01-01  13.055833     Yearly Average
425 1987-01-01  16.964167     Yearly Average
426 1988-01-01  13.360000     Yearly Average
427 1989-01-01  15.780833     Yearly Average
428 1990-01-01  20.730000     Yearly Average
429 1991-01-01  16.606667     Yearly Average
430 1992-01-01  17.138333     Yearly Average
431 1993-01-01  14.905833     Yearly Average
432 1994-01-01  14.831667     Yearly Average
433 1995-01-01  16.127500     Yearly Average
434 1996-01-01  18.536667     Yearly Average
435 1997-01-01  18.101667     Yearly Average
436 1998-01-01  12.090000     Yearly Average
437 1999-01-01  17.075833     Yearly Average
438 2000-01-01  26.085000     Yearly Average
439 2001-01-01  22.710000     Yearly Average
440 2002-01-01  23.734167     Yearly Average
441 2003-01-01  26.733333     Yearly Average
442 2004-01-01  33.455833     Yearly Average
443 2005-01-01  49.201667     Yearly Average
444 2006-01-01  61.431667     Yearly Average
445 2007-01-01  68.368333     Yearly Average
446 2008-01-01  93.776667     Yearly Average
447 2009-01-01  61.756667     Yearly Average
448 2010-01-01  78.057500     Yearly Average
449 2011-01-01 106.025833     Yearly Average
450 2012-01-01 108.921667     Yearly Average
451 2013-01-01 105.427500     Yearly Average
452 2014-01-01 104.014444     Yearly Average
453 1980-01-31   3.650447 Standard Deviation
454 1984-12-31   3.650447 Standard Deviation
455 1985-01-31   5.256374 Standard Deviation
456 1989-12-31   5.256374 Standard Deviation
457 1990-01-31   4.027496 Standard Deviation
458 1994-12-31   4.027496 Standard Deviation
459 1995-01-31   3.313954 Standard Deviation
460 1999-12-31   3.313954 Standard Deviation
461 2000-01-31   4.619182 Standard Deviation
462 2004-12-31   4.619182 Standard Deviation
463 2005-01-31  20.926305 Standard Deviation
464 2009-12-31  20.926305 Standard Deviation
465 2010-01-31  12.778173 Standard Deviation
466 2014-09-30  12.778173 Standard Deviation

1 个答案:

答案 0 :(得分:2)

喜欢这个??

df.labels <- aggregate(Date~Value,
                       data.final[data.final$Type=="Standard Deviation",],
                       mean)
ggplot(data.final, aes(x=Date, y=Value)) +
  geom_line(aes(color=Type)) +
  geom_text(data=df.labels,aes(y=Value+5, label=round(Value,2)),size=3) + 
  xlab("Year") +
  ylab("Price (2013 U.S. Dollars)")