r knit word document plots automatically re-sized
Asked Answered
U

2

12

I am trying to add a plot to a word document. I would like the plot to maximize the area available when the page size is set to legal with narrow margins. I can set the fig.width and fig.height but it seems the plots get automatically re-sized to fit the default page size (letter) with normal margins.

Here is a sample .rmd file that produces the same results:

---
title: "plot-resize"
output: word_document
---

Plot with the height set to 3" and the width to 7.5":

```{r, echo = FALSE, fig.height=3, fig.width=7.5, warning=FALSE, message=FALSE}
plot(cars)
```

However when the word document is created the image is automatically 
re-sized to 79% of this. 

I can re-size the plot in word, but it would be nice to not have to.

Is there a way to set the page size and margins in the .rmd file?

Is there a way to ensure that the plots stay at the specified size even if they do not fit within the margins of the created word document?

Unbeatable answered 29/3, 2015 at 18:56 Comment(5)
I cannot give you an exact answer but maybe try to play around with out.width as well. fig.width only gives the dimensions for the graphic device used by R to plot.Vickyvico
@jakobr out.width and out.height do not work for Word output.Leto
@Vickyvico Output created: plot-resize.docx Warning message: In (knit_hooks$get("plot"))(file, reduce_plot_opts(options)) : Chunk options fig.align, out.width, out.height, out.extra are not supported for Word outputUnbeatable
@DavidDickson Did you ever find a solution for this? I have the same issue.Odle
Note that even though out.width adn out.height do not work in Word output, fig.height and fig.width do work for Word output, so you can use those (this nuance could easily be missed if you read the comments above too quickly).Rehm
M
7

You can redo the MS Word template file - see http://rmarkdown.rstudio.com/articles_docx.html - you would have to change your margins to narrow (0.5") in the MS Word template file you are using (Under the Layout ribbon). Then, right click on the figure and select size and position, and then adjust scale height and width to 100%. You would then have to save your template file (and don't forget to close it!) and then add this to your YAML:

title: "plot-resize"
output:
  word_document:
    reference_docx: mynew_template.docx
Monitor answered 5/10, 2017 at 14:40 Comment(2)
WOW I NEVER would have found this on my own. Thanks!Pelton
I didn't get this..Elinorelinore
V
0

I use the handles in the plot code. First I check how the plot with the size 100 looks:

---
title: "plot-resize"
output: word_document
---

Plot with the size 100:

```{r, echo = FALSE, out.height=100, out.width=100, warning=FALSE, message=FALSE}
plot(cars)```

Then I note the width and height of the plot that appears in the word document (in my case H:2.64cm and W:2.64cm). In here, we want graph H:3cm and W:7.5cm. To recalculate the size, I do:

H: 3cm * 100/2.64cm = 113.6364

W: 7.5cm * 100/2.64cm = 284.0909 (In my template, anything above 14.81cm will not work due to Word document width constraint)

Ultimately the code for plot looks like this:

Plot with the height set to 3cm and the width to 7.5cm:

```{r, echo = FALSE, out.height=113.6364, out.width=284.0909, warning=FALSE, message=FALSE}
plot(cars)```

Vocalism answered 5/4, 2023 at 11:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.