title: Error: plot.new has not been called yet
body: <p>The following code produces an image. No problem. </p>
  
  <pre><code>change &lt;- function(score, d, k, p) {k*(score - 1/(1+k^(d/p)))}
  parameters &lt;- c(10:110)
  colorshelf &lt;-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 &lt;- round(quantile(seq_along(parameters)))
  legend.param &lt;- legend.index + min(parameters)
  legend.color &lt;- colorshelf[legend.index]
  legend("right", title="k-factor", lty=c(1,1), legend=legend.param, col=legend.color)
  </code></pre>
  
  <p>Now I would like to save the image to a file with specified resolution. So I add: </p>
  
  <pre><code>png(filename="gain by ratingdiff.png", res=30, width = 1000, height = 1000)
  </code></pre>
  
  <p>and </p>
  
  <pre><code>dev.off()
  </code></pre>
  
  <p>before and after the code block. But then I get two errors, complaining about <strong><em>plot.new has not been called yet</em></strong>. </p>
  
  <p>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. </p>
  
  <p>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. "</p>
  
  <p>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. </p>
  
  <p>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. </p>
  
  <p>Also, someone suggested to add { } around the code block (did not work). </p>
  
  <p>Please help.</p>