简体   繁体   English

plot.new 尚未被调用

[英]plot.new has not been called yet

Why does this happen?为什么会这样?

plot(x,y)
yx.lm <- lm(y ~ x)
lines(x, predict(yx.lm), col="red")

Error in plot.xy(xy.coords(x, y), type = type, ...) : plot.new has not been called yet plot.xy(xy.coords(x, y), type = type, ...)中的错误:尚未调用plot.new

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. 可以通过单击关闭按钮来完成。 (Could also 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 but then has hte dev.off() on a separate line so it accidentally closes the interactive device). (绘制到文件图形设备时,也可以通过额外的dev.off()完成。如果您粘贴多行绘图命令,但该命令的末尾有dev,off(),则可能会发生这种情况,这可能会发生在外部设备打开时退出,但随后将dev.off()放在单独的行上,因此意外关闭了交互式设备)。

Some (most?) R implementations will start up a screen graphics device open automatically, but if you close it down, you then need to re-initialize it. 一些(大多数?)R实现将启动自动打开的屏幕图形设备,但是如果将其关闭,则需要重新初始化它。 On Windows that might be window() ; 在Windows上可能是window() ; on a Mac, quartz() ; 在Mac上, quartz() ; and on a linux box, x11() . 在Linux机器上,是x11() You also may need to issue a plot.new() command. 您可能还需要发出plot.new()命令。 I just follow orders. 我只听命令 When I get that error I issue plot.new() and if I don't see a plot window, I issue quartz() as well. 当我收到该错误时,我发出plot.new() ,如果没有看到绘图窗口,我也会发出quartz() I then start over from the beginning with a new plot(., ., ...) command and any further additions to that plot screen image. 然后,我从头开始使用新的plot(., ., ...)命令以及对该图屏幕图像的任何进一步添加。

In my case, I was trying to call plot(x, y) and lines(x, predict(yx.lm), col="red") in two separate chunks in Rmarkdown file. 就我而言,我试图在Rmarkdown文件的两个单独的块中调用plot(x, y)lines(x, predict(yx.lm), col="red") It worked without problems when running chunk by chunk, but the corresponding document wouldn't knit. 逐块运行时,它可以正常工作,但是对应的文档不会编织。 After I moved all plotting calls within one chunk, problem was resolved. 将所有绘图调用移动到一个块中之后,问题得以解决。

As a newbie, I faced the same 'problem'. 作为一个新手,我面临着同样的“问题”。

In newbie terms : when you call plot() , the graph window gets the focus and you cannot enter further commands into R. That is when you conclude that you must close the graph window to return to R. However, some commands, like identify() , act on open/active graph windows. 在新手而言:当你调用plot()图形窗口获得焦点,当你决定你必须关闭图形窗口返回R.但是,不能输入其他命令到R.也就是说,一些命令,如identify() ,作用于打开/活动的图形窗口。 When identify() cannot find an open/active graph window, it gives this error message. identify()找不到打开/活动的图形窗口时,它将给出此错误消息。

However, you can simply click on the R window without closing the graph window . 但是,您可以简单地单击R窗口而不关闭图形窗口 Then you can type more commands at the R prompt, like identify() etc. 然后,您可以在R提示符下键入更多命令,例如identify()等。

I had the same problem... my problem was that I was closing my quartz window after plot(x,y). 我有同样的问题...我的问题是我在plot(x,y)之后关闭了石英窗。 Once I kept it open, the lines that previously resulted in errors just added things to my plot (like they were supposed to). 一旦我打开它,以前导致错误的线就把东西添加到我的绘图中(就像它们本来应该那样)。 Hopefully this might help some people who arrive at this page. 希望这可以对到达此页面的某些人有所帮助。

If someone is using print function (for example, with mtext), then firstly depict a null plot: 如果有人正在使用print功能(例如,使用mtext),则首先描述一个空图:

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

and then print with newpage = F 然后以newpage = F打印

print(data, newpage = F)

plot.new() error occurs when only part of the function is ran. 仅运行部分函数时会发生plot.new()错误。

Please find the attachment for an example to correct error With error....When abline is ran without plot() above 请找到示例的附件以纠正有错误的错误。...在没有上述plot()的情况下运行abline 在此处输入图片说明 Error-free ...When both plot and abline ran together 无错误...当情节和退位同时发生时 在此处输入图片说明

I had the problem in an RMarkdown, and putting the offending line on the previous line of code helped.我在 RMarkdown 中遇到了问题,将有问题的行放在前一行代码有帮助。

Minimal Reproducible Example最小可重现示例

This will error if run line by line in an Rmd:如果在 Rmd 中逐行运行,这将出错:

x <- rbind(matrix(rnorm(100, sd = 0.3), ncol = 2),
           matrix(rnorm(100, mean = 1, sd = 0.3), ncol = 2))
colnames(x) <- c("x", "y")
(cl <- kmeans(x, 2))
plot(x, col = cl$cluster)
points(cl$centers, col = 1:2, pch = 8, cex = 2)

but this works:但这有效:

x <- rbind(matrix(rnorm(100, sd = 0.3), ncol = 2),
           matrix(rnorm(100, mean = 1, sd = 0.3), ncol = 2))
colnames(x) <- c("x", "y")
(cl <- kmeans(x, 2))
plot(x, col = cl$cluster); points(cl$centers, col = 1:2, pch = 8, cex = 2)

The only change is that the offending line (the last one) is placed after the last succeeding line (placing a ; in between).唯一的变化是违规行(最后一行)被放置在最后一行之后(在中间放置一个; )。 You can do it for as many offending lines as desired.您可以根据需要对任意数量的违规行执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM