Select Page

# How to Solve R Error: plot.new has not been called yet

by | Programming, R, Tips

The error “plot. new has not been called yet” occurs when you try to do something that requires a plot to exist but have not yet created a plot.

You can solve this by creating a plot first before trying to perform the required action. For example,

```# Constructing coordinate vectors
x <- c(3.7, 2.7, 3.6, -2.2, -4.5,
3.4, 6.7, 4.8, 10.1, -11.9, 12.8, 9.3)
y <- c(1.6, 3.3, 2.3, -4.5, -3.7, 2.8,
2.7, 1.8, 2.2, 10.4, 1.5, 3.8)

plot(x, y, cex = 1, pch = 3,
xlab ="x", ylab ="y",
col ="black")

# Try to add horizontal line at y=5
abline(a=3, b=0, lwd=3)```

This tutorial will go through the error and how to solve it with code examples.

## Example #1

Let’s look at an example to reproduce the error:

```# Get weight from mtcars dataset

wt <- mtcars\$wt

# Get miles per gallon from mtcars dataset

mpg <- mtcars\$mpg

wt```

Let’s look at the `mpg` and `wt` values:

``` [1] 2.620 2.875 2.320 3.215 3.440 3.460 3.570 3.190 3.150 3.440 3.440 4.070 3.730 3.780
[15] 5.250 5.424 5.345 2.200 1.615 1.835 2.465 3.520 3.435 3.840 3.845 1.935 2.140 1.513
[29] 3.170 2.770 3.570 2.780```
`mpg`
``` [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4 10.4
[17] 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7 15.0 21.4```

Next, we will try to fit a linear regression model to the data:

```# Fit linear regression model to dataset

fit <- lm(mpg ~ wt)

# Get summary of model

summary(fit)```

Let’s run the code to get the summary of the fitted model.

```Call:
lm(formula = mpg ~ wt)

Residuals:
Min      1Q  Median      3Q     Max
-4.5432 -2.3647 -0.1252  1.4096  6.8727

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept)  37.2851     1.8776  19.858  < 2e-16 ***
wt           -5.3445     0.5591  -9.559 1.29e-10 ***
---
Signif. codes:  0 â€˜***â€™ 0.001 â€˜**â€™ 0.01 â€˜*â€™ 0.05 â€˜.â€™ 0.1 â€˜ â€™ 1

Residual standard error: 3.046 on 30 degrees of freedom
Multiple R-squared:  0.7528,	Adjusted R-squared:  0.7446
F-statistic: 91.38 on 1 and 30 DF,  p-value: 1.294e-10```

Next, we will try to plot the fitted line using the `lines()` function:

```# Attempt to plot fitted regression line

lines(wt, predict(fit), col='red')```

Let’s run the code to see the result:

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

The error occurs because we cannot use the `lines()` function without creating the plot first.

### Solution

We can solve the error by plotting the scatterplot using the `plot()` function and then call the `lines()` function to plot the fitted regression line:

```plot(wt~mpg)
lines(mpg, predict(fit), col='red')```

Let’s run the code to get the result:

## Example #2

Let’s look at a second example to reproduce the error.

```# Constructing coordinate vectors
x <- c(3.7, 2.7, 3.6, -2.2, -4.5,
3.4, 6.7, 4.8, 10.1, -11.9, 12.8, 9.3)
y <- c(1.6, 3.3, 2.3, -4.5, -3.7, 2.8,
2.7, 1.8, 2.2, 10.4, 1.5, 3.8)

# Try to add horizontal line at y=3 using the abline() function
abline(a=3, b=0, lwd=3)```

Let’s run the code to see what happens:

```Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) :
plot.new has not been called yet```

The error occurs because we cannot use the `abline()` function without creating a plot first.

### Solution

We can solve the error by first creating the scatterplot using the `plot()` function and then calling the `abline()` function to add a horizontal line to the scatterplot.

```# Constructing coordinate vectors
x <- c(3.7, 2.7, 3.6, -2.2, -4.5,
3.4, 6.7, 4.8, 10.1, -11.9, 12.8, 9.3)
y <- c(1.6, 3.3, 2.3, -4.5, -3.7, 2.8,
2.7, 1.8, 2.2, 10.4, 1.5, 3.8)

plot(x, y, cex = 1, pch = 3,
xlab ="x", ylab ="y",
col ="black")

# Try to add horizontal line at y=5
abline(a=3, b=0, lwd=3)```

Let’s run the code to see the result:

## Summary

Congratulations on reading to the end of this tutorial!

For further reading on R-related errors, go to the articles:Â

Go to the online courses page on R to learn more about coding in R for data science and machine learning.

Have fun and happy researching!