title: Create legend for subsets of a bar chart
body: <p>How to create a legend for the folowing plot and marginals? I searched trough <code>?theme</code> but I get no idea.</p>
  
  <pre><code>library(ggplot2)
  data &lt;- data.frame(col1 = c(1, 2, 2, 3, 3, 4), col2 = c(1, 2, 2, 1, 2, 1), z = rnorm(6))
  
  p1 &lt;- ggplot(data, aes(col1, y = ..count..,fill=col2))
  p1 &lt;- p1 + geom_bar()
  
  p2 &lt;- ggplot(data, aes(x = z))
  p2&lt;-p2 + geom_density()
  p2
  
  gt1 &lt;- ggplot_gtable(ggplot_build(p1))
  gt2 &lt;- ggplot_gtable(ggplot_build(p2))
  </code></pre>
  
  <p>Get maximum widths and heights for x-axis and y-axis title and text</p>
  
  <pre><code>maxWidth = unit.pmax(gt1$widths[2:3], gt2$widths[2:3])
  </code></pre>
  
  <p>Set the maximums in the gtables for gt1, gt2 and gt3</p>
  
  <pre><code>gt1$widths[2:3] &lt;- as.list(maxWidth)
  gt2$widths[2:3] &lt;- as.list(maxWidth)
  </code></pre>
  
  <p>Combine the scatterplot with the two marginal boxplots
  Create a new gtable</p>
  
  <pre><code>gt &lt;- gtable(widths = unit(c(7, 2), "null"), height = unit(c(2, 7), "null"))
  </code></pre>
  
  <p>Instert gt1, gt2 and gt3 into the new gtable</p>
  
  <pre><code>gt &lt;- gtable_add_grob(gt, gt1, 2, 1)
  gt &lt;- gtable_add_grob(gt, gt2, 1, 1)
  </code></pre>
  
  <p>And render the plot</p>
  
  <pre><code>grid.newpage()
  grid.draw(gt)
  </code></pre>