rr-markdownofficedown

How to import a figure as its original height/width ratio?


I'm using the officedown package to generate a Word document. May I ask, if I want to import a well-designed figure from the disk, how to control officedown not to change the height/width ratio of the figure?

For example, my original figure looks like this:

enter image description here

However, in the Word document generated by officedown, it looks like this:

enter image description here

May I ask, how to avoid the distortion in officedown? And how to make the width of the figure take the whole line?

My question can be reproduced by the following code:

---
output: officedown::rdocx_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(
    echo = FALSE,
    fig.cap = TRUE,
    message = FALSE,
    warning = FALSE
)
library(officedown)
library(officer)
```

```{r}
knitr::include_graphics("the file path to a figure")
```

Your kind guidance is much appreciated!


Solution

  • I have experimented a little.

    For the output:

    bookdown::word_document2
    

    ... I haven't found any solution.

    But for the

    officedown::rdocx_document
    

    ... construction

    ```{r fig.width=5, fig.height=5}
    knitr::include_graphics("xxx.png") 
    ```
    

    ... works without any problems.


    An addition:

    It should work for your task:

    ```{r}
    library(imager)
    my_pic <- load.image("xxx.png")
    asp_rat <- dim(my_pic)[2]/dim(my_pic)[1] #find our aspect ratio
    
    ```
    
    ```{r fig.asp = asp_rat, fig.height = ??, fig.width = ??} #choose the best for your pic
    knitr::include_graphics("xxx.png") 
    ```
    

    enter image description here

    Look, the aspect ratio is saved. You should only determinate fig.height/width for the each case. I haven't any ideas yet...