编织超过一页的子图或带字幕的子标题? [英] Subfigures or Subcaptions with knitr that span more than one page?

查看:107
本文介绍了编织超过一页的子图或带字幕的子标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此帖子非常相似.但是,我的图块超出了一页的显示范围,导致在按下编织"后,其余的图块被切除.有谁知道如何通过在下一页上绘制其他图来解决这个问题?

Very similar to this post. However I have more plots than that fit on one page causing the remaining plots to be cut off after pressing Knit. Does any one know how to solve this by plotting the other plots on the next page?

title: 'title'
author: "--"
date: "`r Sys.Date()`"
output: pdf_document
header-includes:
  - \usepackage{subfig}
  - \usepackage{float}

## To make the example more reproducible ##

```{r echo=FALSE, message=FALSE}
knitr::opts_chunk$set(fig.width=6, fig.height=9, fig.show="hold", 
hightligh=TRUE, warnings=TRUE, error=FALSE, cache=FALSE, echo=FALSE, 
dpi=100)

library(ggplot2)

```

```{r test, fig.cap='A collection of figs', fig.subcap= "-", out.width="49%", fig.asp=1, fig.ncol = 2, fig.show = 
"asis", fig.align="center"}

       for (ii in 1:10) {
       qplot(1:3, 1:3, main=ii)
       }

```

## this part is only to resemble the answer by *Michael Harper* in the post mentioned before and should be uncommented to replicate (google maps API needed) ## 

# ```{r}
# locations <- c("Southampton, UK", "London, UK", "Bristol, UK", 
# "Birmingham, UK", "Liverpool, UK", "Southampton, UK", "London, UK", 
# "Bristol, UK", "Birmingham, UK", "Liverpool, UK") 
# ggmap::register_google(key = "....")
# ```

# ```{r fig-sub-2, fig.cap='A collection of maps', fig.subcap= locations, 
# out.width='.49\\linewidth', fig.asp=1, fig.ncol = 2}
# library(ggmap)
# lapply(locations, function(x) 
# ggmap(get_map(x))
# )
# ```

来自 Michael Harper 的答案与相同帖子使我走得很远,但并没有说出地块数量超过一页上的地块数量.

The answer from Michael Harper in the same post got me quite far but not to the point that the number of plots exceed the number of plots that fit on one page.

输出上述代码,仅显示10个地块中的6个

推荐答案

为完整起见,我将展示如何使用@MichaelHarper发布的解决方案来解决问题

For completeness I will show how to solve the problem with the solution from @MichaelHarper posted here.

---
title: "tmp"
author: "Kevin Ouwerkerk"
date: "4-5-2020"
output: pdf_document
header-includes:
  - \usepackage{subfig}
  - \usepackage{float}

---

```{r echo=FALSE, message=FALSE}
library(ggplot2)

knitr::opts_chunk$set(warnings=TRUE, error=FALSE, cache=FALSE, echo=FALSE, 
dpi=100)

plots <- list()
```

```{r test}
   for (ii in 1:10) {
   plots[[ii]] <- qplot(1:3, 1:3, main=ii)
   }
```

```{r split-function}
knitr::opts_chunk$set(echo = FALSE)

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"}

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

```

如果标题很长并且出现错误Error in dirname(name) : path too long,则可以在cat函数中text参数后面的代码块名称中删除{{caption}}

If you have a very long caption and you get the error Error in dirname(name) : path too long you can remove {{caption}} in the code chunk name right after the text argument in the cat function

这篇关于编织超过一页的子图或带字幕的子标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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