我无法将geom_vline添加到我的ggplot 2图表中。我正在x轴上绘制日期。 as.Date格式。然后在Y轴上绘制数值。
在同一个情节上。我从另一个数据框中提取了匹配的日期。它们与我上面的情节具有相同的日期格式。
添加geom_vline时的问题是它会抛出错误:
Error: ggplot2 doesn't know how to deal with data of class numeric
如果我想要使用的两个日期都是日期格式,我不知道为什么它会抛出一个数字错误日期。我唯一的数字格式是Y轴上的数据。
见下面的代码:
p1 <- ggplot(data=plotdataframe, aes(x=ddate, y=SP500close1, colour=SP500close1)) +
geom_vline(data=matches.df,aes(xintercept=as.numeric(ddatesmatches)))+
geom_line() +
theme_bw() +
labs(title="S&P500 Close",y="S&P500 Close",x="Date")+
theme(plot.title = element_text(hjust = 0.5)) +
theme(plot.subtitle = element_text(hjust = 0.5))
grid.arrange(p1)
以下是制作此图的所有数据的格式:
> str(matches.df)
'data.frame': 48 obs. of 1 variable:
$ ddatesmatches: Date, format: "1929-10-28" "1929-11-11" "1929-11-20" "1931-06-08" ...
> str(ddate)
Date[1:23539], format: "1928-01-03" "1928-01-04" "1928-01-05" "1928-01-06" "1928-01-07" "1928-01-09" "1928-01-10" "1928-01-11" ...
> str(SP500close1)
num [1:23539] 17.8 17.7 17.6 17.7 17.7 ...
正如您所看到的......两个日期都是日期格式,而我的Y轴(SP500close1)是一个数值。
似乎可以添加到此图表中。由于我的格式是他们需要的地方,任何人都可以对此有所了解吗?
注意 - 它们来自两个不同的数据源,因此我在geom_vline中指定(data = matches.df ....以便读取数据以绘制来自该特定数据帧的vlines。
任何见解都表示赞赏。
由于
这是plotdataframe的数据
ddate SP500close1
1 1928-01-03 17.76
2 1928-01-04 17.72
3 1928-01-05 17.55
4 1928-01-06 17.66
5 1928-01-07 17.68
6 1928-01-09 17.50
7 1928-01-10 17.37
8 1928-01-11 17.35
9 1928-01-12 17.47
10 1928-01-13 17.58
11 1928-01-14 17.44
12 1928-01-16 17.29
13 1928-01-17 17.30
14 1928-01-18 17.26
15 1928-01-19 17.38
16 1928-01-20 17.48
17 1928-01-21 17.53
18 1928-01-23 17.64
19 1928-01-24 17.71
20 1928-01-25 17.52
21 1928-01-26 17.63
22 1928-01-27 17.69
23 1928-01-28 17.58
24 1928-01-30 17.49
25 1928-01-31 17.57
26 1928-02-01 17.53
27 1928-02-02 17.63
28 1928-02-03 17.40
29 1928-02-04 17.43
30 1928-02-06 17.45
31 1928-02-07 17.44
32 1928-02-08 17.49
33 1928-02-09 17.55
34 1928-02-10 17.54
35 1928-02-11 17.53
36 1928-02-14 17.44
37 1928-02-15 17.40
38 1928-02-16 17.35
39 1928-02-17 17.04
40 1928-02-18 16.99
41 1928-02-20 16.95
42 1928-02-21 17.11
43 1928-02-23 17.13
44 1928-02-24 17.18
45 1928-02-25 17.19
46 1928-02-27 17.11
47 1928-02-28 17.16
48 1928-02-29 17.26
49 1928-03-01 17.30
50 1928-03-02 17.30
51 1928-03-03 17.42
52 1928-03-05 17.57
53 1928-03-06 17.67
54 1928-03-07 17.58
55 1928-03-08 17.64
56 1928-03-09 17.93
57 1928-03-10 17.87
58 1928-03-12 18.00
59 1928-03-13 17.92
60 1928-03-14 17.93
61 1928-03-15 18.07
62 1928-03-16 18.26
63 1928-03-17 18.32
64 1928-03-19 18.36
65 1928-03-20 18.46
66 1928-03-21 18.65
67 1928-03-22 18.60
68 1928-03-23 18.78
69 1928-03-24 18.79
70 1928-03-26 19.05
71 1928-03-27 18.94
72 1928-03-28 18.89
73 1928-03-29 18.95
74 1928-03-30 19.28
75 1928-03-31 19.13
76 1928-04-02 18.91
77 1928-04-03 19.02
78 1928-04-04 18.99
79 1928-04-05 19.23
80 1928-04-09 19.27
81 1928-04-10 19.10
82 1928-04-11 19.44
83 1928-04-12 19.41
84 1928-04-13 19.73
85 1928-04-14 19.59
86 1928-04-16 19.68
87 1928-04-17 19.52
88 1928-04-18 19.52
89 1928-04-19 19.63
90 1928-04-20 19.34
91 1928-04-23 19.15
92 1928-04-24 19.25
93 1928-04-25 19.33
94 1928-04-26 19.47
95 1928-04-27 19.68
96 1928-04-28 19.75
97 1928-04-30 19.75
98 1928-05-01 19.78
99 1928-05-02 19.83
100 1928-05-03 19.93
101 1928-05-04 20.24
102 1928-05-07 20.41
103 1928-05-08 20.27
104 1928-05-09 20.24
105 1928-05-10 20.21
106 1928-05-11 20.36
107 1928-05-14 20.44
108 1928-05-15 20.39
109 1928-05-16 20.05
110 1928-05-17 20.12
111 1928-05-18 19.92
112 1928-05-21 19.63
113 1928-05-22 19.36
114 1928-05-23 19.75
115 1928-05-24 19.87
116 1928-05-25 19.90
117 1928-05-28 19.57
118 1928-05-29 19.82
119 1928-05-31 20.00
120 1928-06-01 20.07
121 1928-06-02 20.22
122 1928-06-04 19.80
123 1928-06-05 19.85
124 1928-06-06 19.66
125 1928-06-07 19.61
126 1928-06-08 19.37
127 1928-06-09 19.12
128 1928-06-11 18.68
129 1928-06-12 18.34
130 1928-06-13 18.89
131 1928-06-14 19.05
132 1928-06-15 18.74
133 1928-06-16 18.75
134 1928-06-18 18.40
135 1928-06-19 18.34
136 1928-06-20 18.57
137 1928-06-21 18.56
138 1928-06-22 18.54
139 1928-06-23 18.46
140 1928-06-25 18.47
141 1928-06-26 18.68
142 1928-06-27 18.86
143 1928-06-28 19.07
144 1928-06-29 19.14
145 1928-06-30 19.19
146 1928-07-02 18.98
147 1928-07-03 19.29
148 1928-07-05 19.48
149 1928-07-06 19.39
150 1928-07-07 19.44
151 1928-07-09 19.46
152 1928-07-10 19.43
153 1928-07-11 18.95
154 1928-07-12 18.81
155 1928-07-13 18.96
156 1928-07-14 18.93
157 1928-07-16 18.70
158 1928-07-17 18.80
159 1928-07-18 19.03
160 1928-07-19 18.99
161 1928-07-20 19.03
162 1928-07-21 18.96
163 1928-07-23 19.12
164 1928-07-24 19.09
165 1928-07-25 19.20
166 1928-07-26 19.24
167 1928-07-27 19.42
168 1928-07-28 19.47
169 1928-07-30 19.47
170 1928-07-31 19.43
171 1928-08-01 19.42
172 1928-08-02 19.33
173 1928-08-03 19.36
174 1928-08-04 19.41
175 1928-08-06 19.58
176 1928-08-07 19.49
177 1928-08-08 19.25
178 1928-08-09 19.30
179 1928-08-10 19.25
180 1928-08-11 19.30
181 1928-08-13 19.29
182 1928-08-14 19.13
183 1928-08-15 19.47
184 1928-08-16 19.61
185 1928-08-17 19.64
186 1928-08-18 19.71
187 1928-08-20 19.77
188 1928-08-21 20.02
189 1928-08-22 20.00
190 1928-08-23 20.02
191 1928-08-24 20.34
192 1928-08-25 20.45
193 1928-08-27 20.38
194 1928-08-28 20.54
195 1928-08-29 20.58
196 1928-08-30 20.61
197 1928-08-31 20.87
198 1928-09-01 20.87
199 1928-09-04 20.97
200 1928-09-05 21.07
201 1928-09-06 20.94
202 1928-09-07 21.09
203 1928-09-08 21.03
204 1928-09-10 20.92
205 1928-09-11 21.08
206 1928-09-12 21.16
207 1928-09-13 21.14
208 1928-09-14 21.14
209 1928-09-15 21.23
210 1928-09-17 21.33
211 1928-09-18 21.18
212 1928-09-19 21.18
213 1928-09-20 21.22
214 1928-09-21 21.36
215 1928-09-22 21.24
216 1928-09-24 21.43
217 1928-09-25 21.41
218 1928-09-26 21.40
219 1928-09-27 21.13
220 1928-09-28 21.14
221 1928-09-29 21.37
222 1928-10-01 21.36
223 1928-10-02 21.26
224 1928-10-03 21.19
225 1928-10-04 21.26
226 1928-10-05 21.22
227 1928-10-06 21.17
228 1928-10-08 21.11
229 1928-10-09 21.18
230 1928-10-10 21.41
231 1928-10-11 21.51
232 1928-10-13 21.63
233 1928-10-15 21.65
234 1928-10-16 21.72
235 1928-10-17 21.79
236 1928-10-18 21.84
237 1928-10-19 21.96
238 1928-10-20 21.84
239 1928-10-22 21.81
240 1928-10-23 21.95
241 1928-10-24 21.99
242 1928-10-25 21.91
243 1928-10-26 21.65
244 1928-10-27 21.87
245 1928-10-29 22.00
246 1928-10-30 21.76
247 1928-10-31 21.68
248 1928-11-01 21.99
249 1928-11-02 21.95
250 1928-11-03 21.97
251 1928-11-05 22.24
252 1928-11-07 22.50
253 1928-11-08 22.39
254 1928-11-09 22.55
255 1928-11-10 22.73
256 1928-11-12 22.84
257 1928-11-13 22.91
258 1928-11-14 22.91
259 1928-11-15 23.03
260 1928-11-16 23.37
261 1928-11-17 23.37
262 1928-11-19 23.37
263 1928-11-20 23.57
264 1928-11-21 23.22
265 1928-11-22 23.52
266 1928-11-23 23.70
267 1928-11-26 23.80
268 1928-11-27 23.91
269 1928-11-28 24.19
270 1928-11-30 24.28
271 1928-12-01 24.03
272 1928-12-03 23.85
273 1928-12-04 23.91
274 1928-12-05 23.81
275 1928-12-06 22.91
276 1928-12-07 22.36
277 1928-12-08 21.92
278 1928-12-10 22.31
279 1928-12-11 22.74
280 1928-12-12 22.52
281 1928-12-13 22.48
282 1928-12-14 22.53
283 1928-12-15 22.52
284 1928-12-17 22.44
285 1928-12-18 22.70
286 1928-12-19 22.98
287 1928-12-20 23.07
288 1928-12-21 23.33
289 1928-12-22 23.33
290 1928-12-24 23.54
291 1928-12-26 23.44
292 1928-12-27 23.64
293 1928-12-28 23.98
294 1928-12-29 24.05
295 1928-12-31 24.35
296 1929-01-02 24.81
297 1929-01-03 24.86
298 1929-01-04 24.85
299 1929-01-05 24.57
300 1929-01-07 24.25
301 1929-01-08 24.17
302 1929-01-09 24.54
303 1929-01-10 24.58
304 1929-01-11 24.54
305 1929-01-12 24.47
306 1929-01-14 24.59
307 1929-01-15 24.23
308 1929-01-16 24.56
309 1929-01-17 24.69
310 1929-01-18 24.77
311 1929-01-19 24.88
312 1929-01-21 24.86
313 1929-01-22 25.07
314 1929-01-23 25.22
315 1929-01-24 25.06
316 1929-01-25 25.48
317 1929-01-26 25.43
318 1929-01-28 25.35
319 1929-01-29 25.36
320 1929-01-30 25.45
321 1929-01-31 25.74
322 1929-02-01 25.84
323 1929-02-02 25.80
324 1929-02-04 25.64
325 1929-02-05 25.70
326 1929-02-06 25.49
327 1929-02-07 24.71
328 1929-02-08 24.44
329 1929-02-11 24.98
330 1929-02-13 24.95
331 1929-02-14 24.85
332 1929-02-15 24.35
333 1929-02-16 24.05
334 1929-02-18 24.39
335 1929-02-19 24.38
336 1929-02-20 24.59
337 1929-02-21 24.86
338 1929-02-25 24.91
339 1929-02-26 24.95
340 1929-02-27 25.25
341 1929-02-28 25.59
342 1929-03-01 25.83
343 1929-03-02 25.77
344 1929-03-04 25.49
345 1929-03-05 25.32
346 1929-03-06 24.92
347 1929-03-07 25.15
348 1929-03-08 25.31
349 1929-03-09 25.31
350 1929-03-11 24.95
351 1929-03-12 24.97
352 1929-03-13 25.24
353 1929-03-14 25.73
354 1929-03-15 26.08
355 1929-03-16 26.13
356 1929-03-18 26.04
357 1929-03-19 26.03
358 1929-03-20 26.02
359 1929-03-21 25.90
360 1929-03-22 25.55
361 1929-03-23 25.21
362 1929-03-25 24.51
363 1929-03-26 24.35
364 1929-03-27 25.09
365 1929-03-28 25.53
366 1929-04-01 24.88
367 1929-04-02 25.17
368 1929-04-03 24.86
369 1929-04-04 25.30
370 1929-04-05 25.05
371 1929-04-06 25.07
372 1929-04-08 24.83
373 1929-04-09 24.63
374 1929-04-10 24.78
375 1929-04-11 25.06
376 1929-04-12 25.15
377 1929-04-13 25.17
378 1929-04-15 25.00
379 1929-04-16 25.06
380 1929-04-17 25.32
381 1929-04-18 25.43
382 1929-04-19 25.41
383 1929-04-20 25.45
384 1929-04-22 25.70
385 1929-04-23 25.85
386 1929-04-24 25.75
387 1929-04-25 25.63
388 1929-04-26 25.58
389 1929-04-27 25.65
390 1929-04-29 25.53
391 1929-04-30 25.94
392 1929-05-01 25.99
393 1929-05-02 26.11
394 1929-05-03 26.37
395 1929-05-04 26.48
396 1929-05-06 26.32
397 1929-05-07 26.07
398 1929-05-08 26.18
399 1929-05-09 26.02
400 1929-05-10 26.34
401 1929-05-11 26.24
402 1929-05-13 25.64
403 1929-05-14 25.95
404 1929-05-15 25.84
405 1929-05-16 25.97
406 1929-05-17 26.09
407 1929-05-18 26.05
408 1929-05-20 25.39
409 1929-05-21 25.70
410 1929-05-22 24.76
411 1929-05-23 25.31
412 1929-05-24 25.12
413 1929-05-25 25.03
414 1929-05-27 24.20
415 1929-05-28 24.59
416 1929-05-29 24.62
417 1929-05-31 24.83
418 1929-06-01 24.92
419 1929-06-03 25.27
420 1929-06-04 25.64
421 1929-06-05 25.59
422 1929-06-06 25.64
423 1929-06-07 25.53
424 1929-06-08 25.39
425 1929-06-10 25.27
426 1929-06-11 25.43
427 1929-06-12 25.45
428 1929-06-13 25.84
429 1929-06-14 25.93
430 1929-06-15 26.05
431 1929-06-17 26.41
432 1929-06-18 26.42
433 1929-06-19 26.32
434 1929-06-20 26.47
435 1929-06-21 26.62
436 1929-06-22 26.72
437 1929-06-24 26.62
438 1929-06-25 26.90
439 1929-06-26 27.06
440 1929-06-27 27.11
441 1929-06-28 27.41
442 1929-06-29 27.62
443 1929-07-01 27.77
444 1929-07-02 28.06
445 1929-07-03 28.18
446 1929-07-05 28.32
447 1929-07-06 28.36
448 1929-07-08 28.37
449 1929-07-09 28.28
450 1929-07-10 28.20
451 1929-07-11 28.26
452 1929-07-12 28.57
453 1929-07-13 28.64
454 1929-07-15 28.45
455 1929-07-16 28.65
456 1929-07-17 28.72
457 1929-07-18 28.66
458 1929-07-19 28.76
459 1929-07-20 28.81
460 1929-07-22 28.43
461 1929-07-23 28.83
462 1929-07-24 28.64
463 1929-07-25 28.77
464 1929-07-26 28.66
465 1929-07-27 28.55
466 1929-07-29 28.18
467 1929-07-30 28.50
468 1929-07-31 28.88
469 1929-08-01 29.08
470 1929-08-02 29.56
471 1929-08-03 29.81
472 1929-08-05 29.54
473 1929-08-06 29.43
474 1929-08-07 29.13
475 1929-08-08 29.47
476 1929-08-09 28.38
477 1929-08-10 28.83
478 1929-08-12 29.27
479 1929-08-13 29.53
480 1929-08-14 29.62
481 1929-08-15 29.67
482 1929-08-16 30.22
483 1929-08-17 30.24
484 1929-08-19 30.50
485 1929-08-20 30.68
486 1929-08-21 30.55
487 1929-08-22 30.79
488 1929-08-23 31.09
489 1929-08-24 31.15
490 1929-08-26 31.07
491 1929-08-27 31.05
492 1929-08-28 30.99
493 1929-08-29 31.29
494 1929-08-30 31.71
495 1929-09-03 31.82
496 1929-09-04 31.72
497 1929-09-05 31.02
498 1929-09-06 31.83
499 1929-09-07 31.92
500 1929-09-09 31.67
对于matches.df
ddatesmatches
1 1929-10-28
2 1929-11-11
3 1929-11-20
答案 0 :(得分:1)
通过在代码中添加:scale_x_date()+来解决此问题
p1 <- ggplot(data=plotdataframe, aes(x=ddate, y=SP500close1, colour=SP500close1)) +
scale_x_date()+
geom_vline(data=matches.df,aes(xintercept=as.numeric(ddatesmatches)))+
geom_line() +
theme_bw() +
labs(title="S&P500 Close",y="S&P500 Close",x="Date")+
theme(plot.title = element_text(hjust = 0.5)) +
theme(plot.subtitle = element_text(hjust = 0.5))
grid.arrange(p1)