Fixing the R error in plot.xy(xy.coords(x, y), type = type, …) : plot.new has not been called yet

The plot.new has not been called yet error message occurs when you try to draw a line on an r window before a plot window is opened. To fix this problem you must first open a plot window using the plot function. This is an easy problem to understand and fix. It results largely from forgetting to open a plot window before trying to draw a line. It is most likely to occur when doing a lot of grafting and forgetting to open a new plot in the process.

Description of the R error: plot.new has not been called yet

The plot function creates a graph window and a scatter plot within it, based on the data frame it is handed. It produces points based on the coordinates given to it by the object. You can select the color and change the axis labels using the ylab and xlab functions. This message is most likely to occur when trying to draw a separate line on an existing plot whose creating function is not within the visible code. The plot function is not part of a package, but it is a basic R function. Each value from the data frame is used to create a line or a curve from the lines and abline functions. However, a graph window needs to be open for these functions to work, and so the plot function must be called first.

Explanation of the R error: plot.new has not been called yet

Here is a code example that shows how to produce this error message.

> df = data.frame(x=c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
+ y=c(2, 4, 6, 8, 10,12, 14,16,18,20))
>
> lines(y~x, data=df)
Error in plot.xy(xy.coords(x, y), type = type, …) :
plot.new has not been called yet

There is nothing wrong with the data points in this data frame. Furthermore, there are no argument issues with the lines function. The error message occurs because the plot function did not precede it and therefore could not create the graph window. As a result, the lines function had no place to draw its lines. You get a similar error message when using the abline function under the same circumstances.

How to fix the R Error: plot.new has not been called yet

Here are two code examples illustrating how to fix this problem.

> df = data.frame(x=c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
+ y=c(2, 4, 6, 8, 10,12,14,16,18, 20))
>
> plot(y~x, data=df)
> lines(y~x, data=df)

In this example, the plot function is called before the lines function. No problem occurs because the graph window exists when the lines function is called.

> df = data.frame(x=c(10,11,13,15,16,15,13,11,10),
+ y=c(10, 5, 3, 5,10,15,17,15,10))
>
> plot(y~x, data=df)
> lines(y~x, data=df)

This example also illustrates how to fix this problem, not only are both functions present, but they are in the right order. When you run this code, it draws a nice little polygon in the graph window.

This is an easy problem to understand and fix. It is also an easy mistake to make because you may think you have called the plot function earlier without doing so. This is most likely to occur when you are writing a lengthy program. Once you understand the mistake that leads to this message you should be able to avoid it. However, if you stumble into it, you will find it an easy problem to fix. All you need to do is add the plot function before the lines function and it will fix the problem when it occurs.

Scroll to top