how to arrange tables in specific place grid.arrange

332 Views Asked by At

I am using the gtable and gridExtra packages to manipulate PDF. I am struggling with setting the place manually for my tables i.e I would like to be both of the more in the center not so close to the edges. The place is set up automatically when the grid.arrange() formula is being used.

Additionally, I am wondering how can I make the footnote be more on the left, even "before" the table.

how can I adjust the layout_matrix() to that ?

grid.arrange(table1, table)

Please see the simplified code:

pdf("mrcars_check.pdf",width = 15, height=10, bg = "white", onefile = TRUE, title = "ANALYSIS OF MoM CHANGES IN BUSA MORTGAGES")


table1 <- tableGrob((format(mtcars[1:2,], big.mark = ",", digits = NULL, decimal.mark = ".", nsmALL=0, scientific = FALSE)),rows = NULL)
title1 <- textGrob(paste0("Title"),gp=gpar(fontsize=16))
footnote1 <- textGrob(paste0("Footnote"),x=0, hjust=0,gp=gpar(fontface="italic",fontsize=14))

padding <- unit(2,"line")
table1 <- gtable_add_rows(table1, 
                          heights = grobHeight(title1) + padding,
                          pos = 0)
table1 <- gtable_add_rows(table1, 
                          heights = grobHeight(footnote1)+ padding)
table1 <- gtable_add_grob(table1, list(title1, footnote1),
                          t=c(1, nrow(table1)), l=c(1,2), 
                          r=ncol(table1))

### table 2
table <- tableGrob((format(mtcars[4:9,], big.mark = ",", digits = NULL, decimal.mark = ".", nsmALL=0, scientific = FALSE)),rows = NULL) 
title <- textGrob(paste0(" Title"),gp=gpar(fontsize=14))
footnote <- textGrob(paste0("
This is a footnote.This is a footnote.This is a footnote.This is a footnote.This is a footnote.This is a footnote.This is a footnote.This is a footnote.This is a footnote.This is a footnote.This is a footnote.
                            This is a footnote.This is a footnote.This is a footnote.This is a footnote.This is a footnote."),x=0, hjust=0,
                     gp=gpar(fontface="italic",fontsize=14))

padding <- unit(2,"line")
table <- gtable_add_rows(table, 
                         heights = grobHeight(title) + padding,
                         pos = 0)
table <- gtable_add_rows(table, 
                         heights = grobHeight(footnote)+ padding)
table <- gtable_add_grob(table, list(title, footnote),
                         t=c(1, nrow(table)), l=c(1,2), 
                         r=ncol(table), clip='off')

print(grid.arrange(table1, table))

dev.off()

Many thanks for any help.

0

There are 0 best solutions below