【问题标题】:Plotting a loop of plots in Sweave/latex在 Sweave/latex 中绘制一个循环图
【发布时间】:2013-11-30 14:40:27
【问题描述】:

假设我有一个函数

plotSingle <- function(x) {
   plot(x)
}

我想在Sweave 的循环中使用它来生成 20 页,每页有 1 个绘图。例如:

\begin{figure}[htb]
\caption{}
\centering
<<fig=TRUE,echo=FALSE>>=
for(loop in 1:20) {
    plotSingle(loop)
    cat('\\newpage')
}
@
\end{figure}

但是,这只会在 1 页上生成 1 个绘图,而不是我所追求的 20 页上的 20 个绘图。

如何调整上面的Sweave 代码以做我想做的事?

【问题讨论】:

    标签: r plot latex sweave


    【解决方案1】:

    您应该添加plot.new 或其别名frame() 而无需添加到cat('\\newpage')

    <<,fig=TRUE,echo=FALSE>>=
    plotSingle <- function(x) {
       plot.new()   ##  ~ frame()
       plot(0)
    }
    for(i in 1:3) plotSingle(mtcars)
    @
    

    编辑与布局:

    我认为 knitr 在找到相同的情节语句时会进行一些优化。那个

    <<,echo=FALSE>>=
    
    plotSingle <- function(x) {
    
       layout(matrix(1:3,nrow=3))
       plot(0,main=x)          ## the 3 plots statement here are different
       plot(1,main=x)          ## you can just change the title also and it will works
       plot(2,main=x)          ## keep in mind to give knitr different plot statement.
    
    }
    
     for(i in 1:5)  plotSingle(i)  ## you have also to give different iodex here
                                   ## plotSingle(0) will not work for example.
    @
    

    【讨论】:

    • 如果plotSingle 函数实际上绘制了一个layout 绘图,该绘图包含同一设备中的 3 个子绘图?
    • @user2763361 很好。我编辑我的答案。我希望它很清楚。
    猜你喜欢
    • 2012-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-30
    • 1970-01-01
    • 2015-08-29
    • 1970-01-01
    • 2020-11-08
    相关资源
    最近更新 更多