alco
alco

Reputation: 51

Problems saving several pdf files in R

I'm trying to save several xyplots created with a "for" loop in R and I'm not able to get complete pdf files (all files have the same size and I can't open them) if I execute the following loop:

for (i in 1:length(gases.names)) {
   # Set ylim;
   r_y <- round(range(ratio.cal[,i][ratio.cal[,i]<999], na.rm = T), digits = 1);
   r_y <- c(r_y[1]-0.1, r_y[2]+0.1);

   outputfile <- paste (path, "/cal_ratio_",gases.names[i], ".pdf", sep="");
   dev.new();
   xyplot(ratio.cal[,i] ~ data.GC.all$data.time, groups = data.vial, panel = 
      panel.superpose, xlab = "Date", ylab = gases.names[i], xaxt="n", ylim = r_y);
   savePlot(filename = outputfile, type = 'pdf', device = dev.cur());
   dev.off();
}

(a previous version was using trellis.device() instead of dev.new() + savePlot())

Do you know why I can't get good pdf files? If I do it "manually" it works... Any idea?

Upvotes: 3

Views: 404

Answers (1)

mnel
mnel

Reputation: 115382

use pdf directly

for (i in seq_along(gases.names)) {
  # Set ylim
  r_y <- round(range(ratio.cal[,i][ratio.cal[,i]<999], na.rm = T), digits = 1)
  r_y <- c(r_y[1]-0.1, r_y[2]+0.1)

   outputfile <- paste (path, "/cal_ratio_",gases.names[i], ".pdf", sep="")
   pdf(file = outputfile, width = 7, height = 7)
   print(xyplot(ratio.cal[,i] ~ data.GC.all$data.time, groups = data.vial, 
                 panel =  panel.superpose, xlab = "Date", ylab = gases.names[i], 
                 xaxt="n", ylim = r_y))

  dev.off()

}

Upvotes: 5

Related Questions