How to control rmarkdown word document images that are being rescaled by Word?
Asked Answered
C

0

7

I'm trying to produce a word document from an rmarkdown file that includes graphs/plots sized to a height of 4 inches and a width of 6.5 inches (which should take up all the width of a normal letter sized page with regular 1-inch margins on each side). However, after knitting to Word, the figures are being automatically resized/rescaled (they end up being 3.59in by 5.83in). I've tried setting the figure parameters using fig.width/height and out.width/height but that doesn't seem to solve the issue. Is there any way to control the images so they are exactly as specified by the markdown file? Otherwise, I would have to go into Word and change each individual image size by hand. Thank you in advance for your help.

Here is the code I'm using:

---
title: "Title"
author:
- "Authors"
date: "7/1/2020"
output: bookdown::word_document2
---

```{r setup, include=FALSE}
rm(list=ls(all=TRUE)) # Clear the workspace
library(tidyverse)
knitr::opts_chunk$set(echo = FALSE)

# load data
data("mtcars")    
```

```{r fig-1, fig.cap="Example Figure", fig.height=4, fig.width=6.5, out.height="4in", out.width="6.5in"}
mtcars %>% 
  ggplot(aes(x = factor(cyl), y = mpg)) + 
  geom_boxplot() + theme_classic()
```

```{r fig-2, fig.cap="Example Figure", fig.height=4, fig.width=6.5, out.height="100%", out.width="100%"}
mtcars %>% 
  ggplot(aes(x = factor(cyl), y = mpg)) + 
  geom_boxplot() + theme_classic()
```
Canthus answered 11/7, 2020 at 22:39 Comment(3)
Interestingly, it seems to be that you're running up against some kind of maximum figure size or something. Experimenting, you can exactly set the size to be 2 inches by 1 inch, or whatever else, except that it won't go wider than 5.83 inchesGasworks
I had a similar problem. I solved it by using fig.asp instead of height: {r , fig.width = 6, fig.asp = 1} However, I think there must be a better solution.Amr
This is still an issue. Has anyone found a work around?Nimiety

© 2022 - 2024 — McMap. All rights reserved.