title: Iterate over entries in `TableGrob` to find max width?
body: <p>Please consider this modified, but shamelessly <a href="https://stackoverflow.com/a/16258375/2103880">filched</a> code:</p>
  
  <pre><code>library(ggplot2)
  library(gtable)
  library(gridExtra)
  p1 &lt;- ggplot(
    data.frame(
      x=c("a","b","longer"),
      y=c("happy","sad","ambivalent about")),
    aes(x=factor(0),fill=x)) + 
    geom_bar() +
    geom_point(aes(y=seq(3),color=y))
  p2 &lt;- ggplot(
    data.frame(
      x=c("a","b","c"),
      y=c("happy","sad","ambivalent about life")),
      aes(x=factor(0),fill=y)) + 
  geom_bar()
  
  # Get the widths
  gA &lt;- ggplot_gtable(ggplot_build(p1))
  gB &lt;- ggplot_gtable(ggplot_build(p2))
  
  # The parts that differs in width
  leg1 &lt;- with(gA$grobs[[8]], grobs[[1]]$widths[[4]])
  leg2 &lt;- with(gB$grobs[[8]], grobs[[1]]$widths[[4]])
  
  # Set the widths
  gA$widths &lt;- gB$widths
  
  # Add an empty column of "abs(diff(widths)) mm" width on the right of 
  # legend box for gA (the smaller legend box)
  gA$grobs[[8]] &lt;- gtable_add_cols(gA$grobs[[8]], unit(abs(diff(c(leg1, leg2))), "mm"))
  
  # Arrange the two charts
  grid.newpage()
  grid.arrange(gA, gB, nrow = 2)
  </code></pre>
  
  <p>Under these conditions the placement of the legends does not work as wanted as <code>gA$grobs[[8]]</code> has 2 entries and the code accesses explicitly the first one to determine the needed legend adjustment.</p>
  
  <p>What I accordingly want to do is iterate over all entries in <code>gA$grobs[[8]]</code> and find the maximal width to use.</p>
  
  <p>BTW:</p>
  
  <pre><code>library(gtable)
  a &lt;- gtable(unit(1:3, c("cm")), unit(5, "cm"))
  a # See, "TableGrob" exists (somewhat) ;)
  </code></pre>
  
  <p>I hope this clears up what I intend to do.</p>
  
  <p>Thanks for any pointers, Joh</p>