[英]ggsave() fails with Error in png_dev(..., res = dpi, units = "in") : unable to start png() device
当我尝试使用ggsave()
将 ggplot 保存到新目录时,出现错误。
library(ggplot2)
ggplot(mtcars, aes(x = cyl)) +
geom_bar()
ggsave("current_folder/new_folder/new_plot.png")
产生错误:
Saving 11.3 x 7.32 in image
Error in png_dev(..., res = dpi, units = "in") :
unable to start png() device
In addition: Warning messages:
1: In png_dev(..., res = dpi, units = "in") :
unable to open file 'current_folder/new_folder/new_plot.png' for writing
2: In png_dev(..., res = dpi, units = "in") : opening device failed
如果目标目录尚不存在, ggsave()
不会将其创建为保存的一部分。 相反,它将引发此错误。
首先在 R 之外的操作系统中创建目录,或者使用dir.create
像在这个答案中一样: https://stackoverflow.com/a/29784923 。
然后用ggsave()
保存。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.