6.9 C
London
Thursday, December 19, 2024
HomeRFix Common Errors in RHow to Fix in R: plot.new has not been called yet

How to Fix in R: plot.new has not been called yet

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

One error you may encounter when using R is:

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

This error occurs when you attempt to perform some action that requires a plot to already exist in R, yet a plot does not exist.

The following examples show how to fix this error in practice.

Example 1: How to Fix Error with lines()

Suppose we attempt to plot a fitted regression line in R:

#create data
df frame(x=c(1, 2, 2, 3, 5, 6, 8, 8, 9, 9, 10, 11, 12, 15, 15),
                 y=c(2, 3, 3, 4, 5, 5, 6, 7, 8, 8, 9, 10, 16, 19, 28))

#fit polynomial regression model
model #define new sequence of x-values
new_x #attempt to plot fitted regression line
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

We receive an error because we can’t use the lines() function without first creating a plot in R.

To fix this error, we can first create a scatterplot and then use the lines() function:

#create data
df frame(x=c(1, 2, 2, 3, 5, 6, 8, 8, 9, 9, 10, 11, 12, 15, 15),
                 y=c(2, 3, 3, 4, 5, 5, 6, 7, 8, 8, 9, 10, 16, 19, 28))

#fit polynomial regression model
model #define new sequence of x-values
new_x #create scatterplot of x vs. y values
plot(y~x, data=df)

#attempt to plot fitted regression line
lines(new_x, predict(model, newdata = data.frame(x=new_x))) 

Notice that we don’t receive an error because we first used the plot() function before using the lines() function.

Example 2: How to Fix Error with abline()

Suppose we attempt to create a scatterplot with a straight horizontal line in R:

#create data
df frame(x=c(1, 2, 2, 3, 5, 6, 8, 8, 9, 9, 10, 11, 12, 15, 15),
                 y=c(2, 3, 3, 4, 5, 5, 6, 7, 8, 8, 9, 10, 16, 19, 28))

#attempt to add horizontal line at y=10
abline(a=10, b=0, lwd=2)

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

We receive an error because we can’t use the abline() function without first creating a plot in R.

To fix this error, we can first create a scatterplot and then use the abline() function:

#create data
df frame(x=c(1, 2, 2, 3, 5, 6, 8, 8, 9, 9, 10, 11, 12, 15, 15),
                 y=c(2, 3, 3, 4, 5, 5, 6, 7, 8, 8, 9, 10, 16, 19, 28))

#create scatterplot of x vs. y
plot(y~x, data=df)

#add horizontal line at y=10
abline(a=10, b=0, lwd=2)

Notice that we don’t receive an error because we first used the plot() function before using the abline() function.

Related: How to Use abline() in R to Add Straight Lines to Plots

Additional Resources

The following tutorials explain how to fix other common errors in R:

How to Fix in R: Unexpected String Constant
How to Fix in R: invalid model formula in ExtractVars
How to Fix in R: argument is not numeric or logical: returning na

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories