How to add captions of two or more figures in one chunk within PDF output?

I am working with a large dataset, and have generated several figures with "StripPlot" method. Certainly the best way is to be able to add captions for each figure in the chunk, but not to export figures first, then import again with markdown syntax like !FIG. X. Captions of the 1st figure., !FIG. Y. Figure Two's caption.

After review some posts in SO and RStudio community, I am not able to find an answer. Currently I find two possible solutions:

  1. Add "fig.cap=c("FIG. X. Captions of the 1st figure.", "FIG. Y. Figure Y caption", "FIG. Z. Figure Z caption")" in r chunk, but looks like preceding captions are overlapped by following figures, thus only the caption of the last one is kept.
  2. Use "captioner" package, but after reading the examples, I really don't know how to use it.

There might be other solutions like use ggplot2, while I have to admit that is pretty new for me, and I will have to re-write those plot codes.

So I would like to know if there is an easy solution?

P.S. In other words, is there a way to add one or several new/empty line(s) after on figure?

Following is a sample code:

---
title: "How to add captions of two or more figures in one chunk with PDF output?"
author: "test"
date: "10/11/2020"
fontsize: 10pt
geometry: margin=0.75in
output: 
  pdf_document:
    includes:
      in_header: C:/Elekta/VersaHD/header.tex
      number_sectins: yes
      fig_caption: true
    keep_tex: yes
  html_document:
    df_print: paged
    self_contained: no
header-includes: \usepackage{caption, dcolumn}
---

\captionsetup[table]{labelformat=empty, justification=raggedright,singlelinecheck=false}

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

### "Captions of two or more figures in one chunk with PDF output"


```{r, echo=FALSE, fig.asp=0.7, fig.show ='hold', dev=c('pdf', 'png'), message=FALSE, warning=FALSE, out.width='80%', paged.print=FALSE, crop=TRUE, fig.cap=c("FIG. X. Captions of the 1st figure.", "FIG. Y. Figure Y caption", "FIG. Z. Figure Z caption")}
x <- rnorm(10)
plot(x)

y <- rcauchy(10)
plot(y)

z <- runif(10)
plot(z)

Slightly related but I guess there is no harm sharing it : Did you considered subfigure ?

I think the issue you have is because you set fig.show ='hold'. If you try to remove this should works with one caption per figure.

Wow, it's so simple, it works now. Thanks a lot!

1 Like

If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

After I remove the "fig.show ='hold'" for the chunk option, then it works, here is another sample of R chunk.

Thanks for your help!



x <- rnorm(10)
plot(x)

y <- rcauchy(10)
plot(y)

z <- runif(10)
plot(z)

xx <- rnorm(10)
plot(xx)

yy <- rcauchy(10)
plot(yy)

zz <- runif(10)
plot(zz)
1 Like