How to Fix in R: plot.new has not been called yet
Last Updated :
23 May, 2022
In this article, we will discuss how to fix the "plot.new has not been called yet" error in the R programming language.
The R compiler produces such an error when we try to carry an operation that requires a plot to exist in R but the plot doesn't exist yet.
Error in plot.xy(xy.coords(x, y), type = type, ...) :
plot.new has not been called yet
Method 1: How to fix with lines() function
Here we will focus on how we can fix the error that can be produced by the R compiler while dealing with the lines() function.
Example:
Let's consider an example, we have two vectors that hold the corresponding X and Y coordinates of 12 different points. Then we have used the lines() function to draw lines joining the points.
R
# R program to add lines into plots
# Constructing coordinate vectors
x <- c(1.7, 2.7, 3.7, -3.7, -5.7,
3.7, 5.7, 4.8, 10.3, -12.9, 13.8, 12.3)
y <- c(1.2, 2.3, 3.2, -3.5, -3.2, 2.1,
4.7, .8, 1.2, 11.5, 1.3, 3.2)
# Trying to draw lines of red colors
lines(x, y, col = "red")
Output:

The R compiler produces such an error because we didn't create the plot before using the lines() function. We can fix this error easily by creating a plot before using the lines() function:
Example:
Here, in this example, we are fixing the above error by simply calling the plot function before the lines() function to get the idea of the points given.
R
# R program to add lines into plots
# Constructing coordinate vectors
x <- c(1.7, 2.7, 3.7, -3.7, -5.7,
3.7, 5.7, 4.8, 10.3, -12.9, 13.8, 12.3)
y <- c(1.2, 2.3, 3.2, -3.5, -3.2, 2.1,
4.7, .8, 1.2, 11.5, 1.3, 3.2)
# Plotting the graph
plot(x, y, cex = 1, pch = 3,
xlab ="x", ylab ="y",
col ="black")
# Trying to draw lines of red colors
lines(x, y, col = "red")
Output:

This time the program compiled successfully because we have created the plot before using the lines() function.
Method 2: How to fix with abline() function:
In this part, we focus on how we can fix the error that can be produced by the R compiler while dealing with the abline() function.
Example:
Let's consider an example, we have two vectors that hold the corresponding X and Y coordinates of 12 different points. Then we have used the ablines() function to draw a horizontal line at Y = 5.
R
# R program to add a horizontal line
# Constructing coordinate vectors
x <- c(1.7, 2.7, 3.7, -3.7, -5.7,
3.7, 5.7, 4.8, 10.3, -12.9, 13.8, 12.3)
y <- c(1.2, 2.3, 3.2, -3.5, -3.2, 2.1,
4.7, .8, 1.2, 11.5, 1.3, 3.2)
# Try to add horizontal line at y=5
abline(a=5, b=0, lwd=3)
Output:

The R compiler produces such an error because we didn't create the plot before using the abline() function. We can fix this error easily by creating a plot before using the abline() function:
Example:
So, here for resolving the above error, we are calling the plot() function before the abline function with the given parameters, and by this, the program compiled successfully without any error.
R
# R program to add a horizontal line
# Constructing coordinate vectors
x <- c(1.7, 2.7, 3.7, -3.7, -5.7,
3.7, 5.7, 4.8, 10.3, -12.9, 13.8, 12.3)
y <- c(1.2, 2.3, 3.2, -3.5, -3.2, 2.1,
4.7, .8, 1.2, 11.5, 1.3, 3.2)
# Plotting the graph
plot(x, y, cex = 1, pch = 3,
xlab ="x", ylab ="y",
col ="black")
#attempt to add horizontal line at y=5
abline(a=5, b=0, lwd=3)
Output:

Similar Reads
How to Fix - Values Not Appearing in ggplot Plot in R When creating visualizations with ggplot2 in R, you might encounter situations where some values do not appear in the plot. This can be frustrating, but there are several common reasons and straightforward solutions for this issue. This article will guide you through diagnosing and fixing problems r
4 min read
How to plot user-defined functions in R? Plotting user-defined functions in R is a common task for visualizing mathematical functions, statistical models, or custom data transformations. This article provides a comprehensive guide on how to plot user-defined functions in R, including creating simple plots, enhancing them with additional fe
3 min read
How to Make ECDF Plot with ggplot2 in R? Empirical Cumulative Distribution Function Plot (ECDF) helps us to visualize one or more distributions. ECDF plot is a great alternative for histograms and it has the ability to show the full range of data without the need for various parameters. In this article, we will discuss how to draw an ECDF
3 min read
How to Add a plot title to ggvis in R In this article, we will be looking at the approach to adding a plot title to ggvis package in the R programming language. The ggvis package in R is used to provide the data visualization. It is used to create visual interactive graphics tools for data plotting and representation. The package can be
3 min read
How to Add Constant Line to Animated Plot in Plotly? Animated plots are a useful way to visualize data over time or to highlight the relationship between different variables. In this article, we will learn how to create animated plots using the plot_ly() function in R Programming Language. To create an animated plot in R, we will use the plot_ly() fun
2 min read
How to Fix Error in model.frame.default in R Errors in the model. frame. default function in R Programming Language can be annoying, but knowing how to fix them is essential for effective modelling and data analysis. However, users may encounter errors while using a model. frame.default, often due to issues related to the structure or content
3 min read