• Register
0 votes
2.9k 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

Please log in or register to answer this question.

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
1 answer 13 views
13 views
Problem: i tried alot to fix it but it didnt work , can anybody explain why not ? Error in plot.xy(xy.coords(x, y), type = type, ...) : plot.new has not been called yet.
asked Mar 6 Wafa Abu Yousef 6.1k points
0 votes
1 answer 2 views
2 views
Problem: it shows me: Error in xy.coords(x, y, xlabel, ylabel, log) : 'x' and 'y' lengths differ It does not work, inside t, both a and b are of length 2, why it showing me x, y lengths differ?
asked 4 days ago ummesalma 25.2k points
0 votes
1 answer 5 views
5 views
Problem: I've looked in different questions for a solution and I've tried what was suggested but I have not found a solution to make it work. Every time I want to run this code it always says: Error in plot.new() : figure margins too large and I don't know how to fix it.
asked Mar 30 ummesalma 25.2k points
0 votes
1 answer 44 views
44 views
Problem: error in sort(y) : argument "y" is missing, with no default.
asked Feb 16 charles mathews 5.5k points
0 votes
1 answer 2 views
2 views
Problem: My question is: How to show F2, F3, ..., F11 against the label in one graph with some dodge position?
asked 13 hours ago anika11 4.1k points
0 votes
1 answer 3 views
3 views
Problem : Can anybody please tell me plotting subset of data in r ?
asked Apr 3 Madhav96 2.3k points
0 votes
1 answer 3 views
3 views
Problem: In a dataset, I want to take two attributes and create a supervised scatter plot. Does anyone know how to give different colors to each class? Thanks
asked Mar 27 ummesalma 25.2k points
1 vote
1 answer 8 views
8 views
Problem: I&rsquo;m new in data frame. I would like to plot y1 and y2 in the same plot. x <- seq(-2, 2, 0.05) y1 <- pnorm(x) y2 <- pnorm(x, 1, 1) plot(x, y1, type = "l", col = "red") plot(x, y2, type = "l", col = "green") But when I do it like this, they are not plotted in the same plot together. In Matlab one can do hold on, but does anyone know how to do this in R?
asked Mar 10 Shovo210 13.8k points
0 votes
2 answers 183 views
183 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 802 views
802 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, 2020 Raphael Pacheco 4.9k points