ggsave png 尺寸较大时出现错误

发布于 2024-10-03 21:03:47 字数 701 浏览 0 评论 0原文

我正在保存一个分面 ggplot2 图,它可以很好地以较小的尺寸保存,但当我想增加它时却失败了。

> ggsave("tst.png",height=6.75,width=9)
# works fine

> ggsave("tst.png",height=9,width=12)
Error in grDevices::png(..., width = width, height = height, res = dpi,  : 
  unable to start device
In addition: Warning messages:
1: In grDevices::png(..., width = width, height = height, res = dpi,  :
  Unable to allocate bitmap
2: In grDevices::png(..., width = width, height = height, res = dpi,  :
  opening device failed

我之前用 ggsave 保存过这种大小的 png,有什么想法为什么它不起作用吗?

可重现的示例:

library(car)
qplot(education,data=Vocab,geom="density",colour=sex)+facet_wrap(~year)

I'm saving a faceted ggplot2 plot which works fine to save at a smaller size, but fails when I want to increase it.

> ggsave("tst.png",height=6.75,width=9)
# works fine

> ggsave("tst.png",height=9,width=12)
Error in grDevices::png(..., width = width, height = height, res = dpi,  : 
  unable to start device
In addition: Warning messages:
1: In grDevices::png(..., width = width, height = height, res = dpi,  :
  Unable to allocate bitmap
2: In grDevices::png(..., width = width, height = height, res = dpi,  :
  opening device failed

I've saved pngs of this size before with ggsave, any ideas why its not working?

Reproducible example:

library(car)
qplot(education,data=Vocab,geom="density",colour=sex)+facet_wrap(~year)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一绘本一梦想 2024-10-10 21:03:47

注意:在 Windows 7 64 位上使用 R 2.12.1,此问题已消失。如果您遇到此问题,请首先尝试更新您的 R 版本。

在另一个问题中再次出现该问题后,我在新系统上重新运行了测试代码,看看错误是否消失了,事实确实如此。


编辑:底层代码可以工作的秘诀在于它仅使用 72 dpi 的分辨率,而不是我相信的 ggsave() 中的标准 300dpi。

所以 ggsave("tst.png",height=9,width=12,dpi=72) 可以做到这一点。

但如果你不能接受的话,你真的必须有一个疯狂的情节。据我猜测,问题与显卡有关(源自 来自 Ripley 教授的消息)。

如果分辨率有问题,您最好使用矢量化格式,例如 eps 或 pdf。


编辑2:

显然,某个地方可能存在涉及某种内存泄漏的错误?给出以下代码:

library(car)
library(ggplot2)
qplot(education,data=Vocab,geom="density",colour=sex)+facet_wrap(~year)
setwd("G:/Temp")
i<-1
while(1){
  tryCatch(ggsave("tst.png",height=9+i,width=12+i),error=function(e) {print(i);stop(e);})
  i <- i+1
}

这对我来说运行良好,直到我达到大约 9,然后我得到你得到的错误。每次尝试运行该代码(从 i=1 重新开始)都会给出相同的错误。尝试使用 png()dev.off() 再次出现相同的错误。似乎内存的某些部分被填满并且没有被清空,有效地防止了保存另一个 png 文件。对我来说,gc()也没有做任何事情。即使关闭R并再次重新打开也不起作用。

使用 ggsave("tst.pdf") 已“解决”该问题,但错误仍然存​​在。我会向 R 团队报告。

NOTE : Using R 2.12.1 on Windows 7 64bit, this problem has vanished. If you run into this problem, first try updating your R version.

After the problem came up again in another question, I reran my test code on my new system to see if the bug was gone, and it is.


EDIT: The trick why underlying code could work is the fact that it uses a resolution of only 72 dpi and not 300dpi as is the standard in ggsave() I believe.

so ggsave("tst.png",height=9,width=12,dpi=72) could do the trick.

But you really must have a crazy plot if it can't take it. As far as I can guess, the problem is related to the graphics card (as derived from this message from prof. Ripley ).

If resolution is a problem, you could better go to vectorized formats like eps or pdf.


EDIT 2 :

Apparently, there is a bug somewhere involving some kind of memory leak maybe? Give following code:

library(car)
library(ggplot2)
qplot(education,data=Vocab,geom="density",colour=sex)+facet_wrap(~year)
setwd("G:/Temp")
i<-1
while(1){
  tryCatch(ggsave("tst.png",height=9+i,width=12+i),error=function(e) {print(i);stop(e);})
  i <- i+1
}

This runs fine for me until i reaches about 9, then I get the error you get. Every next attempt at running the code, starting again with i=1, gives the same error. Trying with png() and dev.off() gives again the same error. Seems like there is some part of a memory filling up and not being emptied, effectively preventing to get another png file saved. also for me gc() didn't do a thing. Even closing R and reopening again didn't work.

It is "solved" using ggsave("tst.pdf"), but the bug remains. I'd report to the R team.

一曲爱恨情仇 2024-10-10 21:03:47

我在 Windows(32 位)中使用 png、jpeg 和 pdf 扩展名时就遇到过这种情况。经过一番研究,我发现原因是我试图将它们直接保存在硬盘中:

ggsave(paste("C:/how",eval(parse(text = "i")),eval(parse(text = "j")),".pdf",sep="_"),height=6.75,width=9)

看来RStudio没有管理员权限直接写入C:/。我已将文件夹更改为桌面,现在一切正常。

It has happened to me with png, jpeg and pdf extensions in Windows (32 bits). After a bit of research, I discovered that the cause was that I was trying to save them in the hard disk directly:

ggsave(paste("C:/how",eval(parse(text = "i")),eval(parse(text = "j")),".pdf",sep="_"),height=6.75,width=9)

It seems that RStudio has not administrator permissions to write directly into C:/. I have changed the folder to Desktop and now everything is working fine.

少跟Wǒ拽 2024-10-10 21:03:47

我遇到了这个问题,我只是输入

ggsave(plot_name, filename = "output_directory/xxxx.png")

将文件保存在所需的目录中,而没有提及路径或设备。

我正在使用 Windows 10。

更新:

我尝试了两个示例来使用 ggsave 保存绘图,并且通过使用我的解决方案(其中直接包含绘图和文件名)效果很好。或者我添加路径、文件名、设备的位置。

看来问题与 Rstudio 上的项目目录路径较长有关,因此 ggsave 会解决该错误。

library(ggplot2)

plot <- ggplot(data = iris, aes(x = Petal.Length, y = Petal.Width, color = Species)) + geom_point()


ggsave(plot, path = "01_Output/long path saving file for ggsave example/", filename = "iris3.png", device = "png")

ggsave(plot, filename = "01_Output/long path saving file for ggsave example/iris2.png")

I faced that problem and I just typed

ggsave(plot_name, filename = "output_directory/xxxx.png")

it saved the file in the required directory without mentioning the path or the device.

I am using windows 10.

update:

I tried two examples to save plot using ggsave, and it worked well by using my solution where I included the plot and file name directly. or where I added path, filename, device.

It looks the problem is related to the path of the project directory on Rstudio is long so ggsave throughs that error.

library(ggplot2)

plot <- ggplot(data = iris, aes(x = Petal.Length, y = Petal.Width, color = Species)) + geom_point()


ggsave(plot, path = "01_Output/long path saving file for ggsave example/", filename = "iris3.png", device = "png")

ggsave(plot, filename = "01_Output/long path saving file for ggsave example/iris2.png")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文