在R中保存几个pdf文件时出现问题

时间:2012-10-29 00:42:15

标签: r pdf save

我正在尝试保存在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文件吗?如果我“手动”它,它的工作原理......任何想法?

1 个答案:

答案 0 :(得分: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()

}