[英]ggsave() fails with Error in png_dev(..., res = dpi, units = "in") : unable to start png() device
When I try to save a ggplot to a new directory with ggsave()
, I get an error.当我尝试使用
ggsave()
将 ggplot 保存到新目录时,出现错误。
library(ggplot2)
ggplot(mtcars, aes(x = cyl)) +
geom_bar()
ggsave("current_folder/new_folder/new_plot.png")
Produces the error:产生错误:
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
If the target directory does not already exist, ggsave()
will not create it as part of saving.如果目标目录尚不存在,
ggsave()
不会将其创建为保存的一部分。 It will instead throw this error.相反,它将引发此错误。
First create the directory, either in your operating system outside of R or with dir.create
like in this answer: https://stackoverflow.com/a/29784923 .首先在 R 之外的操作系统中创建目录,或者使用
dir.create
像在这个答案中一样: https://stackoverflow.com/a/29784923 。
Then save with ggsave()
.然后用
ggsave()
保存。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.