1

In this answer https://stackoverflow.com/a/61017301/2554330 I partially answered a problem in resizing subfigures in ggplot2 output using code similar to this:

---
title: "Untitled"
header-includes:
   - \usepackage{subcaption}
output: 
  pdf_document:
    keep_tex: TRUE
---

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

```{r, echo = FALSE, fig.height=3, fig.width=1,fig.subcap=c("first", "second", "third"),fig.cap="Main"}
library(ggplot2)
df <- data.frame(
  x = rnorm(30),
  y = rnorm(30)
)
p1 <- p2 <- p3 <- ggplot(df, aes(x, y)) + geom_point()
p1 + theme(plot.margin = unit(c(1,0,1,0),"in") + theme_get()$plot.margin)
p2 + theme(plot.margin = unit(c(1/2,0,1/2,0),"in") + theme_get()$plot.margin)
p3
```

This produces this output:

screenshot

Notice how there is no bottom margin in the first and second plots, even though I requested equal top and bottom margins. If I look at the actual .pdf files in the figure directory, I can see that they don't contain any margins at all: the file seems to have been cropped to the edge of the bounding box of the ink on the page, so the first plot is (according to Acrobat "Document Properties") 0.83in by 0.83in, the second is 0.83in by 1.83in, and the last one is 0.83in by 2.83in.

What I'd expect to get is to have each of the three plots vertically centred within the 1in by 3in size that I requested. The PDF files should all be that size.

Is there a way to suppress this cropping?

user2554330
  • 37,248
  • 4
  • 43
  • 90
  • sorry, it's a bit unclear what you want to get - want to get the plots aligned in some way? Maybe best and safest to create a combined plot *first*, with `cowplot`or `patchwork`, then knitting it. – tjebo Apr 04 '20 at 14:57
  • 1
    @Tjebo, I've added some text to explain what I want. I don't want a combined plot, because I want to use LaTeX subfigure environments. – user2554330 Apr 04 '20 at 15:04

1 Answers1

2

After looking through the knitr and rmarkdown source code for a while, I've found the answer. The rmarkdown::pdf_document output function has an argument fig_crop that defaults to TRUE. Setting it to FALSE suppresses cropping of figures in the whole document. So all I need to do in this example is change my header YAML to include

output: 
  pdf_document:
    fig_crop: FALSE

and the problem is solved. As far as I know there is no chunk-level option to change this: all figures are cropped, or none are.

user2554330
  • 37,248
  • 4
  • 43
  • 90
  • Was coming across a very similar problem and thinking back to this here. only now found this related thread: https://stackoverflow.com/questions/43195871/adjusting-figure-margins-in-rmarkdown – tjebo Apr 16 '20 at 17:40