ggsave png 尺寸较大时出现错误
我正在保存一个分面 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
注意:在 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:
显然,某个地方可能存在涉及某种内存泄漏的错误?给出以下代码:
这对我来说运行良好,直到我达到大约 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:
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 withpng()
anddev.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 megc()
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.我在 Windows(32 位)中使用 png、jpeg 和 pdf 扩展名时就遇到过这种情况。经过一番研究,我发现原因是我试图将它们直接保存在硬盘中:
看来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:
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.
我遇到了这个问题,我只是输入
将文件保存在所需的目录中,而没有提及路径或设备。
我正在使用 Windows 10。
更新:
我尝试了两个示例来使用 ggsave 保存绘图,并且通过使用我的解决方案(其中直接包含绘图和文件名)效果很好。或者我添加路径、文件名、设备的位置。
看来问题与 Rstudio 上的项目目录路径较长有关,因此 ggsave 会解决该错误。
I faced that problem and I just typed
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.