When rendering the below Rmd
to a bookdown::html_document2
document everything looks fine. When I render it to an officedown::rdocx_document
, however, I get a distorted and truncated version of the graph.
Why is that and how do I fix this?
---
output:
officedown::rdocx_document:
base_format: "rmarkdown::word_document"
bookdown::html_document2:
toc: true
---
```{r, echo = FALSE, message = FALSE}
library(officedown) # 0.3.0
library(DiagrammeR) # 1.0.9
create_graph() %>%
add_balanced_tree(k = 2, h = 3) %>%
render_graph()
```
When it becomes a Knitr or R Markdown sized plot, the default is 7in wide x 5in tall, which officedown converts to 5in wide by 4in wide, since the aspect ratio at 7:5 != 5:4 you get distortion.
2 options to fix:
To set the graph to a specific size, you have to define the pixels, not inches.
All that matters is that the width to height ratio is 5:4.
The larger the values you use the more crisp the image will be (and the bigger the file will be).
For example:
---
title: "Untitled"
author: "me"
date: "2024-07-27"
output:
officedown::rdocx_document:
base_format: "rmarkdown::word_document"
bookdown::html_document2:
toc: true
---
```{r setup, echo=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(officedown)
library(DiagrammeR)
create_graph() %>% add_balanced_tree(k = 2, h = 3) %>%
render_graph(width = 1000, height = 800)
```
This is the word document on the left and the html on the right from this code.
To change the default figure size in Knitr, you set this in output:
---
title: "Untitled"
author: "me"
date: "2024-07-27"
output:
officedown::rdocx_document:
base_format: "rmarkdown::word_document"
fig_width: 5
fig_height: 4
bookdown::html_document2:
toc: true
---
```{r setup, echo=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(officedown)
library(DiagrammeR)
create_graph() %>% add_balanced_tree(k = 2, h = 3) %>%
render_graph(width = 1000, height = 800)
```
Here's the word and html again (left and right, respectively).