![](/img/trans.png)
[英]How to fit 4 figures (2 by 2) in a single slide with rmarkdown beamer
[英]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 模板中的幻燈片母版,因為:
該模板應包含以下四個布局作為其前四個布局 [...]
然后它專門將幻燈片母版布局列為:
您的自定義母版似乎違反了“雙內容”格式,因為您有 3 個內容塊。 (不僅如此,從我的實驗來看,甚至更改模板主幻燈片中的框大小都會弄亂內容的插入方式。)我認為:
:::::::::::::: {.columns}
::: {.column}
:::
::: {.column}
:::
::::::::::::::
語法僅適用於 2 列,而不適用於您所擁有的 3 列。
如果你想在不使用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.