60

Why does this happen?

plot(x,y)
yx.lm <- lm(y ~ x)
lines(x, predict(yx.lm), col="red")

Error in plot.xy(xy.coords(x, y), type = type, ...) : plot.new has not been called yet

2
  • 1
    Works here when applied to two short vectors. Please show us str(x) and str(y).
    – NPE
    Aug 22, 2011 at 22:04
  • 3
    How do you expect us to comment with specifics? We don't have x, y, nor the code used to fit the linear model. Please help us to help you. Aug 22, 2011 at 22:32

7 Answers 7

46

Some action, very possibly not represented in the visible code, has closed the interactive screen device. It could be done either by a "click" on a close-button, or it could also be done by an extra dev.off() when plotting to a file-graphics device. (The second possibility might happen if you paste in a multi-line plotting command that has a dev.off() at the end of it, but had errored out at the opening of the external device. So the dangling dev.off() on a separate line accidentally closes the interactive device).

Some (most?) R implementations will start up a screen graphics device open automatically, but if you close it down, you then need to re-initialize it. On Windows that might be window(); on a Mac, quartz(); and on a Linux box, x11(). You also may need to issue a plot.new() command. I just follow orders. When I get that error I issue plot.new() and if I don't see a plot window, I issue quartz() as well. I then start over from the beginning with a new plot(., ., ...) command and any further additions to that plot screen image.

5
  • 8
    This answer is similar to the "if in doubt, reformat your hard drive" advice. There's no need to add unwanted plot.new() calls. The OP (obviously) left out his lm() call, and most likely some other call that closed is plot window. plot.new() will not "re-attach" to the existing plot of x vs y, so his lines() call wouldn't be written to that plot. Aug 23, 2011 at 11:30
  • If it is at worst harmless, then it is not similar to your example nor even similar to rm(list=ls()).
    – IRTFM
    Aug 23, 2011 at 13:16
  • 2
    It is not always harmless. And as I pointed out, plot(x,y);plot.new();lines(...) will not do what the OP wanted Aug 23, 2011 at 15:42
  • I suppose. I didn't imagine that it would have been issued between the plot and the lines commands. I was assuming it would be issued before the plot call. As I said ... after the interactive graphics device has been re-initialized, I then redo the plotting operation from the beginning.
    – IRTFM
    Dec 18, 2013 at 16:17
  • 2
    on linux, I used plot.new().
    – dorien
    Oct 23, 2014 at 18:11
25

In my case, I was trying to call plot(x, y) and lines(x, predict(yx.lm), col="red") in two separate chunks in Rmarkdown file. It worked without problems when running chunk by chunk, but the corresponding document wouldn't knit. After I moved all plotting calls within one chunk, problem was resolved.

1
  • 9
    You also get the error if you try to run the lines separately within the same chunk, rather than executing the whole thing. I guess it calls dev.off() somewhere under the hood?
    – Gabriel
    Apr 28, 2017 at 15:18
6

As a newbie, I faced the same 'problem'.

In newbie terms : when you call plot(), the graph window gets the focus and you cannot enter further commands into R. That is when you conclude that you must close the graph window to return to R. However, some commands, like identify(), act on open/active graph windows. When identify() cannot find an open/active graph window, it gives this error message.

However, you can simply click on the R window without closing the graph window. Then you can type more commands at the R prompt, like identify() etc.

2

I had the same problem... my problem was that I was closing my quartz window after plot(x,y). Once I kept it open, the lines that previously resulted in errors just added things to my plot (like they were supposed to). Hopefully this might help some people who arrive at this page.

2

If someone is using print function (for example, with mtext), then firstly depict a null plot:

plot(0,type='n',axes=FALSE,ann=FALSE)

and then print with newpage = F

print(data, newpage = F)
2

plot.new() error occurs when only part of the function is ran.

Please find the attachment for an example to correct error With error....When abline is ran without plot() above enter image description here Error-free ...When both plot and abline ran together enter image description here

1

I had the problem in an RMarkdown, and putting the offending line on the previous line of code helped.

Minimal Reproducible Example

This will error if run line by line in an Rmd:

x <- rbind(matrix(rnorm(100, sd = 0.3), ncol = 2),
           matrix(rnorm(100, mean = 1, sd = 0.3), ncol = 2))
colnames(x) <- c("x", "y")
(cl <- kmeans(x, 2))
plot(x, col = cl$cluster)
points(cl$centers, col = 1:2, pch = 8, cex = 2)

but this works:

x <- rbind(matrix(rnorm(100, sd = 0.3), ncol = 2),
           matrix(rnorm(100, mean = 1, sd = 0.3), ncol = 2))
colnames(x) <- c("x", "y")
(cl <- kmeans(x, 2))
plot(x, col = cl$cluster); points(cl$centers, col = 1:2, pch = 8, cex = 2)

The only change is that the offending line (the last one) is placed after the last succeeding line (placing a ; in between). You can do it for as many offending lines as desired.

Not the answer you're looking for? Browse other questions tagged or ask your own question.