繁体   English   中英

在针织机上绘制三分之一的图

[英]plot one in three plots on knitr

我想遍历一个脚本并制作许多图,但只在最后的图上进行减价
因此,我试图做的是将许多地块保存为地块列表,但不将其发布到降价处。
第二步是遍历列表,并在三个图中绘制一个,但由于某种原因,我只能得到最后一个图。

#+ setup, include=FALSE

library(knitr)
opts_chunk$set(fig.path = 'figure/silk-', fig.width = 10, fig.height = 10)

#' Make a list of plots.
#' 
#/* do not show in Markdown
index = 1
plots<-list()
for (let in letters)
{
 plot(c(index:100))
 assign(let,recordPlot())
 plot.new()
 plots[index]<-(let)
 index=index+1
}
#*/go through list of plots and plot then to markdown file
for (p in seq(from = 1, to = length(plots), by =3))
{

    print(get(plots[[p]]))

} 

作为其他编程语言的遗迹,您的代码中存在一些错误:

  • 根本不使用assign 被允许使用分配的人将不会使用它。
  • plot.new()创建一个空白页。 忽略
  • 不要使用get 它已在S-Plus中使用,但如今已无济于事。
  • 对于列表,请使用[[ ,例如plots[[index]]
  • 最重要:您想要的东西是有道理的,但是标准图形(例如绘图)非常不适合此操作,因为它是在考虑操作而不是分配的情况下构建的。 latticeggplot2图形都是可分配的。
  • 在示例中,我将lapply用作标准R实践的演示。 在这种情况下,for循环不会变慢,因为绘制花费了大部分时间。
  • 为此,最好使用构面或面板,而不要使用许多单独的图。

`

library(knitr)
library(lattice)
# Make a list of plots.
# do not show in Markdown
plots = lapply(letters[1:3],
    function(letter) {xyplot(rnorm(100)~rnorm(100), main=letter)})

# print can use a list (not useful in this case)    
print(plots)

# go through list of plots and plot then to markdown file
# This only makes sense if you do some paging in between. 
for (p in seq(from = 1, to = length(plots), by =3))
{
  print(plots[[p]])
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM