如何使用Knitr在代码块内的子图之间插入分页符

时间:2018-10-31 13:41:33

标签: r latex r-markdown knitr pdflatex

我有以下R arkdown代码:

---
output:
  pdf_document: default
  keep_tex: yes
header-includes: 
  - \usepackage{subfig}
---


```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = F)
library(ggplot2)
library(knitr)
```
## Including Plots

You can also embed plots, for example:
```{r pressure, echo=FALSE, fig.width = 20, fig.height=10, fig.cap=LETTERS[1:3], fig.subcap=LETTERS[1:5], fig.ncol=2,  out.width='0.4\\linewidth'}
for (x in 1:3) {
  df <- data.frame(a = runif(50))
  p <- ggplot(df, aes(x=a)) + geom_histogram(bins=10)
  print(p)
  df <- data.frame(a = runif(50))
  p <- ggplot(df, aes(x=a)) + geom_histogram(bins=10)
  print(p)
  df <- data.frame(a = runif(50))
  p <- ggplot(df, aes(x=a)) + geom_histogram(bins=10)
  print(p)
  df <- data.frame(a = runif(50))
  p <- ggplot(df, aes(x=a)) + geom_histogram(bins=10)
  print(p)
  df <- data.frame(a = runif(50))
  p <- ggplot(df, aes(x=a)) + geom_histogram(bins=10)
  print(p)
  cat('\n\\pagebreak\n')
}

我想要三个不同的图形,每个图形包含五个子图。我试图在第五个子图上打破这个数字,但这是行不通的。

1 个答案:

答案 0 :(得分:2)

fig.cap旨在采用单个参数,因此,我们将必须以编程方式为所需的三个图分别生成新的代码块。作为explained here,我们可以使用knit_expand函数。我已经修改了提供的示例,以包括子图标题和从列表中打印图的方法。

我已经编写了函数splitSubFig,该函数将限制每个图的子图数量。如果我们超过这个数字,它将创建一个新页面。注意,您将需要在块标题中使用results="asis"来创建图形:

---
output:
  pdf_document: default
header-includes: 
  - \usepackage{subfig}
---


```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = F)

library(ggplot2)
library(knitr)

# Function to create a separate chunk to generate a plot for each item in list
splitSubFig <- function(plots, caption, maxlength) {

  knitr::opts_knit$set(progress = FALSE, verbose = FALSE)

  n <- length(plots)
  numPages <- ceiling(n / maxlength)
  splitPlots <- split(plots, rep(1:ceiling(n/maxlength), each=maxlength)[1:n])

  for (page in 1:numPages) {

    cat(
      knit(text=knit_expand(
        text=(
          "```{r {{caption}}{{page}}, fig.cap='{{caption}}: {{page}}', fig.subcap=LETTERS[1:length(splitPlots[page])],  out.width='8cm', fig.ncol=2, echo = FALSE, message = FALSE}
for(i in splitPlots[page]){

for(plots in i){
 print(plots)
}

}
```"),
caption = caption,
page = LETTERS[page])))
  }
}

```

```{r, results="asis"}

plots <- list(ggplot(data.frame(a = runif(50)), aes(x=a)) + geom_histogram(bins=10, fill = "red"),
             ggplot(data.frame(a = runif(50)), aes(x=a)) + geom_histogram(bins=10),
             ggplot(data.frame(a = runif(50)), aes(x=a)) + geom_histogram(bins=10),
             ggplot(data.frame(a = runif(50)), aes(x=a)) + geom_histogram(bins=10),
             ggplot(data.frame(a = runif(50)), aes(x=a)) + geom_histogram(bins=10),
             ggplot(data.frame(a = runif(50)), aes(x=a)) + geom_histogram(bins=10, fill = "blue"),
             ggplot(data.frame(a = runif(50)), aes(x=a)) + geom_histogram(bins=10),
             ggplot(data.frame(a = runif(50)), aes(x=a)) + geom_histogram(bins=10),
             ggplot(data.frame(a = runif(50)), aes(x=a)) + geom_histogram(bins=10),
             ggplot(data.frame(a = runif(50)), aes(x=a)) + geom_histogram(bins=10))

splitSubFig(plots, caption = "Your Caption", maxlength = 6)
```

enter image description here

此功能肯定可以改进,但是应该可以满足您的需求!