【问题标题】:Plotting and saving a list of time series in R在 R 中绘制和保存时间序列列表
【发布时间】:2017-04-22 16:57:34
【问题描述】:

您好,我有一个包含 67 个时间序列对象的列表,我想绘制这些对象,然后将这些图保存为不同名称的 jpeg 文件。这是此列表的预览:

$ SE_VALUES_IMV_IN          :List of 3
  ..$ 220830  : num [1:84] 0 0 0 0 0 0 0 0 0 0 ...
  ..$ 22083030: num [1:84] 0 0 0 0 0 0 0 0 0 0 ...
  ..$ 22083082: num [1:84] 0 0 0 0 0 0 0 0 0 0 ...
  ..- attr(*, "tsp")= num [1:3] 2010 2010 12
  ..- attr(*, "class")= chr "ts"
 $ SI_VALUES_IMV_EU28_EXTRA  :List of 8
  ..$ 220830  : num [1:84] 235549 117953 163342 214415 248162 ...
  ..$ 22083011: num [1:84] 8026 0 0 6166 0 ...
  ..$ 22083030: num [1:84] 0 40 0 0 0 0 0 0 0 0 ...
  ..$ 22083041: num [1:84] 0 0 0 0 0 0 0 0 0 0 ...
  ..$ 22083071: num [1:84] 0 0 0 0 55910 ...
  ..$ 22083079: num [1:84] 0 0 0 0 0 0 0 0 0 0 ...
  ..$ 22083082: num [1:84] 227523 117913 163342 207068 192252 ...
  ..$ 22083088: num [1:84] 0 0 0 1181 0 ...
  ..- attr(*, "tsp")= num [1:3] 2010 2011 12
  ..- attr(*, "class")= chr "ts"
 $ SI_VALUES_IMV_EU28_INTRA  :List of 12
  ..$ 220830  : num [1:84] 218674 255317 327377 363219 335267 ...
  ..$ 22083011: num [1:84] 9363 10129 19672 20631 10403 ...
  ..$ 22083019: num [1:84] 0 1978 0 0 7116 ...
  ..$ 22083030: num [1:84] 3837 15684 14588 20487 30870 ...
  ..$ 22083041: num [1:84] 18979 5123 7176 36842 9390 ...
  ..$ 22083049: num [1:84] 688 0 0 0 0 0 0 0 0 0 ...
  ..$ 22083061: num [1:84] 0 0 3452 4225 96 ...
  ..$ 22083069: num [1:84] 0 0 0 40 0 0 7520 0 0 0 ...
  ..$ 22083071: num [1:84] 139915 204803 256095 218105 185088 ...
  ..$ 22083079: num [1:84] 0 3219 0 0 3381 ...
  ..$ 22083082: num [1:84] 45892 14381 26394 62889 88527 ...
  ..$ 22083088: num [1:84] 0 0 0 0 396 0 0 0 642 105 ...
  ..- attr(*, "tsp")= num [1:3] 2010 2011 12
  ..- attr(*, "class")= chr "ts"

所以我编写了一个函数来通过将lapply() 应用到我的列表来绘制(使用ts.plot())这些元素:

fun2 <- function(x){
  mypath <- file.path("C:","Users","Documents", "FI-R", "graphs", paste("multiplot-", x ,".jpeg", sep = ""))
  jpeg(file=mypath, width = 10, height = 10, units = 'in', res = 400)
  ts.plot(x, col = 1:ncol(x), ylab="Import Value", main=deparse(substitute(x))) 
  legend("topleft", colnames(x), col = 1:ncol(x), lty = 1)
  dev.off()}


Plot.Ts.whisk.lst <- lapply(Ts.whisk.lst.2, fun2)

这当然行不通,但它显示了我的野心。我试图将问题简化为仅运行以下代码:

Plot.Ts.whisk.lst <- lapply(Ts.whisk.lst.2, function(x) ts.plot(x) )

但我得到了错误:

Error in xy.coords(x, NULL, log = log) : 
  (list) object cannot be coerced to type 'double'

我知道我的时间序列列表有问题,但不明白它是什么。任何帮助表示赞赏。

谢谢

【问题讨论】:

    标签: r list plot time-series lapply


    【解决方案1】:

    我在单个 ts(双重强制)上遇到了同样的问题,使用 using 解决了这个问题。不确定它是否适用于您的。

    用 2 个 ts 试试,只用这些来运行你的代码。

    ts$Value <- as.numeric(as.character(ts$Value))
    

    您是否尝试绘制单个 ts?

    【讨论】:

      猜你喜欢
      • 2021-11-28
      • 2011-12-09
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 2020-08-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多