在 for 循环中打印为 PDF
我想循环绘图并将绘图结果放入 PDF
中。
以下代码用于执行此操作:
它的作用是循环 3 次并根据 iris 数据集绘制 3 个不同的图。然后它应该将其保存到C:/
驱动器。 PDF
文件已创建,但已损坏。
for(i in 1:3){
pdf(paste("c:/", i, ".pdf", sep=""))
plot(cbind(iris[1], iris[i]))
dev.off()
}
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()
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要在设备上绘制点阵图,需要打印通过调用点阵图形函数之一生成的对象。通常,在交互使用中,如果未分配,R 会自动打印对象。然而,在循环中,自动打印不起作用,因此必须安排要打印的对象,通常是将其包装在
print()
中。这是一个示例(请原谅我滥用公式符号;-):
这会在
pdf
设备上生成三个图。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 ;-):
This produces the three plots on a
pdf
device.包含“c:/”的文件名在您的操作系统上是有效的文件名吗?这看起来像是您在调用
pdf
之前要设置的工作目录的一部分。我收到一条错误消息,告诉我无法打开该文件:如果我从文件名中删除“c:/”位,则会正确生成三个 PDF。另外,如果将
dev.off()
移出 for 循环,您将获得一个包含三页的 PDF,而不是三个 PDF。可能是你想要的,也可能不是你想要的......Is a file name that contains "c:/" a valid file name on your OS? That looks like part of the working directory that you'd want to set before calling
pdf
. I get an error telling me it can't open that file:If I drop the "c:/" bit from the file name, three PDFs are generated properly. Also, if you move the
dev.off()
outside of the for loop, you'll get a single PDF with three pages instead of three PDFs. May or may not be what you want...