2017-03-06 133 views
1

以下代码会生成图像。没问题。错误:plot.new尚未调用

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) 

现在我想将图像保存到指定分辨率的文件。所以,我想补充:

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

dev.off() 

之前和代码块之后。但后来我得到两个错误,抱怨plot.new尚未被调用

我知道这个问题出现了上百万次。在这里有很多关于这个在stackoverflow上的帖子。但是这些都没有帮助我。我尝试在代码中的不同位置添加plot.new()。但是这并没有帮助。

plot.new()上的帮助页面显示如下: “此函数(frame是plot.new的别名)导致当前绘图(如果有的话)中的绘图完成并且前进到新的图形框架,用于所有高级绘图功能,当使用多图区域时也可用于跳过绘图。“

但是,这真的是我想要的吗?我的意思是,我想在一个图形设备中绘制所有东西,那么为什么我想要导致绘图的完成,除了可能在代码的末尾。

其他人提出的,问题与RStudio的使用有关,但我不使用RStudio。我将Notepad ++与NppToR结合使用。

此外,有人建议围绕代码块添加{}(没有工作)。

请帮忙。

+1

如果你在'for'循环之前加上'plot(NA,xlim = c(-500,500),ylim = c(0,100)),该怎么办?问题在于'曲线'与曲线有一条曲线,但你没有开始的曲线。 – Axeman

+0

甜。而已。让它成为答案,它也可能帮助其他人。 –

+0

然而,当我不将它保存到一个文件时,仍然有点奇怪为什么它会起作用。标准图形设备是否也不需要这种情节?为什么不? –

回答

1

使用前curve()功能需要运行plot()。这就是为什么在保存情节时你有问题。

运行前:

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")} 

你需要运行plot()给予rerpesent您的图像有用的利润率,标签和信息。