title: ggplot2: Flip axes and maintain aspect ratio of data
body: <p>In ggplot2, the <code>coord_fixed()</code> coordinate system ensures that the aspect ratio of the data is maintained at a given value. So, the shape of the panel changes to maintain the shape of the data. Meanwhile <code>coord_flip()</code> swaps the axes of the plot. However, a plot in ggplot2 must have exactly one coordinate system, so these functions cannot be combined. </p>
  
  <p>My question is:</p>
  
  <blockquote>
    <p>Does there exist a way to combine the behaviours of <code>coord_fixed()</code> and <code>coord_flip()</code>, resulting in a coordinate system with the x and y axes exchanged and a fixed aspect ratio of the data? </p>
  </blockquote>
  
  <p>This is a popular question, however the common answer is incorrect:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/35799426/how-do-i-to-fix-aspect-ratio-and-apply-coord-flip-in-ggplot2">How do I to fix aspect ratio and apply coord_flip in ggplot2?</a></li>
  <li><a href="https://stackoverflow.com/questions/30011410/flipping-and-maintaining-aspect-ratio-of-a-chart-in-ggplot2">Flipping and maintaining aspect ratio of a chart in ggplot2</a></li>
  </ul>
  
  <p>The commonly suggested answer is to use <code>coord_flip()</code> together with <code>theme(aspect.ratio = 1)</code> instead of <code>coord_fixed()</code>. However, as per the ggplot2 documentation, this setting refers to the "aspect ratio of the panel." Thus, the data will change shape to maintain the shape of the panel.</p>
  
  <p>I suspect that this is a feature that does not currently exist in ggplot2. But more importantly I think that a correct solution or at least response to this question should be documented.</p>
  
  <p>Quick minimal example of the issue:</p>
  
  <pre><code>library(ggplot2)
  x &lt;- 1:100; data &lt;- data.frame(x = x, y = x * 2)
  p &lt;- ggplot(data, aes(x, y)) + geom_point()
  
  p # by default panel and data both fit to device window
  p + coord_fixed() # panel changes shape to maintain shape of data
  p + theme(aspect.ratio = 1) # data changes shape to maintain shape of panel
  p + coord_fixed() + coord_flip() # coord_flip() overwrites coord_fixed()
  
  # popular suggested answer does not maintain aspect ratio of data:
  p + coord_flip() + theme(aspect.ratio = 1)
  </code></pre>