【问题标题】:R - How to print more than three tables on one page in a PDFR - 如何在 PDF 的一页上打印三个以上的表格
【发布时间】:2017-07-28 17:18:36
【问题描述】:

我正在尝试从 R 创建一个包含 data.frames 和 ggplots 的多页 PDF 文件。我设法将 data.frames 转换为 grobs 并使用 grid.arrange 函数。

我现在遇到的问题是我想在 PDF 的第一页上打印五个表格/grobs,一个在另一个下面。在第 2 页上,我只想要一个表,因为它包含不同类型的数据。第 3 页将包含一个 ggplot。在一页上最多放置三个表格非常有效,但是一旦我尝试使用四个或更多表格,它就会将它们打印在两列中并且它们会重叠。

这是我到目前为止尝试过的:

# Load required packages
library(gridBase)
library(gridExtra)
library(ggplot2)

# Transform tables into grobs
Table1 <- tableGrob(df1)
Table2 <- tableGrob(df2)
Table3 <- tableGrob(df3)
Table4 <- tableGrob(df4)
Table5 <- tableGrob(df5)
Table6 <- tableGrob(df6)

# Create the PDF file

pdf("file.pdf", height = 15, width = 20)
layout(matrix(c(1,2,3,4,5,6), 1, 6, byrow = TRUE))
grid.arrange(Table1, Table2, Table3, Table4, Table5)
grid.arrange(Table6)
grid.arrange(ggplot1)
dev.off()

有人知道我哪里错了吗?

提前非常感谢!

干杯, 蒂尔曼

This is what the first page of the PDF looks like as soon as I use more than three tables. I want them to be all in one column

【问题讨论】:

  • 就像评论一样:如果你只画一个 grob,最好使用grid.draw(Table6) 而不是grid.arrange(Table6)(对于 ggplot1 也是如此)。 grid.arrange() 仅对多个 grobs 有意义,否则您将使用未使用的布局为图形添加不必要的复杂性。

标签: r ggplot2 gridextra


【解决方案1】:

刚刚解决了 - 当然还有一个我没有想到的简单解决方案。

grid.arrange(Table1, Table2, Table3, Table4, Table5, ncol = 1, nrow = 5)

只需添加 ncol 和 nrow 即可。

【讨论】:

    【解决方案2】:

    尝试使用或查找\newpage 函数的变体?这超出了块的范围。

    【讨论】:

    • 非常感谢您的回复!我试过了,但它会给我和以前一样的结果。我认为这并不是我真正想要的,因为我已经有了我想要的页数,只需使用新的 grid.arrange() 命令。我想要的是在同一个页面上收集前五个表。
    • 刚刚添加了 PDF 输出的第一页图片,其中包含来自 mtcars 的数据
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-26
    • 1970-01-01
    相关资源
    最近更新 更多