. Advertisement .
..3..
. Advertisement .
..4..
Recently, I ran some of my r code, and it gave the warning text:
Error in plot.xy(xy.coords(x, y), type = type, ...) :
plot.new has not been called yet
While searching, I realized that some people added some command lines in my sample above. But I don’t think it is the best way to correct the problem – error in plot.xy(xy.coords(x, y), type = type, …) : plot.new has not been called yet How would you explain this trouble? or Is there a better way? Below is the detail of the command that I used:
#create data
df <- data.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 <- lm(y~poly(x, 2), data=df)
#define new sequence of x-values
new_x <- seq(min(df$x), max(df$y))
#attempt to plot fitted regression line
lines(new_x, predict(model, newdata = data.frame(x=new_x)))
The cause: This error occurs because the lines() function can’t be used without first creating a plot in R.
Solution: First let’s create a scatterplot and then use the lines() function as below:
The interactive screen device has been closed by some action that is not visible in the code. You could do it by clicking on the close-button or by using an extra dev.off() to plot to a file-graphics device. The second possibility is when you paste in a multiline plotting command with a
dev.off()
at its end, but that was not correct at the opening of an external device. The interactive device is accidentally closed by thedev.off()
that was left hanging on a separate line.Some (most?) of the above mentioned implementations will open a screen graphics device automatically. Some (most?) R implementations will automatically open a screen graphics device. However, if it is closed down, you need to re-initialize the device. This
window()
quartz()
x11()
window()
window()
quartz()
x11()
x11()
x11()
x11()
x11()
window()
window()
window()
window()
quartz()
quartz()
quartz()
x11()
x11()
x11()
Aplot.new()
command may also be required. I simply follow orders. If I receive an error, I issueplot.new()
. If I don’t see the plot window, I issuequartz()
. I then restart the process withplot(., ., ...)
and any additional plot screen images.