T. Wolf
T. Wolf

Reputation: 111

R - How to print more than three tables on one page in a PDF

I am trying to create a multiple page PDF file from R which contains both data.frames and ggplots. I managed to do so transforming the data.frames into grobs and using the grid.arrange function.

The problem I have now is that I want to print five tables/grobs on the first page in the PDF, one below another. On page 2 I want only one table as it contains different kinds of data. Page 3 will contain a ggplot. It works perfectly fine to put up to three tables on one page but as soon as I try it with four or more it will print them in two columns and they will overlap.

Here is what I tried up to now:

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

Does anybody know where I'm wrong?

Thanks a lot in advance!

Cheers, Tilman

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

Upvotes: 6

Views: 3551

Answers (2)

T. Wolf
T. Wolf

Reputation: 111

Just solved it - and of course there's an easy solution I did not think about.

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

Just adding ncol and nrow makes the deal.

Upvotes: 4

skhan8
skhan8

Reputation: 131

Try using or looking up variations of the \newpage function? That goes outside of a chunk.

Upvotes: 0

Related Questions