6

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!

Xiaochi
  • 65
  • 5
  • 1
    Possible duplicate: https://stackoverflow.com/questions/62799018/maximize-imported-image-on-page-in-officer-officedown-to-word-docx – Vishal A. Dec 11 '21 at 11:02
  • Thanks for your quick response @VishalA. I tried the method in that question but didn't solve the problem. – Xiaochi Dec 11 '21 at 13:51

1 Answers1

3

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...

Patrick
  • 742
  • 7
  • 19
manro
  • 3,529
  • 2
  • 9
  • 22
  • Thanks, @manro. Yes, `fig.width=5` and `fig.height=5` can change the figure size. However, when I import a figure from outside, I don't know the exact height/width ratio of the figure. If I set `fig.width=X` and `fig.height=Y`, the figure could not be imported as it is. – Xiaochi Dec 12 '21 at 08:37
  • @Xiaochi From outside? What is it? An external document with pictures? – manro Dec 12 '21 at 12:02
  • Hello @manro, "from outside" just means the figure is imported from the disk, not generated in the current R environment. To be clearer, I modified the question, please have a look. Thank you very much! – Xiaochi Dec 13 '21 at 01:09
  • @Xiaochi Look, I corrected my answer ;) – manro Dec 13 '21 at 10:50
  • Thanks, @manro.The figure would still be changed. I think `fig.asp=1` means setting `height/width = 1`, which is going to make the figure become a square. – Xiaochi Dec 13 '21 at 22:32
  • @Xiaochi Look to an addition – manro Dec 14 '21 at 13:39
  • 1
    This method works perfectly! Thank you so much for your kind guidance, @manro. – Xiaochi Dec 18 '21 at 07:51
  • If you have access to a Linux system with the `identify` tool installed you can do this without the `imager` package like this: `as.numeric(strsplit(system("identify -format '%wx%h\n' fig/xxx.png", intern = TRUE), split="x")[[1]][1]) / as.numeric(strsplit(system("identify -format '%wx%h\n' xxx.png", intern = TRUE), split="x")[[1]][2])` – Patrick Jul 12 '23 at 07:24