简体   繁体   English

错误:尚未调用 plot.new

[英]Error: plot.new has not been called yet

The following code produces an image.以下代码生成图像。 No problem.没问题。

change <- function(score, d, k, p) {k*(score - 1/(1+k^(d/p)))}
parameters <- c(10:110)
colorshelf <-rainbow(length(parameters), start=1/6) #yellow is low
for(i in seq_along(parameters)) {
    curve(change(score=1, d=x, k=parameters[i], p=-800), from=-500, to=500, add=T, ylim=c(0, 100), col=colorshelf[i], xlab="rating difference", ylab="gain for winning")
}
legend.index <- round(quantile(seq_along(parameters)))
legend.param <- legend.index + min(parameters)
legend.color <- colorshelf[legend.index]
legend("right", title="k-factor", lty=c(1,1), legend=legend.param, col=legend.color)

Now I would like to save the image to a file with specified resolution.现在我想将图像保存到具有指定分辨率的文件中。 So I add:所以我补充说:

png(filename="gain by ratingdiff.png", res=30, width = 1000, height = 1000)

and

dev.off()

before and after the code block.在代码块之前和之后。 But then I get two errors, complaining about plot.new has not been called yet .但是后来我收到两个错误,抱怨plot.new has not been called yet

I know this issue came up like a million times.我知道这个问题出现了一百万次。 And there are so many posts about this here on stackoverflow.在stackoverflow上有很多关于这个的帖子。 But none of these really helped me out.但这些都没有真正帮助我。 I tried adding plot.new() at different places in the code.我尝试在代码的不同位置添加 plot.new() 。 But that did not help.但这并没有帮助。

The help page on plot.new() reads: "This function (frame is an alias for plot.new) causes the completion of plotting in the current plot (if there is one) and an advance to a new graphics frame. This is used in all high-level plotting functions and also useful for skipping plots when a multi-figure region is in use. " plot.new() 上的帮助页面显示:“此函数(frame 是 plot.new 的别名)导致在当前绘图中完成绘图(如果有)并前进到新的图形框架。这是用于所有高级绘图功能,也可用于在使用多图形区域时跳过绘图。”

But is this really what I want?但这真的是我想要的吗? I mean, I want to draw everything in one graphics device, so why would I want to cause the completion of plotting, except maybe at the end of the code.我的意思是,我想在一个图形设备中绘制所有内容,那么为什么我要导致绘图完成,除了可能在代码的末尾。

Others have suggested, the problem is related to the usage of RStudio, but I do not use RStudio.其他人建议,问题与RStudio的使用有关,但我不使用RStudio。 I use Notepad++ in combination with NppToR.我将 Notepad++ 与 NppToR 结合使用。

Also, someone suggested to add { } around the code block (did not work).另外,有人建议在代码块周围添加 { } (不起作用)。

Please help.请帮忙。

Before using curve() function is needed to run plot() .在使用curve()函数之前需要运行plot() That is why you have a problem when saving the plot.这就是为什么您在保存绘图时遇到问题的原因。

Before running:运行前:

    for(i in seq_along(parameters)) {
      curve(change(score=1, d=x, k=parameters[i], p=-800), from=-500, to=500, add=T, ylim=c(0, 100), col=colorshelf[i], xlab="rating difference", ylab="gain for winning")}

you need to run plot() giving the margins, labels and information useful to represent your images.您需要运行plot()给出对表示图像有用的边距、标签和信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM