Figures generated in RMarkdown when compiling to PDF have axes cut off

In the last 3 days something has happened to RMarkdown. When rendering the default .rmd file produced by RStudio to a PDF file, the plot created has no margins beyond the plot axes and the axis scales and titles are not visible. When rendering to .html, the figures are as they should be. Today I reinstalled R and RStudio and updated all of the libraries (update.packages()) and the problem remains. This first cropped up in a bookdown project so I checked the .png files that bookdown produces and they all have the same problem - no axis scales or titles and the axis margins are at the figure limits.

Any thoughts?

R $version.string
[1] "R version 4.1.3 (2022-03-10)"
RStudio version
[1] ‘2022.2.1.461’

Is this the same issue as in these other ones ?

It is hard to follow with the 3 opened thread.

I answered in Different Rmarkdown to PDF compiler behavior with RMarkown and Bookdown - #2 by cderv

There is a cropping by default with PDF when some tools are present. The fig_crop = TRUE argument as always been there but conditionnal to some tool you may not had before maybe. See function help page

fig_crop
Whether to crop PDF figures with the command pdfcrop. This requires the tools pdfcrop and ghostscript to be installed. By default, fig_crop = TRUE if these two tools are available.

Only on Windows the default behavior may have change some time ago.

Can you set fig_crop = FALSE to see if this fixes it ?

Thanks for all your comments. After many hours of removing/reinstalling R, R Studio, tinytex, Miktex (and removing Miktex again), every library I had, and then sequentially adding my .rmd code until it broke, I discovered an offending extra ] in an R chunk far away from where the errors were being reported, things settled back down to where I was before the problem began. My guess is that the extra ] was wreaking havoc with the chunk options in the next chunk that caused the cropping of the figures. Now, both of the issues (couldn't compile with two out of 12 chapter .rmd files but could if I removed them plus the figure cropping) seem to have resolved. I've been using both rmarkdown, bookdown, and beamer since their inception, and it just goes to show that complacency is never a good attitude to have. Thanks again.

Glad you found a fix for this!

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:

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.