0

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

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.

output form the code above showing only 6 out of 10 plots

Kevin
  • 126
  • 1
  • 6
  • 1
    Looks like I may have already answered your question :) [How to insert page break among subfigures inside a code chunk with knitr](https://stackoverflow.com/questions/53084808/how-to-insert-page-break-among-subfigures-inside-a-code-chunk-with-knitr) – Michael Harper May 14 '20 at 14:44
  • @MichaelHarper Thanks for the link, I tried to use your function to get it to work on my example however it is not showing anything after knitting. Can you maybe show a reproducible example with for my problem ? – Kevin May 18 '20 at 10:08
  • Unfortunately I don't have the time to do that. Potentially worth updating your own question if you are encountering a specific problem. Are you sure you're got the chunk options set correctly i.e. results = "asis"? – Michael Harper May 18 '20 at 14:01
  • That is unfortunate, yes i did set result = "asis". I will update my question with a second part where I use your function aswell. – Kevin May 18 '20 at 14:16
  • @MichaelHarper I managed to get it working for the reproducible example above. I will later add it as answer for completeness. However, I came across another problem that is in my own script I use a really long caption in the function you created but then I get the error `Error in dirname(name) : path too long` which seems strange since it is not actually a path right? – Kevin May 19 '20 at 10:30

1 Answers1

0

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)

```

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

Kevin
  • 126
  • 1
  • 6