[英]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.