如何在RMarkdown中将多个图形添加到PowerPoint幻灯片中?

5
我用R-markdown生成了一份PowerPoint演示文稿。如何在幻灯片上包含多个图像或“内容”?
我尝试修改PowerPoint模板,将三个内容块添加到幻灯片中: img 但是我无法向右下角的对象添加内容。
---
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)
```
:::
::::::::::::::

我原本期望在幻灯片上的图表下方添加“汇总(汽车)”部分,但它被简单地忽略了。

你可以在 R 中预先创建多个图形,然后稍后将它们添加到 PowerPoint 中吗?https://dev59.com/w3M_5IYBdhLWcg3wvV1w - Tung
2个回答

4

我无法使用r-markdown成功,所以我研究了其他的包,发现了"officer",它能够生成我想要的结果。

它不支持不是数据帧的表格,所以我无法添加 "summary(cars)" 部分。但是通过两个绘图作为例子,我能够产生结果Result with officer

使用以下代码

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()

0

背景

我尝试修改 PowerPoint 模板,以包含以下三个内容块:

根据this RStudio Support article,我认为您不允许更改 PPTX 模板中的幻灯片母版,因为:

该模板应包含以下四个布局作为其前四个布局[...]。

然后具体列出了幻灯片母版布局:

  1. 标题。用于YAML头中的信息,具有标题和副标题的占位符。

  2. 标题和内容。用于幻灯片级别的标题和内容的占位符。

  3. 节标题。具有部分标题的占位符-内容不会在此布局上呈现。用于幻灯片级别上方的标题。

  4. 两个内容。用于幻灯片级别的标题和两个内容的占位符,并在中显示内容。

您的自定义母版似乎违反了“两个内容”格式,因为您有3个内容块。(不仅如此,从我的实验中,即使更改模板的母版幻灯片中的框大小也会搞乱插入内容的方式。)我认为:

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

:::
::: {.column}

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

语法仅适用于2列,不适用于您拥有的3列。

在PowerPoint输出中,R Markdown解决方案可包含一个2列幻灯片中的多个图像

如果您想在R Markdown工作流程中不使用officer,则一种非常简单的方法是使用最近的patchwork软件包来包含多个ggplot2绘图在单个图像中,然后将组合的绘图块添加到其中一个列中:

---
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
```
:::
:::::::::


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接