JoelKuiper
JoelKuiper

Reputation: 4720

Why does the plot size differ between docx and html in rmarkdown::render?

When trying to plot a figure from Rmd to both Word and Docx the plot size differs, where the docx version trims the edges. Is there any way to prevent this?

This is an Rmd that somewhat minimally reproduces this. (in other plots the effect is much more extreme but requires more code to reproduce)

```{r}
library(gemtc)
example(gemtc)
forest(results)
```

rmarkdown::render("./test.Rmd", output_format="word_document", clean=F)

Word version

rmarkdown::render("./test.Rmd", output_format="html_fragment") HTML version

Note the trimmed CrIon the right hand side.

The plot parameters seem to be different between runs (this is from a different plot):

par(no.readonly = T)

(docx)

## $pin
## [1] 3.76 2.16
## 
## $plt
## [1] 0.164 0.916 0.255 0.795

vs.

(html)

## $pin
## [1] 5.76 3.16
## 
## $plt
## [1] 0.1171429 0.9400000 0.2040000 0.8360000
## 

On the same plot. This causes the edges to be trimmed quite extremely in some cases for Word.

version info

R version 3.4.4 (2018-03-15)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.1 LTS

Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1

locale:
 [1] LC_CTYPE=C.UTF-8           LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] knitr_1.21.1   rmarkdown_1.11 gemtc_0.8-2    coda_0.19-2   

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.0      lattice_0.20-38 digest_0.6.18   truncnorm_1.0-8
 [5] slam_0.1-43     plyr_1.8.4      grid_3.4.4      meta_4.9-3     
 [9] magrittr_1.5    evaluate_0.12   stringi_1.2.4   tools_3.4.4    
[13] stringr_1.3.1   igraph_1.2.2    xfun_0.4        compiler_3.4.4 
[17] pkgconfig_2.0.2 Rglpk_0.6-3     htmltools_0.3.6

Illustrative example of a more extreme case (same commands as above, run on the same Rmd file, plot comes from the coda package)

HTML version (correct) HTML version (correct) DOCX version (wrong sizes) docx version (wrong sizes)

Upvotes: 10

Views: 1243

Answers (1)

tarleb
tarleb

Reputation: 22544

The cut-off is due to the plotting device (png) being too small. R Markdown uses different defaults for figure width depending on the output format. Knitting to HTML with default options will produce 7 inch wide images, while images in docx output are only 5 inch wide per default. The problematic plots are too wide for the chosen device width, resulting in the unintended cut-off.

You can verify this by setting the figure width to 5 inches for HTML output. The produced images will have the same problems as those in docx:

---
output:
  html_document:
    fig_width: 5
---

```{r}
library(gemtc)
example(gemtc)
forest(results)
```

The fix is therefore to choose a larger figure width, either globally

---
output:
  word_document:
    fig_width: 5.5
---

or for each plot separately:

```{r fig.width=5.5}
library(gemtc)
example(gemtc)
forest(results)
```

Upvotes: 15

Related Questions