gpt4 book ai didi

r - 使用 Sweave 的 for 循环中的多个绘图

转载 作者:行者123 更新时间:2023-12-04 14:00:26 25 4
gpt4 key购买 nike

我在 Sweave 中的部分:

<<fig=TRUE,echo=FALSE>>=
for(i in 1:10) {
plot(rep(i,10))
dev.new()
}
@

在生成的 pdf 中,我只得到一个图(从第一次迭代开始)。我想打印所有 10 个图。我究竟做错了什么?我尝试替换 dev.new()frame()plot.new()但什么也没发生。

最佳答案

正如@rawr 建议的那样,最简单的解决方案是切换到 knitr (真的没有理由不这样做!)并输入 fig.keep="all"在您的代码块选项中(如果您切换到 knitr 您不再需要 fig=TRUE ...包括数字自动工作,fig.keep="none"fig=FALSE 的类似物)

或者,如果您想坚持使用 Vanilla Sweave,请查看 Sweave manual页。 17:

A.9 Creating several figures from one figure chunk does not work

Consider that you want to create several graphs in a loop similar to


<<fig=TRUE>>
for (i in 1:4) plot(rnorm(100)+i)
@

This will currently not work, because Sweave allows only one graph per figure chunk. The simple reason is that Sweave opens a postscript device before executing the code and closes it afterwards. If you need to plot in a loop, you have to program it along the lines of


<<results=tex,echo=FALSE>>=
for(i in 1:4){
file=paste("myfile", i, ".eps", sep="")
postscript(file=file, paper="special", width=6, height=6)
plot(rnorm(100)+i)
dev.off()
cat("\\includegraphics{", file, "}\n\n", sep="")
}
@

关于r - 使用 Sweave 的 for 循环中的多个绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24130725/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com