overly-aggressive figure cropping with rmarkdown-generated PDFs only on mac

Viewed 100

I'm compiling the following rmarkdown document in Rstudio:

---
title: "Untitled"
author: "Richard D. Morey"
date: '2022-07-24'
output: 
  pdf_document:
    keep_tex: false
    fig_crop: true
---

```{r setup, include=FALSE}
library(ggplot2)
library(patchwork)
knitr::opts_chunk$set(echo = FALSE, dev=c('pdf', 'png'))
```


```{r pressure, echo=FALSE}
ggplot(data=mtcars, aes(x=mpg,y=disp)) +
  geom_point() +
  annotate(
    'text',x=20,y=200,label='hi there 0', color = 'red'
    ) -> gg0
ggplot(data=mtcars, aes(x=mpg,y=disp)) +
  geom_point() +
  annotate(
    'text',x=20,y=200,label='hi there 1', color = 'blue'
    )  -> gg1
```

```{r}
gg0
```

```{r}
gg1
```

```{r}
gg0 + gg1
```

Notice that I've added the fig_crop:true option (which is the default). When I compile it on my Mac, the first two figures appear cropped in such a way that the axes are missing. This setup saves the png files as well, which do not suffer from the same problem. I verified this by setting keep_tex: true and inspecting the output files. The PDF looks like this:

PDF output on Mac, cropped aggressively

but the PNG file looks like this:

PNG output on Mac, not cropped, the way it is supposed to look

Additionally, the {patchwork} output from gg0+gg1 only shows the left panel instead of both panels, and again agressively cropped so that the axes labels are missing.

Importand things that seem to affect it (or not):

  • The cropping problem also occurs for base R plots.
  • If I swap the orders of pdf and png in device list, so png is first, it will use the png files and the plots look "correct" (but low-res pngs).
  • When I set fig_crop: false the problem goes away.
  • The problem also goes away when I use cairo_pdf as the device rather than pdf.
  • The problem also does not occur when I use ggplot2::ggsave('test.pdf', plot = gg0, device = 'pdf') in the console.
  • The problem remains whether or not I have the {magick} package installed.
  • The problem does not occur on my Windows machine, nor on Rstudio.cloud (Ubuntu).
  • It seems others have experienced the problem (e.g., here)

So it seems to be something unique to {rmarkdown} or {knitr} using the pdf() device on this Mac. Maybe it is be down to some configuration or version of a utility on my Mac, but I don't know where to look.

Here's my session info on the Mac:

sessioninfo::session_info()
─ Session info ─────────────────────────────────────────────────────────
 setting  value                       
 version  R version 4.2.1 (2022-06-23)
 os       macOS Big Sur 11.6.7        
 system   x86_64, darwin17.0          
 ui       RStudio                     
 language (EN)                        
 collate  en_GB.UTF-8                 
 ctype    en_GB.UTF-8                 
 tz       Europe/London               
 date     2022-07-25                  

─ Packages ─────────────────────────────────────────────────────────────
 !  package     * version date       lib source        
    cli           3.0.0   2021-06-30 [2] CRAN (R 4.2.0)
    digest        0.6.27  2020-10-24 [2] CRAN (R 4.2.0)
    evaluate      0.14    2019-05-28 [2] CRAN (R 4.2.0)
    htmltools     0.5.1.1 2021-01-22 [2] CRAN (R 4.2.0)
    knitr         1.33    2021-04-24 [2] CRAN (R 4.2.0)
    renv          0.15.5  2022-05-26 [1] CRAN (R 4.2.0)
 VP rlang         0.4.11  2022-07-12 [2] CRAN (R 4.2.0)
 P  rmarkdown     2.14    2022-04-25 [?] CRAN (R 4.2.0)
    rstudioapi    0.13    2020-11-12 [2] CRAN (R 4.2.0)
    sessioninfo   1.1.1   2018-11-05 [2] CRAN (R 4.2.0)
    withr         2.4.2   2021-04-18 [2] CRAN (R 4.2.0)
 P  xfun          0.31    2022-05-10 [?] CRAN (R 4.2.0)
    yaml          2.2.1   2020-02-01 [2] CRAN (R 4.2.0)

[1] /Users/richard/Downloads/test_crop/renv/library/R-4.2/x86_64-apple-darwin17.0
[2] /Library/Frameworks/R.framework/Versions/4.2/Resources/library

 V ── Loaded and on-disk version mismatch.
 P ── Loaded and on-disk path mismatch.
  

I know there are sometimes issues with PDFs and fonts not showing, but that does not appear to be case here because I've annotated the plot with text and that text shows up.

What is causing the issue, and how can I resolve that root cause?

Updated to add: I tried using knitr 1.39 and the development version (1.39.6) with the same result.

1 Answers

The reason for this was that my Mac had a very old version of ghostscript installed that was being preferred to the newer version. The old version that was causing the problem was:

GPL Ghostscript 9.22 (2017-10-04)
Copyright (C) 2017 Artifex Software, Inc.  All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.

(You can check this by running the gs command from the terminal.)

When I got rid of the old version and installed GPL Ghostscript 9.55.0 (2021-09-27), the problem went away.

I tracked this down by noticing that knitr uses pdfcrop internally, which in turn uses ghostscript. Comparing ghostscript versions across computers led me to the answer.

Related