Reduce the space (margin) between two tables in gridExtra with R

Example:

sample <- data.frame(a=c(1,2,3),b=c(2,3,4),c=c("name1","name2","name3"),d=c(1,2,3))



pdf(path,width=15,height=11)

par(mfrow=c(2,1))

plot.new()
vps <- baseViewports()
pushViewport(vps$figure)
vp1 <-plotViewport()
grid.table(sample,gpar.coretext=gpar(fontsize = 14),gpar.coltext = gpar(fontsize = 14),
        gpar.rowtext = gpar(fontsize = 14),gpar.corefill = gpar(fill = "steelblue2", alpha = 0.4, 
                                                               col = NA),
        h.even.alpha = 0.4,equal.width = FALSE,show.rownames = FALSE,show.vlines = TRUE,
        padding.h = unit(8, "mm"),padding.v = unit(8, "mm"))       # table width/height 
grid.text("Some text comes here ...", x = 0.5, y = 0.75, gp = gpar(fontsize = 12))
popViewport()

plot.new()
par(mar=c(rep(0,4)+0.1))  # no effect here
vps <- baseViewports()
pushViewport(vps$figure)
vp1 <-plotViewport()
grid.table(sample,gpar.coretext=gpar(fontsize = 14),gpar.coltext = gpar(fontsize = 14),
       gpar.rowtext = gpar(fontsize = 14),gpar.corefill = gpar(fill = "steelblue2", alpha = 0.4, 
                                                               col = NA),
       h.even.alpha = 0.4,equal.width = FALSE,show.rownames = FALSE,show.vlines = TRUE,
       padding.h = unit(8, "mm"),padding.v = unit(8, "mm"))       # table width/height 
grid.text("Some text comes here ...", x = 0.5, y = 0.75, gp = gpar(fontsize = 12))
popViewport()
dev.off()

      

enter image description here

How do I remove the spacing between two tables?

EDIT: Another easier way:

sample <- data.frame(a=c(1,2,3),b=c(2,3,4),c=c("name1","name2","name3"),d=c(1,2,3))


pdf(path,width=5,height=4)

grid.arrange(tableGrob(sample, show.rownames=F),tableGrob(sample, show.rownames=F),nrow=2)

dev.off()

      

Playing with the pdf width/height

can actually reduce the space a bit, but is this the correct way to do it?

+3


source to share





All Articles