Search code examples
ggplot2markdownpatchwork

Why is R markdown not able to format my patchwork figure?


I have a question regarding markdown and patchwork. Let's say I combined figures FigureLength and FigureWidth to make CombinedFigure through patchwork (two figures on top of each other):

library(datasets)
library(patchwork)
data(iris)

FigureLength <- iris %>% 
  ggplot(aes(x = Species, y = Sepal.Length)) +
  geom_boxplot()

FigureWidth <- iris %>% 
  ggplot(aes(x = Species, y = Sepal.Width)) +
  geom_boxplot()

CombinedFigure <- FigureLength / FigureWidth

Both figures nicely on top of each other in R

save(CombinedFigure, file = here::here ("CombinedFigure"))

In Markdown when I put the following only Sepal Length appears in my word document and not both figures on top of each other.

```{r CombinedFigure,   dpi = 300, echo = FALSE}
CombinedFigure
```

What appears in markdown - only one figure

Thanks for your help.


Solution

  • When I run the following code:

    ---
    title: "Untitled"
    author: "Author"
    date: "4/4/2022"
    output: word_document
    ---
    
    ```{r}
    library(datasets)
    library(tidyverse)
    library(patchwork)
    data(iris)
    
    FigureLength <- iris %>% 
      ggplot(aes(x = Species, y = Sepal.Length)) +
      geom_boxplot()
    
    FigureWidth <- iris %>% 
      ggplot(aes(x = Species, y = Sepal.Width)) +
      geom_boxplot()
    
    CombinedFigure <- FigureLength / FigureWidth
    ```
    
    ```{r CombinedFigure,   dpi = 300, echo = FALSE}
    CombinedFigure
    ```
    

    I to get both figures nicely on top of each other in WORD like this:

    enter image description here