title: Adding sub-tables on each panel of a facet ggplot in r
body: <p>I am trying to create a complete set of plots using <code>facet_wrap</code> from the ggplot2 package in R.</p>
  
  <p>As a simplified example I used a subset of the dataset mpg included in ggplot2</p>
  
  <pre><code>library(plyr)
  library(ggplot2)
  library(gtable)
  library(gridExtra)
  
  myData = subset(mpg, manufacturer == "audi" | manufacturer == "chevrolet")
  myData = droplevels(myData)
  </code></pre>
  
  <p>Here is my code to plot the data:</p>
  
  <pre><code>p =  ggplot(myData, aes(x=hwy, y=cty, colour=model) )
  p = p + facet_wrap( ~ manufacturer)#, scales="free") # sets panel division 
  p = p + geom_point(size = 3) # sets points aspect
  p = p + geom_smooth(stat="identity")
  print(p)
  </code></pre>
  
  <p>Now here comes the tricky part... I have another dataframe 'indivParam' with extra information that I would like to display as a table on the plot. Let's say this silly one: </p>
  
  <pre><code>indivParam = ddply(myData, .(manufacturer  ,  model), summarize,
                 var1 = unique(class),
                 var2 = round(mean(displ)),
                 var3 = round(mean(cyl)))
  </code></pre>
  
  <p>What I am trying to do is to add a sub-table on each panel, with info extracted from indivParam. For example add the following table on the first panel of the plot:</p>
  
  <pre><code>tg = tableGrob(subset(indivParam, manufacturer == "audi"),
             show.rownames=FALSE, gp=gpar(fontsize=8, lwd=2), 
             xmin=15, xmax=30, ymin=10, ymax=20)
  grid.newpage()
  grid.draw(tg)
  </code></pre>
  
  <p>I tried several options...</p>
  
  <ol>
  <li><p>using <code>annotate()</code> but this argument does not pass dataframes...</p></li>
  <li><p>using <code>annotation_custom()</code> as suggested in this thread : <a href="https://stackoverflow.com/questions/12318120/adding-table-within-the-plotting-region-of-a-ggplot-in-r">Adding table within the plotting region of a ggplot in r</a></p>
  
  <pre><code>p1 = p + annotation_custom(tableGrob(indivParam,
                              show.rownames=FALSE,
                              gp=gpar(fontsize=8, lwd=2)), 
                    xmin=15, xmax=30, ymin=10, ymax=20)
  print(p1)
  </code></pre>
  
  <p>This does not work either because it displays the entire table on each panel, instead of a sub-table with data related to each panel ()</p></li>
  <li><p>Finally, after reading the examples on the <a href="http://www.inside-r.org/packages/cran/gridExtra/docs/arrangeTableGrobs" rel="nofollow noreferrer">'tableGrob' doc page</a>, I tried to create one grid with all the sub-table grobs and simply superimpose it on the plot:</p>
  
  <pre><code>lg &lt;- lapply(as.character(unique(indivParam$manufacturer)),
       function(x) tableGrob( as.data.frame(dlply(indivParam, .(manufacturer))[x]),
                              name="test",show.rownames=FALSE,
                              gp=gpar(fontsize=8, lwd=2)))
  grid.newpage()
  print(p)
  grid.draw(do.call(arrangeGrob, lg))
  </code></pre>
  
  <p>But then, the organization does not match the one used by facet.., and I suspect that even if I could put the two tables next to each other, they would be centered and would hide the plots...</p></li>
  </ol>
  
  <p>Is there any way I could improve this last attempt by choosing the position of the sub-tables? Or is there an even better way to solve this issue? An  obvious one would be to use a <code>geom_table()</code> but I don't think this geom exist (yet)...</p>
  
  <p>Any help/hint will be much appreciated!! :-)</p>