1

I'm new to grobbing and I am trying to create a simple grid.arrange object but cannot figure out how to might a compact/tight layout.

Below is a simple example of what I'm trying to run and the grob that I get.

library(grid)
library(gridExtra)
name = textGrob("My Name", gp=gpar(fontsize = 20, fontface = "bold"))
name2 = textGrob("Second Name", gp=gpar(fontsize = 16))
tbl = tableGrob(head(iris))
grid.arrange(name, name2, tbl)

Output of what I see when I run code

UPDATE: Using the answer found here I was able to get the text compact but I am still struggling to get the table to be right under the text.

library(grid)
library(gridExtra)
name = textGrob("My Name", gp=gpar(fontsize = 20, fontface = "bold"))
name2 = textGrob("Second Name", gp=gpar(fontsize = 16))
tbl = tableGrob(head(iris))
margin = unit(0.5, "line")
grid.newpage()
grid.arrange(name, name2, tbl, 
             heights = unit.c(grobHeight(name) + 1.2*margin, 
                              grobHeight(name2) + margin, 
                              unit(1,"null")))

1 Answers1

1

Typically you'd use the top= argument for a single grob. With two grobs like this, it might be easiest to combine them in a table; the major hurdle is that gtable doesn't consider justification so you have to adjust the positions yourself,

library(gtable)
justify <- function(x, hjust="center", vjust="top", draw=FALSE){
  w <- sum(x$widths)
  h <- sum(x$heights)
  xj <- switch(hjust,
               center = 0.5,
               left = 0.5*w,
               right=unit(1,"npc") - 0.5*w)
  yj <- switch(vjust,
               center = 0.5,
               bottom = 0.5*h,
               top=unit(1,"npc") - 0.5*h)
  x$vp <- viewport(x=xj, y=yj)
  if(draw) grid.draw(x)
  return(x)
}
title <- gtable_col('title', grobs = list(name,name2), 
                    heights = unit.c(grobHeight(name) + 1.2*margin, 
                                   grobHeight(name2) + margin))

grid.newpage()
grid.arrange(justify(title, vjust='bottom'), justify(tbl))