Question

I am trying to plot tables in a plot, using grid. I have 2 tables and I want them to be aligned on their headers:

library(grid)
library(gridExtra)

pdf('test.pdf')
DF1=data.frame(name=1:10,value=1:10)
DF2=data.frame(name=1:4,value=1:4)  

grid.newpage() 
pushViewport(viewport(layout=grid.layout(1, 2, widths=unit(c(3,3), "inches"))))

pushViewport(viewport(layout.pos.col=1, layout.pos.row=1)) 
grid.draw(tableGrob(DF1, gp=gpar(fontsize=6, lwd=.5)))
popViewport(1)

pushViewport(viewport(layout.pos.col=2, layout.pos.row=1, clip="on"))
grid.draw(tableGrob(DF2, gp=gpar(fontsize=6, lwd=.5)))
popViewport()
graphics.off()

This gives me the bellow:

How can I make sure that the headers "name","value" are on the same row ?

enter image description here

Était-ce utile?

La solution

library(gridExtra)

d1=data.frame(name=1:10,value=1:10)
d2=data.frame(name=1:4,value=1:4)  

g1 <- tableGrob(d1)
g2 <- tableGrob(d2)

w1 <- grobWidth(g1)
w2 <- grobWidth(g2)

h1 <- grobHeight(g1)
h2 <- grobHeight(g2)

grid.newpage()
grid.draw(grobTree(g1, vp=viewport(x=unit(0.5, "npc") - 0.5*w1 - unit(1,"line"),
                                   y=unit(1,"npc") - h1*0.5)))
grid.draw(grobTree(g2, vp=viewport(x=unit(0.5, "npc") + 0.5*w2 + unit(1,"line"),
                   y=unit(1,"npc") - h2*0.5)))

enter image description here

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top