2

I was trying to plot a series of scatterplots with different sets of variables. In doing so, I use "paste" to combine two (or more) variable names as

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

Then I use this name in "ggsave" as

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

Each time I tried I get

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

This only happens when I use a plot name created with "paste" containing 2 or more variable names but it does not happen if I use just "plot.png" or a single thread of character ("xxx xxx xxx xxx.png"). This happens with all types of devices (jpg, etc.). Does anyone have any idea why a simple name works but not a name created by "paste" does not to save images? This was never the issue before but it started happening when I tried to use Rstudio. I normally write all codes in Tinn-R then export them to R.

The followings are more details.

Names of 4 variables are;

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

An input dataframe is:

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
      ...

For instance I use "ggcorr" from GGally package to plot correlation matrix;

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")      

Above scrips result in an error;

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

But if I do the following (not using a pasted part of the name) it works.

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

The first script with a pasted name worked before, but stopped working after I tried to run the same script in Rstudio. Now anytime I use pasted names R returns the error message. I have uninstalled Rstudio and R and reinstalled R but the same issue persists. I appreciate any suggestions to fix this problem.

mhonda
  • 21
  • 1
  • 2
  • 4
  • 1
    When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Do any of your variables have slashed or other characters that can't be used in files names by your operating system? – MrFlick Mar 12 '18 at 19:38

3 Answers3

2

I was trying to plot multiple figures for clustering analysis. I put the code into for loop to generate the figures automatically using the column names to name the figures. However, it was giving an error for .png and .bmp type figures. Later, I realized that since I was using the paste() function and one of the column names was including "\", the code was giving the error. In the dataset, I just replaced "\" with "_" and the problem is solved. Hopefully, this helps everyone.

Mehmet Yildirim
  • 471
  • 1
  • 4
  • 17
1

Using a simple plot, I cannot reproduce your problem.

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)

Above code works perfectly fine for me. Try using sep="_" instead of sep=" "; filenames should contain no spaces or special characters such as * . ” / \ [ ] : ; | = , < ? > & $ # ! ‘ { } ( ).

Also, RStudio can have problems with graphic devices that run in the background; you can try to quit open graphic devices that run in the background using dev.off().

Edit:

Unfortunately, is not very easy to find a solution with your examples. Here is a reproducible version:

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") 

Note that I had to extend "2" in ggcorr() with "digits=2" to get the code running, hope that's the right parameter. However, the code still works perfectly fine for me. You might want to look into this thread as well: ggsave png error with larger size

yrx1702
  • 1,619
  • 15
  • 27
  • I tried that. I uninstalled Rstudio. I restarted my computer numerous times. I used " " instead of "_" too. The same result. Nothing has worked so far. No device works. "png" or "jpeg". – mhonda Mar 12 '18 at 20:57
  • It is not limited to ggplot2 extensions. I cannot save plot using a simple plot function and "png" function either. I always get the same error. – mhonda Mar 12 '18 at 21:46
1

I had the same issue and realized I needed to update my directory path..

leo
  • 11
  • 1
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/31481226) – krfurlong Apr 12 '22 at 17:07