Search code examples
rpdflatexr-markdownknitr

How to center figures in one column across multiple rows generated by a R chunk while knitting to a PDF?


I am trying to center two figures in a column across two rows, but the following code is not giving the desired output when written in a .Rmd file and knitted to generate a PDF output:

---
title: "Stack the figures in two rows"
output:
  pdf_document:
    extra_dependencies: "subfig"
---


```{r stack, out.width="50%", fig.cap="FIGS", fig.subcap=c("A", "B"), fig.nrow=2, fig.ncol=1, fig.align="center", echo=FALSE}
plot(rnorm(10), rnorm(10))
plot(rnorm(10), rnorm(10))
```

The code generates the output:

enter image description here

How do I center the image in the top row like the one in the bottom row? Is there a way to do this without using par?


Solution

  • Here is a suggestion using par

    ```{r stack, fig.cap="FIGS", fig.height=6,fig.align = 'center',echo=FALSE}
    par(mfrow=c(2,1))
    plot(rnorm(10), rnorm(10), sub="(a) A")    
    plot(rnorm(10), rnorm(10), sub="(b) B")
    ```
    

    enter image description here