簡體   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