2

I recently tried using officedown to create a .docx report of my document. In my documents I import images from an \images folder in my project directory.

Normally when I knit a document I am able to maximize its position on the page. Does anyone know how to do this using officedown? I have no issues when I run this code in RMarkdown

This is what I get using officedown Here is what I get

This is what I want (notice the image is taking up the whole page) Here is what I want

I have included a reprex below

---
date: "`r Sys.Date()`"
author:
title: "GitHub Example"
output: 
  officedown::rdocx_document
---

```{r setup, include=FALSE, echo = FALSE}
pacman::p_load(readxl, dplyr, apastats, officedown, officer, ggplot2, knitr) # load packages

knitr::opts_chunk$set(fig.align = 'center',
                      fig.cap = TRUE,
                      fig.pos = 'H',
                      fig.path = 'images/',
                      echo = FALSE,
                      warning = FALSE, 
                      message = FALSE,
                      include = TRUE,
                      out.height="100%",  out.width="100%",
                      dpi = 300)

```

```{r}
# Creating a boxplot and saving it in \images directory
plot <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_boxplot()
ggsave("images/plot.png",plot, width=11, height=8.5, dpi=300)

```

## Figures 

Figure \@ref(fig:boxplot) shows a boxplot that is made within the RMarkdown document. 
I want to call in an image saved from a previous R scripts which is saved 
in my `\images` directory shown in Figure \@ref(fig:plot). But notice how it 
does not take up the whole page.

<!---BLOCK_LANDSCAPE_START--->
```{r fig.cap="A boxplot", fig.id = "boxplot"}
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_boxplot()
```
<!---BLOCK_LANDSCAPE_STOP--->


<!---BLOCK_LANDSCAPE_START--->

```{r fig.id="plot", fig.cap="boxplot imported from images folder", echo=FALSE}
knitr::include_graphics("images/plot.png")
``` 

<!---BLOCK_LANDSCAPE_STOP--->
Patrick
  • 915
  • 2
  • 9
  • 26

3 Answers3

4

You can use knitr usual parameters fig.width and fig.height (inches).


---
date: "`r Sys.Date()`"
author:
title: "GitHub Example"
output: 
  officedown::rdocx_document
---

```{r setup, include=FALSE, echo = FALSE}
pacman::p_load(readxl, dplyr, apastats, officedown, officer, ggplot2, knitr) # load packages

knitr::opts_chunk$set(fig.align = 'center',
                      echo = FALSE,
                      warning = FALSE, 
                      message = FALSE,
                      dpi = 300)

```

```{r}
plot <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_boxplot()
```



<!---BLOCK_LANDSCAPE_START--->
```{r fig.cap="A boxplot", fig.id = "boxplot"}
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_boxplot()
```

```{r fig.width=10, fig.height=5, fig.id="plot", fig.cap="boxplot imported from images folder", echo=FALSE}
plot
``` 

<!---BLOCK_LANDSCAPE_STOP--->

enter image description here

David Gohel
  • 9,180
  • 2
  • 16
  • 34
1

Tips for working with plots/figures in Word output:
a) you need to use fig.height and/or fig.width to scale plots/figures;
b) consider using chunk option crop = TRUE with the function hook_pdfcrop() to trim/crop the extra white margin around the plot (see @CL. SO answer here);
c) chunk options: fig.align, fig.pos, out.height, out.width or out.extra are not supported for Word output.

To achieve desired output, you may consider the following changes:

```{r setup, include = FALSE, echo = FALSE}
pacman::p_load(readxl, dplyr, apastats, officedown, officer, ggplot2, knitr) # load packages

knitr::opts_chunk$set(fig.cap = TRUE,
                      fig.path = 'images/',
                      echo = FALSE,
                      warning = FALSE, 
                      message = FALSE,
                      include = TRUE,
                      dpi = 300)
```
```{r}
# Creating a boxplot and saving it in \images directory
plot <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_boxplot()
ggsave("images/plot.png", plot, dpi = 300)
```
```{r fig.id = "plot", fig.cap = "boxplot imported from images folder", fig.height = 6, fig.width = 7.5, echo = FALSE}
knitr::include_graphics("images/plot.png")
```

Does this helps somehow?

Radovan Miletić
  • 2,521
  • 1
  • 7
  • 13
0

After implementing @David Gohel answer for a few projects. I still am envious of the use of out.height and out.width in PDF outputs.

In most of my projects I have resorted to going into File Explorer, right clicking on the figure in question and writing down the images dimensions to calculate its aspect ratio. enter image description here

In a typical officedown project I will only call fig.width so that I can make sure the figure is maximized to the size of the page while maintaining its original aspect ratio.

knitr::include_graphics("images/plot.png")

I worked on trying to implement this automatically which would require reading the images dimensions to set fig.asp. Its not perfect, and I am sure someone else will have a much better and cleaner approach but you can accomplish this using the magick package. This also requires 2 chunks to accomplish the feat (again not optimal)

{r, eval=TRUE, echo=FALSE}
# First chunk to fetch the image size and calculate its aspect ratio
img <- magick::image_read("images/plot1.png") # read the image using the magic library

img.asp <- image_info(img)$height / image_info(img)$width # calculate the figures aspect ratio
{r fig.width=11, fig.asp = img.asp, fig.id="plot", fig.cap="boxplot imported from images folder", echo=FALSE}
# second chunk uses "img.asp" to make sure our aspect ratio is maintained.
knitr::include_graphics("images/plot.png")
Patrick
  • 915
  • 2
  • 9
  • 26