3

我正在尝试保存在 R 中使用“for”循环创建的几个 xyplots,如果执行以下循环,我将无法获得完整的 pdf 文件(所有文件的大小相同,我无法打开它们):

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();
}

(以前的版本使用trellis.device()而不是dev.new() + savePlot()

你知道为什么我不能得到好的 pdf 文件吗?如果我“手动”执行它,它会起作用……知道吗?

4

1 回答 1

5

pdf直接使用

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

}
于 2012-10-29T00:46:33.053 回答