【问题标题】:ggsave() fails with Error in png_dev(..., res = dpi, units = "in") : unable to start png() deviceggsave() 失败并在 png_dev(..., res = dpi, units = "in") 中出现错误:无法启动 png() 设备
【发布时间】:2022-02-04 04:59:37
【问题描述】:

当我尝试使用 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

【问题讨论】:

    标签: r ggplot2 ggsave


    【解决方案1】:

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

    首先在 R 之外的操作系统中或使用dir.create 创建目录,如以下答案:https://stackoverflow.com/a/29784923

    然后用ggsave()保存。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-24
      • 2013-01-19
      • 2020-01-13
      • 2019-02-18
      • 2021-09-19
      • 2015-02-01
      相关资源
      最近更新 更多