如何使用knitr在代码块内的子图中插入分页符 [英] How to insert page break among subfigures inside a code chunk with knitr

查看:14
本文介绍了如何使用knitr在代码块内的子图中插入分页符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 R arkdown 代码:

---输出:pdf_document:默认keep_tex: 是的标头包括:- usepackage{子图}---```{r 设置,包括=FALSE}knitr::opts_chunk$set(echo = F)图书馆(ggplot2)图书馆(编织者)```## 包括地块您还可以嵌入绘图,例如:```{r 压力,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)打印(p)df <- data.frame(a = runif(50))p <- ggplot(df, aes(x=a)) + geom_histogram(bins=10)打印(p)df <- data.frame(a = runif(50))p <- ggplot(df, aes(x=a)) + geom_histogram(bins=10)打印(p)df <- data.frame(a = runif(50))p <- ggplot(df, aes(x=a)) + geom_histogram(bins=10)打印(p)df <- data.frame(a = runif(50))p <- ggplot(df, aes(x=a)) + geom_histogram(bins=10)打印(p)cat('
\分页符
')}

我想要三个不同的图形,每个图形包含五个子图形.我试图打破第五个子图的数字,但它不起作用.

解决方案

fig.cap 设计为采用单个参数,因此我们必须以编程方式为每个参数生成新的代码块你想要的三个地块.作为

这个功能当然可以改进,但应该能满足你的需要!

I have the following R arkdown code:

---
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('
\pagebreak
')
}

I'd like to have three different figures, each containing five subfigures. I try to break the figure at the fifth subfigure, but it does not work.

解决方案

fig.cap is designed to take a single argument, and therefore we are going to have to programmatically generate new code chunks for each of the three plots you want. As explained here, we can use the knit_expand function. I have amended the example provided to include the subfigure captions, and a way to print plots from a list.

I have written the function splitSubFig which will limit the number of subfigures per plot. If we go over this number, it will create a new page. Note, you will need to use the results="asis" in the chunk header creating the figures:

---
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)
```

This function could certainly be improved, but it should be good for what you need!

这篇关于如何使用knitr在代码块内的子图中插入分页符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆