如何在R Markdown中的列表中打印所有记录的图 [英] How to print all recorded plots in a list in R Markdown
问题描述
这是我在StackOverflow上遇到的第一个问题.如果我做错了事,请告诉我以改善下一个问题.
this is my first question at StackOverflow. If I am doing something wrong let me know to improve the next questions.
我从R Markdown开始,并且在打印带有for循环的记录图时遇到一些麻烦.好吧,在我运行Rmarkdown文件之前,我运行了R函数,该函数生成了几个数据框和图表的列表.
I am starting with R Markdown and I have some troubles to print recorded plots with a for loop. Well, before I run Rmarkdown file, I run R function that generate a list of several data frames and plots.
为了简化起见,我在此处仅列出了绘图对象,并简化了该列表的可重现示例.
To make it easier I put here a simplified reproducible example of this list just with plot objects.
x <- c(1,2,3,4,5)
y <- c(1,2,3,4,5)
plot(x,y)
abline(h=1)
p1.1 <- recordPlot()
plot(x,y)
abline(h=3)
p1.2 <- recordPlot()
plot(x,y)
abline(h=4)
p2.1 <- recordPlot()
plot(x,y)
abline(h=6)
p2.2 <- recordPlot()
lista<-NULL
lista["p1.1"] <- list(p1.1)
lista["p1.2"] <- list(p1.2)
lista["p2.1"] <- list(p2.1)
lista["p2.2"] <- list(p2.2)
save(new_list, file = "Data.RData")
然后我将这个列表加载到R Markdown文件中,如下所示:
Then I load this list in R Markdown file like this:
```{r setup}
knitr::opts_chunk$set(echo = TRUE,fig.keep = "all")
load("Data.RData")```
我尝试像这样打印这些图:
And I try to print this plots like this:
```{r,echo=FALSE, results='asis',fig.keep='all'}
for (i in c(1,2)){
for(j in c(1,2)){
print(lista[[paste(paste("p",i,sep=""),j,sep=".")]])
}
}```
当我运行Knitr来获取HTML文件时,结果是只显示了for循环的最后一个图.
When I run Knitr to obtain HTML file the result is that just the last plot of the for loop is shown.
我尝试用lapply代替for循环,但是对我来说不起作用.我也尝试使用replayPlot函数print(replayPlot(lista[[paste(paste(paste("p",i,sep=""),".",sep=""),j,sep="")]]))
并得到相同的结果.
I have tried use lapply instead of for loop, but it did not work for me. Also I tried to use replayPlot function print(replayPlot(lista[[paste(paste(paste("p",i,sep=""),".",sep=""),j,sep="")]]))
with the same result.
有什么方法可以解决此问题而无需修改以前生成绘图列表的R函数?
Is there any way to solve this without modifying the previous R function that generate the plot list?
谢谢您的回答.
推荐答案
使用的语法对于ggplot
对象的列表可能是可以的,但是对于基本的plot
对象,则需要调用plot.new()
列表中的下一个图不会覆盖前一个图:
The syntax you use would probably be OK with a list of ggplot
objects, but with base plot
objects, you need to call plot.new()
so that the next plot in the list doesn't overwrite the previous one:
---
title: "test"
output: html_document
---
## Define plots
```{r}
x <- c(1,2,3,4,5)
y <- c(1,2,3,4,5)
plot.new()
plot(x,y)
abline(h=1)
p1.1 <- recordPlot()
plot.new()
plot(x,y)
abline(h=3)
p1.2 <- recordPlot()
plot.new()
plot(x,y)
abline(h=4)
p2.1 <- recordPlot()
plot.new()
plot(x,y)
abline(h=6)
p2.2 <- recordPlot()
lista<-NULL
lista["p1.1"] <- list(p1.1)
lista["p1.2"] <- list(p1.2)
lista["p2.1"] <- list(p2.1)
lista["p2.2"] <- list(p2.2)
```
# Print list
```{r,echo=F}
for (i in c(1,2)){
for(j in c(1,2)){
# Needed to avoid overwrite
plot.new()
print(lista[[paste(paste("p",i,sep=""),j,sep=".")]])
}
}
```
请注意,您可以简化循环的语法:
Note that you can simplify the syntax of the loops:
for(p in lista) {
plot.new()
print(p)
}
这篇关于如何在R Markdown中的列表中打印所有记录的图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!