0

I have been trying to automatically save my plots in single rows of 5 inside a folder. Weirdly, I can see the plots being created in the RStudio GUI and they are also saved as files in my folder but when I open them its just a white page. My data looks something like this:

I think this thread describes a similar problem with no solution: Par() function in R,plot not shown

Also my coding experience is limited to one uni course I didn't finish and some training with an app. I know there are probably 100 better ways to create my graphs but those 15 lines already took me 4 days.

for (i in 1:ncol(data_cold)){
  model <- (aov(data_cold[[i]] ~ data_cold$test))
  df <- df.residual(model)
  MSerror <- deviance(model)/df
  out <- (LSD.test(data_cold[[i]],data_cold$test,df,MSerror))
  mytitle = paste("Seed lot", i)

  if (i == 1 || i == 6 || i == 11 || i == 16 || i == 21 || i == 26 || i == 31 || i == 36 ) {
     par(mfrow = c(1,5))
     plot(out, main = mytitle, ylim = c(0, 110), ylab = "Germination / Emergence %")
     } else {
     plot(out, main = mytitle, yaxt = "n")
     }
  if (i == 5 || i == 10 || i == 15 || i == 20 || i == 25 || i == 30 || i == 35 || i == 39 ) {
     mypath <- file.path("C:", "Users",..., "R_Plots", "cold_plots",paste("cold_plot_", i, ".png", sep = ""))
     png(file = mypath)
     dev.off()
     }
}
  • 3
    `png` opens a file device, `dev.off` closes the device. You should call `plot` when the file device is open, if you want to plot to file. – Roland Aug 15 '22 at 09:17

1 Answers1

0

try this

for (i in 1:ncol(data_cold)){
 model <- (aov(data_cold[[i]] ~ data_cold$test))
 df <- df.residual(model)
 MSerror <- deviance(model)/df
 out <- (LSD.test(data_cold[[i]],data_cold$test,df,MSerror))
 mytitle = paste("Seed lot", i)
  if (i == 5 || i == 10 || i == 15 || i == 20 || i == 25 || i == 30 || i == 35 || i == 39 ) {
     mypath <- file.path("C:", "Users",..., "R_Plots", "cold_plots",paste("cold_plot_", i, ".png", sep = ""))
     }
 png(file = mypath)
 if (i == 1 || i == 6 || i == 11 || i == 16 || i == 21 || i == 26 || i == 31 || i == 36 ) {
     par(mfrow = c(1,5))
     plot(out, main = mytitle, ylim = c(0, 110), ylab = "Germination / Emergence %")
     } else {
     plot(out, main = mytitle, yaxt = "n")
     }`enter code here`
 dev.off()
}
badgorilla
  • 96
  • 3