使用 pdf_output R Markdown 强制空图以“patchwork”显示
我想使用 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 时不起作用:
最后,我还尝试了另一个使用 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
Also note that the plots appear to work in my R session but not when output to pdf:
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我理解正确,您应该在代码块中设置
out.height="50%"
。这意味着绘图将占据页面的 50%。为了创建空白图,我使用了白色背景的主题
。您可以在下面的代码中看到这一点:在 pdf 文档中,您将看到绘图将绘制页面的 50%。
p1 +plot_spacer()
的输出如下所示:p1 + p1
和p1 + 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 atheme
with white background. You can see this in the code below: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:Output of
p1 + p1
andp1 + p2
:And finally the output of
p1 + p3
:As you can see from the images, the plots are half of the pages in a one column.