使用 pdf_output R Markdown 强制空图以“patchwork”显示

发布于 2025-01-13 13:27:20 字数 1311 浏览 4 评论 0原文

我想使用 patchwork 库和 plot_spacer() 显示占据页面一半的单个图(看起来就像占据一列)功能。问题是,我不能让“空地”占据一半的空间!无论我做什么,第一个图似乎占据了页面的大部分内容。我正在寻找与下面的 p1 + p2 等效的内容,但我希望 p2 是空白。

可重现的示例和输出

---
title: " "
author: " "
date: " "
output: pdf_document
geometry: paperheight=8.5in,paperwidth=5.5in,left=0.4in, right=0.4in, top=0.25in, bottom=0.25in
header-includes:
    - \pagenumbering{gobble}
---

{r, fig.height = 1.5}
library(ggplot2)
library(patchwork)
p1 <- ggplot(iris, aes(Sepal.Width, Sepal.Length)) + geom_point()
p2 <- ggplot() + geom_blank()
p3 <- p2 + theme_void()

p1 + plot_spacer()
p1 + p1
p1 + p2
p1 + p3

各种图不做我想做的事情

另请注意,这些图似乎在我的 R 会话中工作,但在输出到 pdf 时不起作用:

来自 R 会话的图片

最后,我还尝试了另一个使用 plot_layout 的建议:

design <- "
1#
"

p1 + plot_layout(design = design)

这似乎也适用于会话,但不适用于 pdf_output。

输入图片此处描述

I would like to display a single plot that takes up half of my page (to appear like it is taking up one column), using the patchwork library and the plot_spacer() function. The thing is, I can't get the "empty plot" to take up half the space! No matter what I do, the first plot seems to take up most of the page. I'm looking for the equivalent of p1 + p2 below, except I'd want p2 to be white space.

Reproducible example and output

---
title: " "
author: " "
date: " "
output: pdf_document
geometry: paperheight=8.5in,paperwidth=5.5in,left=0.4in, right=0.4in, top=0.25in, bottom=0.25in
header-includes:
    - \pagenumbering{gobble}
---

{r, fig.height = 1.5}
library(ggplot2)
library(patchwork)
p1 <- ggplot(iris, aes(Sepal.Width, Sepal.Length)) + geom_point()
p2 <- ggplot() + geom_blank()
p3 <- p2 + theme_void()

p1 + plot_spacer()
p1 + p1
p1 + p2
p1 + p3

various plots that don't do what I want

Also note that the plots appear to work in my R session but not when output to pdf:

pics from R session

Finally, I also tried another suggestion to use plot_layout:

design <- "
1#
"

p1 + plot_layout(design = design)

This also seems to work in the session but not in pdf_output.

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挖鼻大婶 2025-01-20 13:27:20

如果我理解正确,您应该在代码块中设置 out.height="50%" 。这意味着绘图将占据页面的 50%。为了创建空白图,我使用了白色背景的主题。您可以在下面的代码中看到这一点:

output: pdf_document
---

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

\newpage

```{r, out.height="50%"}
library(ggplot2)
library(patchwork)
p1 <- ggplot(iris, aes(Sepal.Width, Sepal.Length)) + geom_point()
p2 <- ggplot() + geom_blank() + theme(panel.background = element_rect(fill = 'white', colour = 'white'))
p3 <- p2 + theme_void()

p1 + plot_spacer()
p1 + p1
p1 + p2
p1 + p3
```

在 pdf 文档中,您将看到绘图将绘制页面的 50%。 p1 +plot_spacer() 的输出如下所示:

在此处输入图像描述

p1 + p1p1 + p2 的输出:

输入图片此处描述

最后是 p1 + p3 的输出:

在此处输入图像描述

正如您从图像中看到的,这些图占了页面的一半在一列中。

If I understand you correctly, you should set out.height="50%" in your code chunk. This means that the plots will take 50% of the page. To create a white space plot, I used a theme with white background. You can see this in the code below:

output: pdf_document
---

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

\newpage

```{r, out.height="50%"}
library(ggplot2)
library(patchwork)
p1 <- ggplot(iris, aes(Sepal.Width, Sepal.Length)) + geom_point()
p2 <- ggplot() + geom_blank() + theme(panel.background = element_rect(fill = 'white', colour = 'white'))
p3 <- p2 + theme_void()

p1 + plot_spacer()
p1 + p1
p1 + p2
p1 + p3
```

In the pdf document you will see that the plots will plot 50% of the page. The output of p1 + plot_spacer() looks like this:

enter image description here

Output of p1 + p1 and p1 + p2:

enter image description here

And finally the output of p1 + p3:

enter image description here

As you can see from the images, the plots are half of the pages in a one column.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文