0

当我尝试使用 将 ggplot 保存到新目录时ggsave(),出现错误。

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
4

1 回答 1

0

如果目标目录尚不存在,ggsave()则不会在保存过程中创建它。相反,它将引发此错误。

首先在 R 之外的操作系统中或dir.create在此答案中创建目录:https ://stackoverflow.com/a/29784923 。

然后用ggsave().

于 2022-02-03T20:59:37.230 回答