vanao veneri
vanao veneri

Reputation: 1064

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.

I know this issue came up like a million times. And there are so many posts about this here on stackoverflow. But none of these really helped me out. I tried adding plot.new() at different places in the code. 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. "

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. I use Notepad++ in combination with NppToR.

Also, someone suggested to add { } around the code block (did not work).

Please help.

Upvotes: 1

Views: 14358

Answers (1)

R18
R18

Reputation: 1560

Before using curve()function is needed to run 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.

Upvotes: 3

Related Questions