簡體   English   中英

如何在 RMarkdown 的 PowerPoint 幻燈片上包含多個數字?

[英]How do I include muliple figures on a PowerPoint slide in RMarkdown?

我正在使用 R-markdown 生成 PowerPoint 演示文稿。 如何在幻燈片上包含多個圖形或“內容”?

我嘗試修改 PowerPoint 模板以包含以下三個內容塊:圖片 但是我無法向右下角的對象添加內容。

---
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-markdown成功,因此我研究了其他軟件包並找到了“官員”,它能夠產生我想要的結果。

它不支持不是數據幀的表,因此我無法添加“summary(cars)”部分。 但是以兩個圖為例,我能夠產生結果 與官員的結果

使用以下代碼

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

背景

我嘗試修改 PowerPoint 模板以包含以下三個內容塊:

根據這篇 RStudio 支持文章,我認為您不允許更改 PPTX 模板中的幻燈片母版,因為:

該模板應包含以下四個布局作為其前四個布局 [...]

然后它專門將幻燈片母版布局列為:

  1. 標題 具有標題和副標題的占位符。 用於 YAML 標頭中的信息。

  2. 標題和內容 具有標題和內容的占位符。 用於幻燈片級別的標題。

  3. 部分標題 具有節標題的占位符 - 內容不會在此布局上呈現。 用於幻燈片級別以上的標題。

  4. 二 內容 具有標題和兩個內容的占位符。 用於幻燈片級別的標題,內容在列中

您的自定義母版似乎違反了“雙內容”格式,因為您有 3 個內容塊。 (不僅如此,從我的實驗來看,甚至更改模板主幻燈片中的框大小都會弄亂內容的插入方式。)我認為:

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

:::
::: {.column}

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

語法僅適用於 2 列,而不適用於您所擁有的 3 列。

R Markdown 解決方案,用於在 PowerPoint 輸出的 2 列幻燈片的一列中包含多個數字

如果你想在不使用officer情況下留在 R Markdown 工作流程中,在單個圖像中包含多個ggplot2繪圖的一種非常簡單的方法是使用最近的patchwork,然后將組合的繪圖塊添加到其中一列中:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM