繁体   English   中英

无法启动 plot 名称的 png() 设备使用“粘贴”function 创建

[英]unable to start png() device for plot names create with "paste" function

我正在尝试 plot 一系列具有不同变量集的散点图。 在这样做时,我使用“粘贴”将两个(或更多)变量名称组合为

plot.name <- paste(paste("var1.name", "var2.name",..., sep="_"), "plot.png", sep=" ")

然后我在“ggsave”中使用这个名字作为

ggsave(plot.name, width=7.5, height=5, units="in", dpi=300)

每次我尝试我得到

Error in grDevices::png(..., res = dpi, units = "in") : unable to start png() device 
In addition: Warning messages:
1: In grDevices::png(..., res = dpi, units = "in") : unable to open file 'plot.name'
2: In grDevices::png(..., res = dpi, units = "in") : opening device failed

只有当我使用包含 2 个或更多变量名称的“粘贴”创建的 plot 名称时才会发生这种情况,但如果我仅使用“plot.png”或字符的单个线程(“xxx xxx xxx xxx.png”)则不会发生这种情况. 所有类型的设备(jpg 等)都会发生这种情况。 有谁知道为什么一个简单的名称可以工作,但“粘贴”创建的名称不能保存图像? 这以前从来不是问题,但当我尝试使用 Rstudio 时它开始发生。我通常在 Tinn-R 中编写所有代码,然后将它们导出到 R。

以下是更多详细信息。

4个变量的名称是;

x.var.actual.name.list <-                           
     c("Elev.Nov.Prev.2.mo", "Sal.Fall", "ArtBio.Sep.Prev", "min.temp.Sep.Prev.2.mo")

输入 dataframe 是:

df.in <- combined.df[,c(y.var, x.var.actual.name.list)]

      Total Elev.Nov.Prev.2.mo Sal.Fall ArtBio.Sep.Prev min.temp.Sep.Prev.2.mo
      10.158711      6381.35   83.05407    17.143527    48.27
      10.684462      6381.00   83.64119    22.075855    49.38
      10.849221      6380.30   84.70405    26.175721    46.06
      10.021848      6381.55   82.23643    20.024815    47.19
      10.019090      6384.15   77.78226    17.459871    47.13
      10.171566      6382.55   80.97417    21.180415    49.33
      ...

例如,我使用从 GGally package 到 plot 相关矩阵的“ggcorr”;

require(GGally)        
cor.scatter.png.file <- paste(paste(x.var.actual.name.list, collapse=" "),
                        "correlation scatter plot matrix.png", sep=" ")    
cor.mat.plot <- ggcorr(df.in,2, palette = "RdYlGn", name = "r",label = T,                      
                label_color = "black", label_round = 2)

ggsave(cor.scatter.png.file, width=7.5, height=5, units="in")      

以上脚本导致错误;

Error in grDevices::png(..., res = dpi, units = "in") :                
unable to start png() device
In addition: Warning messages:
1: In grDevices::png(..., res = dpi, units = "in") :
unable to open file 'Elev.Nov.Prev.2.mo Sal.Fall ArtBio.Sep.Prev 
min.temp.Sep.Prev.2.mo correlation scatter plot matrix.png' for writing
2: In grDevices::png(..., res = dpi, units = "in") : opening device failed

但是,如果我执行以下操作(不使用名称的粘贴部分),它就会起作用。

ggsave("correlation scatter plot matrix.png", width=7.5, height=5, units="in")

具有粘贴名称的第一个脚本以前工作,但在我尝试在 Rstudio 中运行相同的脚本后停止工作。现在任何时候我使用粘贴名称 R 都会返回错误消息。 我已经卸载了 Rstudio 和 R 并重新安装了 R 但同样的问题仍然存在。 我感谢解决此问题的任何建议。

使用简单的情节,我无法重现您的问题。

df <- data.frame(a = rnorm(100,0,1), b=rnorm(100,0,1))
ggplot(df, aes(x=a, y=b)) + geom_point()

plot.name <- paste(paste("var1.name", "var2.name", sep="_"), "plot.png", sep=" ")
ggsave(plot.name, width=7.5, height=5, units="in", dpi=300)

上面的代码对我来说非常好。 尝试使用sep="_"而不是sep=" " ; 文件名不应包含空格或特殊字符,例如 * 。 ” / \\ [ ] : ; | = , < ? > & $ #! ' { } ( )。

此外,RStudio 可能会遇到在后台运行的图形设备的问题; 您可以尝试使用dev.off()退出在后台运行的打开图形设备。

编辑:

不幸的是,通过您的示例找到解决方案并不容易。 这是一个可重现的版本:

x.var.actual.name.list <-                           
  c("Elev.Nov.Prev.2.mo", "Sal.Fall", "ArtBio.Sep.Prev", "min.temp.Sep.Prev.2.mo")

df.in <- data.frame(matrix(nrow=100,ncol=5,rnorm(500)))
colnames(df.in) <- c("Total Elev", x.var.actual.name.list)


require(GGally)   

cor.scatter.png.file <- paste(paste(x.var.actual.name.list, collapse=" "),
                              "correlation scatter plot matrix.png", sep=" ")  


cor.mat.plot <- ggcorr(df.in, digits=2, palette = "RdYlGn", name = "r",label = T,                      
                       label_color = "black", label_round = 2)

ggsave(cor.scatter.png.file, width=7.5, height=5, units="in") 

请注意,我必须使用“digits=2”扩展 ggcorr() 中的“2”才能运行代码,希望这是正确的参数。 但是,该代码对我来说仍然非常好。 您可能还想查看此线程: ggsave png error with large size

我试图绘制多个数字进行聚类分析。 我将代码放入for循环以使用列名自动生成数字来命名数字。 但是,它给出了.png.bmp类型数字的错误。 后来,我意识到由于我使用了paste()函数并且其中一个列名包含“\\”,因此代码给出了错误。 在数据集中,我只是将“\\”替换为“_”,问题就解决了。 希望这对每个人都有帮助。

我有同样的问题并意识到我需要更新我的目录路径..

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM