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

1

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!

Vanhoose answered 11/12, 2021 at 3:21 Comment(2)
Possible duplicate: #62799518Sestina
Thanks for your quick response @VishalA. I tried the method in that question but didn't solve the problem.Vanhoose
I
5

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

Iover answered 11/12, 2021 at 14:55 Comment(8)
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.Vanhoose
@Vanhoose From outside? What is it? An external document with pictures?Iover
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!Vanhoose
@Vanhoose Look, I corrected my answer ;)Iover
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.Vanhoose
@Vanhoose Look to an additionIover
This method works perfectly! Thank you so much for your kind guidance, @manro.Vanhoose
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])Dou

© 2022 - 2024 — McMap. All rights reserved.