[英]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]]
lattice
和ggplot2
图形都是可分配的。 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.