0

I have an R script, which creates a plot and then saves it using ggsave. If I run each command in the R script one by one, in RGui, they all work and ggsave works too.

Similarly, if I submit each command at R prompt one by one, in the MS DOS command window, they all work and ggsave works too.

But If I do a >R CMD BATCH myscript.R, no file is saved by ggsave. I have tried to save a .png file, a .jpg file, or a .pdf file. In all 3 cases, the file did not get created.

However a RPlots.pdf does get created automatically. Now I can always convert this Rplots.pdf to a .png file, in my C# asp.net code, but I do not want to lose any quality aka resolution and waste time either. I am not sure if I will lose any quality aka resolution during such conversion. It will be better to generate the correct file upfront.

here is the code from the R script

library(stats)  
library(ggplot2)  
getwd()  
setwd("C:\\Users\\..................")  
mydatadata <- read.csv(file = "boxplot_test_data.csv", header = TRUE, sep = ",")  
ggplot(mydata, aes(y=Y, x=X, fill=fillFactor)) +     
geom_boxplot(outlier.color=NA) + geom_point(aes(size=AXI, shape=FA), color="gold", position=position_jitter(width=0.1, height=0))  
ggsave("myggplot2.png")  

# I tried this approach too. No success either.
#figure1<-last_plot()    

# I tried this approach too. No success either.  
#ggsave(figure1, file="myggplot2.png") 
MrFlick
  • 195,160
  • 17
  • 277
  • 295
Diana4
  • 53
  • 1
  • 2
  • 9
  • Hi I modified my code to include print() statement. But it had no effect. Here is the new code.. library(stats) library(ggplot2) getwd() setwd("C:\\Users\\..................") mydatadata <- read.csv(file = "boxplot_test_data.csv", header = TRUE, sep = ",") png("myggplot2.png") plt<-ggplot(mydata, aes(y=Y, x=X, fill=fillFactor)) + geom_boxplot(outlier.color=NA) + geom_point(aes(size=AXI, shape=FA), color="gold", position=position_jitter(width=0.1, height=0)) #ggsave("myggplot2.png") print(plt) – Diana4 Jul 05 '17 at 23:41
  • I uploaded my new code...Is it legible.? – Diana4 Jul 05 '17 at 23:46
  • Can you please give me the final code. I still could not get it to work. – Diana4 Jul 06 '17 at 00:01
  • Here is the code I have tried library(stats) library(ggplot2) getwd() setwd("C:\\Users\\..................") mydatadata <- read.csv(file = "boxplot_test_data.csv", header = TRUE, sep = ",") plt<-ggplot(mydata, aes(y=Y, x=X, fill=fillFactor)) + geom_boxplot(outlier.color=NA) + geom_point(aes(size=AXI, shape=FA), color="gold", position=position_jitter(width=0.1, height=0)) print(plt) ggsave("myggplot2.png") dev.off() – Diana4 Jul 06 '17 at 00:30
  • Never mind about the print. Just use `ggsave("myggplot2.png", plt)` – MrFlick Jul 06 '17 at 00:34
  • Still no .png file. I do get the Rplots.pdf file, without fail library(stats) library(ggplot2) getwd() mydata <- read.csv(file = "boxplot_test_data.csv", header = TRUE, sep = ",") plt<-ggplot(mydata, aes(y=Y, x=X, fill=FillFactor)) + geom_boxplot(outlier.color=NA) + geom_point(aes(size=AXI, shape=FA), color="gold", position=position_jitter(width=0.1, height=0)) ggsave("myggplot2.png", plt) dev.off() – Diana4 Jul 06 '17 at 00:45
  • Actually I take that back. The .png file did get created. But for some odd reason, it put a shortcut in my working folder and put the actual file in my recycle bin. Basically deleted it. I had to restore it from the recycle Bin to view it. very strange. – Diana4 Jul 06 '17 at 00:51

0 Answers0