3

I would like to know how to arrange one grob object (a table) inside another. Consider the following example:

library(ggplot2)
library(gridExtra)

p1 <- qplot(data=mtcars, x = mpg, y = hp, facets = ~ hp, geom="point")
t1 <- tableGrob(head(mtcars))
print(arrangeGrob(p1, t1))

This yields:

Plot 01

What I would like to do is place the table inside the other object like this:

Desired Result

Is this possible to do using gridArrange or perhaps some other methods from grid and/or gridExtra?

JasonAizkalns
  • 20,243
  • 8
  • 57
  • 116
  • 1
    [**This**](http://stackoverflow.com/questions/12318120/adding-table-within-the-plotting-region-of-a-ggplot-in-r/12318578#12318578) may be a start. I know there are also some posts about explicitly placing 'something' in an empty panel in a facetted plot. Can't find them right now... – Henrik May 08 '15 at 16:41
  • 2
    This is the one I was thinking of: [**How to use empty space produced by facet_wrap?**](http://stackoverflow.com/questions/22450765/how-to-use-empty-space-produced-by-facet-wrap/22451866#22451866). – Henrik May 08 '15 at 16:47

1 Answers1

4

Here is one possibility using grid functions:

png("SO.png", width = 1440, height = 720)

plot(p1)

vp <- viewport(x = 0.95, y = 0.02, 
               width = unit(0.4, "npc"), height = unit(0.2, "npc"),
               just = c("right", "bottom"))
pushViewport(vp)
grid.draw(t1)

dev.off()

resulting plot

Roland
  • 127,288
  • 10
  • 191
  • 288