5

I am generating a PowerPoint presentation with R-markdown. How do I include multiple figures or "contents" on a slide?

I have tried modifying the PowerPoint template to include three content blocks as following: img But I am unable to add content to the object on the bottom right.

---
title: "pp_test"
output: 
  powerpoint_presentation:
    reference_doc: pp_template3.pptx
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```

# Slide with 3 contents

:::::::::::::: {.columns}
::: {.column}
Text
:::
::: {.column}
```{r pressure, echo=FALSE, fig.cap="Caption"}
plot(pressure)
```
:::
::: {.column}
```{r cars, echo = TRUE}
summary(cars)
```
:::
::::::::::::::

I expected the "summary(cars)" section to be added beneath the plot on the slide, but it is simply excluded.

user308225
  • 101
  • 6
  • Can you pre-create multiple figures in R then add them to the powerpoint later? https://stackoverflow.com/q/1249548/786542 – Tung Dec 10 '19 at 18:12

2 Answers2

4

I was unable to succeed using r-markdown, so I researched other packages and found "officer", which was able to produce the results I wanted.

It does not support tables that are not dataframes, so I was unable to add the "summary(cars)" part. But with two plots as examples, I was able to produce the result Result with officer

Using the following code

library(officer)
library(magrittr)
library(ggplot2)
setwd(mydir)

my_plot <- ggplot(data=pressure) +
  geom_point(aes(temperature, pressure))
my_summary <- as.str(summary(cars))

my_pres <- 
  read_pptx("pp_template3.pptx") %>%
  add_slide(layout = "Two Content", master = "Office Theme") %>%
  ph_with_text(type = "title", index = 1, str = "The title") %>%
  ph_with_gg(type = "body", index = 1, value = my_plot) %>%
  ph_with_text(type = "body", index = 2, str = "Some text") %>%
  ph_with_gg(type = "body", index = 3, value = my_plot) %>%
  print(target = "test_pp_officer.pptx") %>%
  invisible()
user308225
  • 101
  • 6
0

Background

I have tried modifying the PowerPoint template to include three content blocks as following:

According to this RStudio Support article, I don't think you're allowed to change the slide masters in the PPTX template because:

The template should contain the following four layouts as its first four layouts [...]

It then specifically lists the slide master layouts as:

  1. Title. Has placeholders for a title and a subtitle. Used for information in the YAML header.

  2. Title and Content. Has placeholders for title and content. Used for headers at the slide level.

  3. Section Header. Has a placeholder for a section header – content will not render on this layout. Used for headers above the slide level.

  4. Two Content. Has placeholders for title and two content. Used for headers at the slide level with content in columns.

Your custom master seems to violate the "two content" format as you have 3 content blocks. (Not only that, but from my experimentation even changing the box sizes in the master slide of a template messes up how the content is inserted.) I think the:

:::::::::::::: {.columns}
::: {.column}

:::
::: {.column}

:::
::::::::::::::

Syntax only works for 2 columns, not 3 like you have.

R Markdown solution for including multiple figures in one column of a 2-column slide in PowerPoint output

If you want to stay in an R Markdown workflow without using officer, a very easy way to include multiple ggplot2 plots in a single image is to use the recent patchwork package, and then add the combined plot chunk into one of the columns:

---
output:
  powerpoint_presentation:
    slide_level: 2 # h1 makes a new section slide, h2 makes a new slide
    reference_doc: template.pptx
---

```{r setup, echo=FALSE, message=FALSE}
knitr::opts_chunk$set(
    echo = F,
    warning = F,
    message = F
)

library(tidyverse)
library(patchwork)
```

## Define some plots

```{r, echo=TRUE}
p1 <- ggplot(mtcars) + 
  geom_point(aes(mpg, disp)) + 
  ggtitle('Plot 1')

p2 <- ggplot(mtcars) + 
  geom_boxplot(aes(gear, disp, group = gear)) + 
  ggtitle('Plot 2')

p3 <- ggplot(mtcars) + 
  geom_point(aes(hp, wt, colour = mpg)) + 
  ggtitle('Plot 3')

p4 <- ggplot(mtcars) + 
  geom_bar(aes(gear)) + 
  facet_wrap(~cyl) + 
  ggtitle('Plot 4')
```

## Plot text bullets in 1st column and 4 plots in 2nd column

::::::::: {.columns}
::: {.column}
- here is some text 
- in the left column 
- to describe the plots 
- in the right column
:::
::: {.column}
```{r, fig.width=6, fig.height=4, fig.cap="This is the caption for the figure."}
# Adjust figure dimensions in chunk options
# use layout syntax form the 'patchwork' package
p1 + p2 + p3 + p4 # keeps square arrangement by default
```
:::
:::::::::

Leo
  • 187
  • 1
  • 9