【发布时间】:2018-01-21 19:13:38
【问题描述】:
我有一个工作脚本,它使用循环生成许多 xts 类数据的 png 文件图。现在脚本抛出一个错误,如果我注释掉抛出错误的行(调用 abline() )然后脚本执行但不生成 png 文件。该问题似乎涉及绘制 xts 类数据和/或循环或脚本。
在 stackoverflow 上搜索并没有提供对此问题的解决方案或参考。我在以下示例中重现了该问题。在实践中,脚本会在循环和重要数据中使用不同的文件名。
# put following code in 'myscript.R' and execute using source('myscript.R',print.eval=TRUE) or source('myscript.R')
# xts class data
data <- xts(seq(1:10),order.by=as.Date(seq(1:10)))
# a non xts version of same data
#data <- seq(1:10)
for(i in 1:1) {
filename <- 'myfile.png'
png(filename)
plot(data)
lines( (data-1),col='red')
abline(h=1)
dev.off()
}
# The call to abline in above script with xts class data gives error 'plot.new has not been called yet'
# If comment out the call to abline it completes but doesn't produce a png file
# script works fine with abline for non xts data
【问题讨论】: