title: Define a fixed width/height to a bar chart, then set an absolute width to bars, and absolute space between bars, in pixels
body: <p>I want to generate bar charts using <code>ggplot2</code> that will follow a strict standard:</p>
  <ul>
  <li>they must be of certain dimensions (width and height)</li>
  <li>the width of bars must be fixed, in pixels, regardless of how many bars happen to be in the plot</li>
  <li>the space <em>between</em> bars has to be fixed, in pixels, regardless of number of bars</li>
  </ul>
  <p>I work with RStudio, that allows responsiveness in its viewer. That means that when I expand the viewer's boundaries, the plot is stretched accordingly, increasing bars' width and spaces between them. Conversely, making the viewer's boundaries smaller would make the bars thinner and reduce space between them.</p>
  <p>Similarly, in given boundaries of the viewer, plotting a bar chart would yield different bar width for 6 bars than when there are 2 bars only.</p>
  <h2>Demonstration</h2>
  <pre class="lang-r prettyprint-override"><code>library(ggplot2)
  library(dplyr)
  
  p_all_bars &lt;- 
    mpg %&gt;%
    ggplot(aes(x = class)) +
    geom_bar()
  
  p_two_bars &lt;-
    mpg %&gt;%
    filter(class == &quot;compact&quot; | class == &quot;suv&quot;) %&gt;%
    ggplot(aes(x = class)) +
    geom_bar()
  </code></pre>
  <pre class="lang-r prettyprint-override"><code>p_all_bars
  </code></pre>
  <p><a href="https://i.stack.imgur.com/niECn.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/niECn.png" alt="p_all_bars" /></a></p>
  <pre class="lang-r prettyprint-override"><code>p_two_bars
  </code></pre>
  <p><a href="https://i.stack.imgur.com/IrgoO.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IrgoO.png" alt="p_two_bars" /></a></p>
  <hr />
  <p>If I save <strong>both</strong> plots with dimensions of <code>width = 1000 pixels</code> and <code>height = 650 pixels</code> it's clear that both bar width <strong>and</strong> space between bars are different from one plot (7 bars) to another (2 bars).</p>
  <p><a href="https://i.stack.imgur.com/xjsGV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xjsGV.png" alt="all_7" /></a></p>
  <hr />
  <p><a href="https://i.stack.imgur.com/abi7a.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/abi7a.png" alt="2_bars" /></a></p>
  <h2>Bottom line</h2>
  <p>How can I set absolute values for plot's height and width, in pixels, as well for bars' width and space between bars, in pixels too -- regardless of number of bars in the plot?</p>