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

This error message will show up when you are creating a scatter plot and you call the lines function before calling the plot function. The way to fix this problem is to place the plot function before the lines function. This may seem like a ridiculous mistake to make but it is possible when writing a long-complicated program. Furthermore, if you are just learning how to produce graphs using the R programming language, it would be an easy mistake to make.

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

The plot function is designed to produce a scatter plot of the data points provided it. To use it you do not have to install a new package because it is one of the native R functions. When called it will open a plot window to graph the data points you have provided. This will not erase an existing plot but simply add a new plot to the existing stack. It will produce a dot on the graph for each value pair that is given it. This includes data points that will produce a geometric shape like a polygon in the graph window. Both the abline and lines functions can be used to add a separate line to the graph. If the lines function is called before the plot function you will get our error message. You get a similar error message if you call the abline function under the same circumstances.

### Explanation of the error

Here is an example of code that illustrates this problem. It shows just how simple a problem it is.

> 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))
>
> model = lm(y~poly(x, 2), data=df)
> new_x = seq(min(df\$x), max(df\$y))
> lines(new_x, predict(model, newdata = data.frame(x=new_x)))
Error in plot.xy(xy.coords(x, y), type = type, …) :
plot.new has not been called yet

In this example, there is no plot function but there is a lines function. This is the exact situation that causes this problem. The lines function does not produce a graph window, but simply adds a line to an existing plot. Because the graph window does not exist, this message is triggered.

### How to fix the R error

Here is an example of how to fix this problem. It is an extremely easy problem to fix.

> 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))
>
> model = lm(y~poly(x, 2), data=df)
> new_x = seq(min(df\$x), max(df\$y))
> plot(y~x, data=df)
> lines(new_x, predict(model, newdata = data.frame(x=new_x)))

In this example, we have added the plot function followed by the lines function, to produce a separate line. The variable provides data points to the plot function allowing it to open a plot window. Then the lines function has a window that it can draw its line in. The message is not triggered because there is no problem.

This error message differs from most such messages because it is not a problem with the input or a function’s argument. The problem is that the entire plot function is missing from the code. Adding it where it belongs fixes the problem. This is a problem that once you are aware of it and know how to fix, you will not likely encounter it again.

Scroll to top