RMarkdown PowerPoint Presentation

Here is the code:


output: powerpoint_presentation

## Slide with Plot
library(patchwork)
library(tidyverse)
# prep frequency table
freqtable <- table(mpg$manufacturer)
df <- as.data.frame.table(freqtable)
head(df)
theme_set(theme_classic())
# Plot
g <- ggplot(df, aes(Var1, Freq))
g <- g + geom_bar(stat="identity", width = 0.5, fill="tomato2") + 
  theme(axis.text.x = element_text(angle=65, vjust=0.6)) +
  theme(axis.title.x = element_blank())
g+g+g+g

This produces 2 slides. I have 2 questions:

  1. How do I prevent the first slide from being produced so that only the plot slide is created?

  2. How do I change the dimensions/size of the plot? I am using Patchwork to create a 4x4 plot which I want to save to a PowerPoint slide. The code above successfully creates the 4x4 plots but when I execute the above Markdown program, the plot is too small on the powerpoint slide. I have tried the fig.width and fig.height options and a few other suggested methods but none of them appear to affect the size of the plot in PowerPoint. I am also not clear on how to create a custom template - it is difficult for me to understand the directions for doing so....

Thanks for the help!

You're getting close! They key thing here is creating a template PPT that has a slide area that takes up the whole slide. While you're using a custom template, you can also get rid of the title slide and no extra slide will render.

See here for an example template that I use in the code below to generate what I believe you are looking for.

The easiest thing to do would be to download template-no-title.pptx and put it in the same directory as your Rmd file that you will be knitting. Then, the Rmd file will look something like this:

---
output: 
  powerpoint_presentation:
    reference_doc: "template-no-title.pptx"
---
  
```{r setup, include=FALSE}
knitr::opts_chunk$set(
  echo = FALSE,
  message = FALSE,
  warning = FALSE,
  dev = "svg",
  fig.width = 12,
  fig.height = 7
)
```
```{r}
library(patchwork)
library(tidyverse)
# prep frequency table
freqtable <- table(mpg$manufacturer)
df <- as.data.frame.table(freqtable)
theme_set(theme_classic())
# Plot
g <- ggplot(df, aes(Var1, Freq))
g <- g + geom_bar(stat="identity", width = 0.5, fill="tomato2") + 
  theme(axis.text.x = element_text(angle=65, vjust=0.6)) +
  theme(axis.title.x = element_blank())
g+g+g+g
```

And this is the slide that is rendered:

1 Like

Sweet! Thanks Matt! I'll look at it right away - I figured it would require a custom template but I wasn't understanding where to find it or save it, so you answered that question. (Ironically, I downloaded your template last night but couldn't figure out where it was supposed to go.)
Thanks again!

Great! Seems like you're pretty close to what you want. I did have to fiddle around a bit with my PPT template and the fig.width and fig.height chunk options to get my plots to fill up the slide how I wanted.

In the PPT template (which is just a blank PPT file), you can go in to the Slide Master View and change the size and spacing of the slide elements. This is where you could also add a logo or other PPT theme so that when you render from Rmd using that template file, it uses those slide layouts you have defined.

1 Like

Getting a message saying "The Picture Can't be Displayed" in PowerPoint after Knitting the below code. I did save the template-no-title.pptx to the directory where the Rmd file is saved.

---
output: 
  powerpoint_presentation:
    reference_doc: "template-no-title.pptx"
---
knitr::opts_chunk$set(
  echo = FALSE,
  message = FALSE,
  warning = FALSE,
  dev = "svg",
  fig.width = 12,
  fig.height = 7
)
library(patchwork)
library(tidyverse)
# prep frequency table
freqtable <- table(mpg$manufacturer)
df <- as.data.frame.table(freqtable)
theme_set(theme_classic())
# Plot
g <- ggplot(df, aes(Var1, Freq))
g <- g + geom_bar(stat="identity", width = 0.5, fill="tomato2") + 
  theme(axis.text.x = element_text(angle=65, vjust=0.6)) +
  theme(axis.title.x = element_blank())
g+g+g+g

I've narrowed it down to this portion.....will look into why this is preventing the charts from being pasted onto the PowerPoint now.

knitr::opts_chunk$set(
  echo = FALSE,
  message = FALSE,
  warning = FALSE,
  dev = "svg",
  fig.width = 12,
  fig.height = 7
)

You might try change the dev argument to "png" or something else. This controls which graphics device is used for image rendering. I liked the way the "svg" looked on my machine, but that could be problem for you. See here https://yihui.org/knitr/options/#plots for more info on knitr graphics devices.

1 Like

Damn! You beat me to it! lol

The below works....now time to adjust some of the dimensions. Still getting a page before the charts with some data from the table (see image below)

image

Here is the entire code I am running:

---
output: powerpoint_presentation
reference_doc: "template-no-title.pptx"
---
knitr::opts_chunk$set(
  echo = FALSE,
  message = FALSE,
  warning = FALSE,
#  dev = "svg",
  fig.width = 12,
  fig.height = 7
)
## Slide with Plot
library(patchwork)
library(tidyverse)
# prep frequency table
freqtable <- table(mpg$manufacturer)
df <- as.data.frame.table(freqtable)
head(df)
theme_set(theme_classic())
# Plot
g <- ggplot(df, aes(Var1, Freq))
g <- g + geom_bar(stat="identity", width = 0.5, fill="tomato2") + 
  theme(axis.text.x = element_text(angle=65, vjust=0.6)) +
  theme(axis.title.x = element_blank())
g+g+g+g

This is it! Thank you Matt!!!!

---
output: 
  powerpoint_presentation:
    reference_doc: "template-no-title.pptx"
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(
  echo = FALSE,
  message = FALSE,
  warning = FALSE,
#  dev = "svg",
  fig.width = 12,
  fig.height = 7
)
library(patchwork)
library(tidyverse)
# prep frequency table
freqtable <- table(mpg$manufacturer)
df <- as.data.frame.table(freqtable)
theme_set(theme_classic())
# Plot
g <- ggplot(df, aes(Var1, Freq))
g <- g + geom_bar(stat="identity", width = 0.5, fill="tomato2") + 
  theme(axis.text.x = element_text(angle=65, vjust=0.6)) +
  theme(axis.title.x = element_blank())
g+g+g+g 
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.