我试着用不同的变量集绘制一系列的散点图。为此,我使用"paste“将两个(或更多)变量名组合为
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个或更多变量名的"paste“创建的绘图名称时,但如果我只使用"plot.png”或单个字符线程("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")输入数据帧为:
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包中的"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,但同样的问题仍然存在。我很感谢任何解决这个问题的建议。
发布于 2018-04-14 06:06:19
我正在尝试绘制多个数据来进行聚类分析。我将代码放入for循环中,以使用列名命名图形来自动生成图形。然而,对于.png和.bmp类型的图形,它给出了一个错误。后来,我意识到因为我使用的是paste()函数,并且其中一个列名包含"\",所以代码给出了错误。在数据集中,我只需将"\“替换为"_”,问题就解决了。希望这对每个人都有帮助。
发布于 2018-03-13 04:43:31
用一个简单的图,我不能重现你的问题。
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 larger size
https://stackoverflow.com/questions/49243161
复制相似问题