rggplot2ms-word

Export ggplot2 plots to a Word document with high quality


I typically use R to create graphs with ggplot2, and they look fantastic when exported to PDF documents.

However, I encounter an issue when trying to incorporate these graphs into Word documents.

I usually export in PDF and resort to using the Snipping Tool and pasting them into Word, but this drastically reduces the quality.

Below you can see one one example:

library(ggplot2)
set.seed(1234)

wdata = data.frame(
  sex = factor(rep(c("F", "M"), each=200)),
  weight = c(rnorm(200, 55), rnorm(200, 58)))

a <- ggplot(wdata, aes(x = weight))

a + geom_area(stat = "bin",
              color= "black", fill="#00AFBB")

enter image description here

Can someone help me with a method to transfer them to Word without losing their quality?


Solution

  • My default method for this is to use an rmarkdown or quarto document and set the DPI in that. It keeps the resolution in the output document:

    ---
    title: "Test graphs"
    output: word_document
    ---
    
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = TRUE,
                          dpi = 900)
    ```
    
    ```{r}
    library(ggplot2)
    set.seed(1234)
    
    wdata = data.frame(
      sex = factor(rep(c("F", "M"), each=200)),
      weight = c(rnorm(200, 55), rnorm(200, 58)))
    
    a <- ggplot(wdata, aes(x = weight))
    
    a + geom_area(stat = "bin",
                  color= "black", fill="#00AFBB")
    ```