使用R减少gridExtra中2个表之间的空间(边距)

时间:2014-10-27 15:26:17

标签: r

示例:

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

如何删除2个表之间的间距?

编辑:其他更简单的方法:

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

通过使用pdf width/height,实际上可以稍微缩小差距,但这是正确的做法吗?

0 个答案:

没有答案