title: ggplot2: center legend below plot instead of panel area
body: <p><code>ggplot</code> by default centers the legend below the panel, which is really frustrating in some situations. Please see the below example:</p>
  
  <pre><code>ggplot(diamonds, aes(cut, fill = clarity)) + geom_bar() + coord_flip() + 
    theme(legend.position = 'bottom')
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/hHhGF.png" alt="enter image description here"></p>
  
  <p>It can be seen that the last label was cropped from the image, although we have some white-space on the left part of the legend -- it would be a lot better to use that.</p>
  
  <p><strong>Q</strong>: how can I center the legend below the plot and not try to force that to be centered below the panel area to overcome this issue?</p>
  
  <hr>
  
  <p><strong>Update</strong>: further example on this issue:</p>
  
  <pre><code>df &lt;- data.frame(
      x = sample(paste('An extremely long category label that screws up legend placement', letters[1:7]), 1e3, TRUE),
      y = sample(paste('Short label', letters[1:7]), 1e3, TRUE)
      )
  
  ggplot(df, aes(x, fill = y)) + geom_bar() + coord_flip() +
    theme(legend.position = 'bottom')
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/DUejY.png" alt="enter image description here"></p>
  
  <hr>
  
  <p><strong>What I've done:</strong> I can use <code>legend.direction</code> to manually put the legend below the panel and to add some extra margin below the plot, like:</p>
  
  <pre><code>ggplot(diamonds, aes(cut, fill = clarity)) + geom_bar() + coord_flip() + 
    theme(legend.position  = c(0.37, -0.1),
          legend.direction = 'horizontal',
          plot.margin      = grid::unit(c(0.1,0.1,2,0.1), 'lines'))
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/h1daC.png" alt="enter image description here"></p>
  
  <p>But this way I have to compute "manually" the optimal <code>legend.position</code>. Any suggestions?</p>
  
  <hr>
  
  <p><strong>Update</strong>: I am arranging multiple plots next to each other, so I'd rather not center the legend on the actual image, but rather on the single panel. E.g.:</p>
  
  <pre><code>p1 &lt;- ggplot(diamonds, aes(cut)) + geom_histogram()
  p2 &lt;- ggplot(diamonds, aes(cut, fill = clarity)) + geom_bar() + coord_flip() +
    theme(legend.position = 'bottom')
  pushViewport(viewport(layout = grid.layout(nrow = 1, ncol = 2, widths = unit(c(1, 2), c("null", "null")))))
  print(p1, vp = viewport(layout.pos.row = 1, layout.pos.col = 1))
  print(p2, vp = viewport(layout.pos.row = 1, layout.pos.col = 2))
  </code></pre>