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

Hey there, fellow R enthusiasts! Have you ever encountered the frustrating “plot.new has not been called yet” error message when creating plots in R? Fear not, my friend! In this article, we’ll guide you through troubleshooting and resolving this pesky error message so you can create beautiful and accurate visualizations in R. The article explains what the error message means and provides tips and techniques for resolving the issue. 

This 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. 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 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.

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

No problem occurs because the graph window exists when the lines function is called.

So while this error may appear intimidating at first, the solution comes down to how you sequence the activities in your program. Check to make sure the window that you’re drawing on has actually been created. If you’ve got the handled, this error should disappear.

Scroll to top
Privacy Policy