[英]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.