R中的Dygraphs问题

时间:2015-12-11 06:35:55

标签: javascript r dygraphs

我正在尝试在R dygraphs中绘制以下时间序列data.frame。

str(data.frame)
'data.frame':   1440 obs. of  2 variables:
 $ Tm   : POSIXct, format: "2015-12-08 08:00:00" "2015-12-08 08:01:00" "2015-12-08 08:02:00" "2015-12-08 08:03:00" ...
 $ ydata: num  84.2 84.2 84.3 84.6 84.4 ...

下面提到了一个样本数据表。

445  2015-12-08 15:24:00 91.984
446  2015-12-08 15:25:00 92.330
447  2015-12-08 15:26:00 91.982
448  2015-12-08 15:27:00 92.022
449  2015-12-08 15:28:00 91.829
450  2015-12-08 15:29:00 91.984
451  2015-12-08 15:30:00 91.638
452  2015-12-08 15:31:00 91.443
453  2015-12-08 15:32:00 91.368
454  2015-12-08 15:33:00 91.637
455  2015-12-08 15:34:00 91.405
456  2015-12-08 15:35:00 91.478
457  2015-12-08 15:36:00 91.367
458  2015-12-08 15:37:00 91.328
459  2015-12-08 15:38:00 90.595
460  2015-12-08 15:39:00 90.285
461  2015-12-08 15:40:00 90.326
462  2015-12-08 15:41:00 90.364
463  2015-12-08 15:42:00 90.365
464  2015-12-08 15:43:00 90.673
465  2015-12-08 15:44:00 89.979
466  2015-12-08 15:45:00 90.403
467  2015-12-08 15:46:00 90.481
468  2015-12-08 15:47:00 90.326
469  2015-12-08 15:48:00 90.404
470  2015-12-08 15:49:00 90.250
471  2015-12-08 15:50:00 90.365
472  2015-12-08 15:51:00 90.789
473  2015-12-08 15:52:00 90.983
474  2015-12-08 15:53:00 91.253
475  2015-12-08 15:54:00 91.176
476  2015-12-08 15:55:00 91.253
477  2015-12-08 15:56:00 91.445
478  2015-12-08 15:57:00 91.176
479  2015-12-08 15:58:00 91.435
480  2015-12-08 15:59:00 91.368
481  2015-12-08 16:00:00 90.906
482  2015-12-08 16:01:00 91.251
483  2015-12-08 16:02:00 90.944
484  2015-12-08 16:03:00 90.596
485  2015-12-08 16:04:00 91.484
486  2015-12-08 16:05:00 91.368
487  2015-12-08 16:06:00 91.483
488  2015-12-08 16:07:00 91.330
489  2015-12-08 16:08:00 91.522
490  2015-12-08 16:09:00 91.907
491  2015-12-08 16:10:00 91.831
492  2015-12-08 16:11:00 91.598
493  2015-12-08 16:12:00 91.676
494  2015-12-08 16:13:00 91.869
495  2015-12-08 16:14:00 91.945
496  2015-12-08 16:15:00 91.793
497  2015-12-08 16:16:00 92.293
498  2015-12-08 16:17:00 92.754
499  2015-12-08 16:18:00 93.139
500  2015-12-08 16:19:00 93.023
501  2015-12-08 16:20:00 92.946
502  2015-12-08 16:21:00 92.947
503  2015-12-08 16:22:00 93.063
504  2015-12-08 16:23:00 93.178
505  2015-12-08 16:24:00 93.293
506  2015-12-08 16:25:00 93.178
507  2015-12-08 16:26:00 93.101
508  2015-12-08 16:27:00 92.638
509  2015-12-08 16:28:00 92.445
510  2015-12-08 16:29:00 92.987
511  2015-12-08 16:30:00 93.102
512  2015-12-08 16:31:00 93.410
513  2015-12-08 16:32:00 93.293
514  2015-12-08 16:33:00 93.795
515  2015-12-08 16:34:00 93.642
516  2015-12-08 16:35:00 93.564
517  2015-12-08 16:36:00 93.140
518  2015-12-08 16:37:00 93.024
519  2015-12-08 16:38:00 93.064
520  2015-12-08 16:39:00 93.025
521  2015-12-08 16:40:00 92.985
522  2015-12-08 16:41:00 91.677
523  2015-12-08 16:42:00 91.908
524  2015-12-08 16:43:00 92.099
525  2015-12-08 16:44:00 91.984
526  2015-12-08 16:45:00 91.638
527  2015-12-08 16:46:00 91.408
528  2015-12-08 16:47:00 90.789
529  2015-12-08 16:48:00 90.520
530  2015-12-08 16:49:00 91.523
531  2015-12-08 16:50:00 90.790
532  2015-12-08 16:51:00 89.786
533  2015-12-08 16:52:00 89.633
534  2015-12-08 16:53:00 89.518
535  2015-12-08 16:54:00 90.288
536  2015-12-08 16:55:00 90.441
537  2015-12-08 16:56:00 89.864
538  2015-12-08 16:57:00 89.595
539  2015-12-08 16:58:00 89.480
540  2015-12-08 16:59:00 89.632

我可以使用ggplot成功获得输出但是当我尝试使用dygraph时,我收到以下错误。

dygraph(data.frame)
Error in as.POSIXlt.character(x, tz, ...) : 
  character string is not in a standard unambiguous format

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

请参阅下面的代码。但是,一个重要的建议是不要使用内置函数名作为对象名。在运行以下代码之前,还要确保您的Tm列是一个字符,而不是一个因素。

require(xts)
stopifnot(class(dta$Tm) == "character") # make sure it's not a factor!
dta$Tm <- as.POSIXct(dta$Tm, format = "%d/%m/%Y %H:%M:%S")
dta <- xts(dta$ydata, order.by = dta$Tm)
dygraph(dta)