Reputation: 111
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
Upvotes: 6
Views: 3551
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
Reputation: 131
Try using or looking up variations of the \newpage
function? That goes outside of a chunk.
Upvotes: 0