Running the below Rmd will return nice HTML output. However, when rendering a DOCX the resolution of the plot is not appealing. Increasing the dpi (e.g. to 600) makes the font sizes in the DOCX too small and blows up the HTML. The HTML can be fixed by adjusting fig.width and fig.height but the font size issue in the DOCX remains. I've been playing around for a while now and am struggling to find a generic solution that works for both outputs.
I'd like to be able to make both work "out-of-the-box". What can I do?
---
output:
rmarkdown::html_document:
toc: false
rmarkdown::word_document:
toc: false
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, fig.topcaption = FALSE,
warning = FALSE, message = FALSE, dpi = 100)
library(ggplot2) # 3.4.2
library(plotly) # 4.10.4
```
```{r myplot}
ggp <- ggplot(data = iris, aes(x = Sepal.Length)) +
geom_histogram(binwidth = 4, fill = "forestgreen",
color = "white", alpha = 0.5) +
theme_classic()
ggplotly(ggp)
```
The observed behaviour is related to how HTML widgets are handled during rendering of rmarkdown files to DOCX. A screenshot is taken via
webshotand and included into the DOCX. The quality of the image is controlled via thewebshotzoomparameter, which by default is1. This returns images with a somewhat bad resolution.The default
zoomsetting can be overwritten in the chunk options via e.g.screenshot.opts = list(zoom = 5).For the example shown in the question the solution looks like this:
This is documented in the HTML widgets chapter in "bookdown: Authoring Books and Technical Documents with R Markdown".