在for循环中打印到PDF [英] Print to PDF in a for loop

查看:187
本文介绍了在for循环中打印到PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想循环一个绘图并将绘图的结果放在 PDF 中。



下面的代码是用来做到这一点的:



这是循环3次,并从虹膜数据集绘制3个不同的图。然后它应该保存到 C:/ 驱动器。 PDF 文件已创建,但已损坏。

  for(i在1:3){
pdf(paste(c:/,i,.pdf,sep =))
plot(cbind(iris [1],iris [i] ))
dev.off()
}


解决方案为了在设备上绘制格子图,需要将通过调用生成的对象打印到一个格子图形函数中。通常,在交互式使用中,如果未分配,R会自动打印对象。但是,在循环中,自动打印不起作用,所以必须安排打印对象,通常是将其打包在 print()中。



下面是一个例子(请原谅我滥用公式符号; - ):

  require (格子)
(i在1:3){
pdf(paste(plot,i,.pdf,sep =))
print(xyplot(iris [,1]〜iris [,i],data = iris))
dev.off()
}

这会在 pdf 设备上生成三个图。


I want to loop over a plot and put the result of the plot in a PDF.

The following code is used to do this:

What this does is loop 3 times and plot 3 different plots from the iris dataset. Then it should save it to the C:/ drive. The PDF files are created, but are corrupted.

for(i in 1:3){
  pdf(paste("c:/", i, ".pdf", sep=""))
  plot(cbind(iris[1], iris[i]))
  dev.off()
}

解决方案

To drawn lattice plots on the device, one needs to print the object produced by a call to one of the lattice graphics functions. Normally, in interactive use, R auto prints objects if not assigned. In loops however, auto printing does not work, so one must arrange for the object to be printed, usually by wrapping it in print().

Here is an example (please excuse my abuse of the formula notation ;-):

require(lattice)
for(i in 1:3) {
    pdf(paste("plot", i, ".pdf", sep = ""))
    print(xyplot(iris[,1] ~ iris[,i], data = iris))
    dev.off()
}

This produces the three plots on a pdf device.

这篇关于在for循环中打印到PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆