• Register
0 votes
1.6k views

Problem :

Below is my code which is giving me the error.

plot(x,y)
yx.lm <- lm(y ~ x)
lines(x, predict(yx.lm), col="red")
Why am I facing following error?
Error in plot.xy(xy.coords(x, y), type = type, ...) : plot.new has not been called yet
6 5 3
7,540 points

2 Answers

0 votes

Solution :

You must have taken some action very possibly not present in the visible code, has closed your interactive screen device. It could have happened either by a "click" on the close-button. Could also be done by just an extra dev.off() while plotting to a file-graphics device. This can happen if you just paste in the mult-line plotting command that has the dev,off() at the end of it but errors start coming out at the opening of the external device but then has the dev.off() on a separate line so it may accidentally close the interactive device

Some of the R implementations will start up a screen graphics device open automatically, but if you have closed it down, then you need to re-initialize it. On the Windows that might be window(); on the Mac, quartz(); and on the linux box, x11(). You can also issue the plot.new() command. I just follow orders. When I get that particular error I issue the plot.new() and if I don't see the plot window, then I issue quartz() as well. I then simply start over from the beginning with the new plot(., ., ...) command and any further additions on to that plot screen image.

9 7 4
38,600 points
0 votes

Solution:

Some action, very possibly not represented in the visible code, has closed the interactive screen device. It could be done either by a "click" on a close-button. It can be done by an extra dev.off() when plotting to a file-graphics device. This may happen if you paste in a mult-line plotting command that has a dev,off() at the end of it but errors out at the opening of the external device however then has hte dev.off() on a separate line so it accidentally closes the interactive device).

Some  R implementations will start up a screen graphics device open automatically, however if you close it down, you then need to re-initialize it. On Windows that might be window(); on a Mac, quartz(); and on a linux box, x11(). You also may need to issue a plot.new() command. I just follow orders. At the time I get that error I issue plot.new() and if I don't see a plot window, I issue quartz() as well. So, then start over from the beginning with a new plot(., ., ...) command and any further additions to that plot screen image.

In my instance, I was trying to call plot(x, y) and lines(x, predict(yx.lm), col="red") in two separate chunks in R markdown file. It worked without problems when running chunk by chunk, however the corresponding document wouldn't knit. Since I moved all plotting calls within one chunk, problem was resolved.

In newbie terms : At the time you call plot(), the graph window gets the focus and you cannot enter further commands into R. That is the time when you conclude that you must close the graph window to return to R. But, some commands, like identify(), act on open/active graph windows. When identify() cannot find an open/active graph window, it gives this error message.

Nevertheless, you can simply click on the R window without closing the graph window. After you can type more commands at the R prompt, like identify() etc.

In case someone is using print function (for example, with mtext), then firstly depict a null plot:

plot(0,type='n',axes=FALSE,ann=FALSE)

and then print with newpage = F

print(data, newpage = F)

In base plotting, you start a plot by calling plot or a similar command which generates a plot, which you can then add to with functions like lines. For example, to plot a polynomial regression of mtcars,

model <- lm(mpg ~ poly(hp, 2), mtcars)    # make a model
domain <- seq(min(mtcars$hp), max(mtcars$hp))    # define a vector of x values to feed into model

plot(mpg ~ hp, mtcars)    # plot points
lines(domain, predict(model, newdata = data.frame(hp = domain)))    # add regression line, using `predict` to generate y-values

abline is a simplified version of lines that only plots straight lines, and which has a method for plotting simple lm objects.

For example, in the examples at the bottom of ?abline,

z <- lm(dist ~ speed, data = cars)
plot(cars)
abline(z) # equivalent to abline(reg = z) or
abline(coef = coef(z))

 

10 6 4
31,120 points

Related questions

0 votes
2 answers 116 views
116 views
Problem : I am stuck because of following weired error error in sort.list(y) : 'x' must be atomic for 'sort.list' have you called 'sort' on a list?
asked Nov 15, 2019 peterlaw 6.9k points
0 votes
1 answer 223 views
223 views
Problem: I have descent knowledge in R and plot. I have gained this knowledge by attending the online tutorials. Now I have the very simple and only single data file test.txt which contains the data as shown below for your reference: 1  5  7  9  11 Now I am ... : invalid plotting method. Can some expert point out what I am doing wrong here or try to suggest the better solution for above error?
asked Jul 27 Raphael Pacheco 4.9k points
0 votes
1 answer 2K views
2K views
Problem : While I am trying to run the boxcox transformation with the following code urban1 <- subset(ski,urban <= 4,na.rm=TRUE) ski$gender <- as.numeric((as.character(ski$gender)),na.rm=TRUE) urban1 <- as.numeric((as.character(urban1))) a <- (ski$gender*urban1) ... -> boxcar -> boxcar.formula -> lm -> lm.fit Execution halted Please let me know if anyone knows the solution for this error message.
asked Nov 27, 2019 alecxe 7.5k points
1 vote
1 answer 1.3K views
1.3K views
Problem : I want to perform a logistic regression but I am facing following error I am unable to find my mistake. summary(glm(prefmerkel~angst+crisismerkel+leadership+trustworthiness+ideology+pid+agegroups+gender+region,data=gles)) Error in glm.fit(x = c(1, 1, 1, 1, 1, 1, 1, ... In Ops.factor(eta, offset) : -' nicht sinnvoll für Faktoren 3: In Ops.factor(y, mu) : -' nicht sinnvoll für Faktoren
asked Nov 24, 2019 alecxe 7.5k points
0 votes
1 answer 921 views
921 views
Problem : Suppose I have data.frame a And I am using following code m.fit <- lm(col2 ~ col3 * col4, na.action = na.exclude) Here the col2 has some NA values And col3,col4 have values less than 1. I am getting following error: Error in lm.fit ... because of the NAs. Problem with this warning is interpreted by every google results is pointing towards the NA values. Have I misinterpreted the error ?
asked Nov 22, 2019 peterlaw 6.9k points