Note that there are some explanatory texts on larger screens.

plurals
  1. POggplot2 + gridExtra: how to ensure geom_bar in different size plot grobs result in exact same bar width
    text
    copied!<p>This question is motivated by further exploring this <a href="https://stackoverflow.com/questions/11301738/ggplot2-how-to-drop-unused-factors-in-the-axis-in-a-faceted-bar-plot-but-not-ha">question</a>. The problem with the accepted solution becomes more obvious when there is a greater disparity in the number of bars per facet. Take a look at this data and the resultant plot using that solution:</p> <pre><code># create slightly contrived data to better highlight width problems data &lt;- data.frame(ID=factor(c(rep(1,9), rep(2,6), rep(3,6), rep(4,3), rep(5,3))), TYPE=factor(rep(1:3,length(ID)/3)), TIME=factor(c(1,1,1,2,2,2,3,3,3,1,1,1,2,2,2,1,1,1,2,2,2,1,1,1,1,1,1)), VAL=runif(27)) # implement previously suggested solution base.width &lt;- 0.9 data$w &lt;- base.width # facet two has 3 bars compared to facet one's 5 bars data$w[data$TIME==2] &lt;- base.width * 3/5 # facet 3 has 1 bar compared to facet one's 5 bars data$w[data$TIME==3] &lt;- base.width * 1/5 ggplot(data, aes(x=ID, y=VAL, fill=TYPE)) + facet_wrap(~TIME, ncol=1, scale="free") + geom_bar(position="stack", aes(width = w),stat = "identity") + coord_flip() </code></pre> <p><img src="https://dzwonsemrish7.cloudfront.net/items/1A432A2J3r2r3y2Y1P0m/Image%202012-07-22%20at%209.05.37%20PM.png" alt="widths all the same but spacing is bad"></p> <p>You'll notice the widths look exactly right, but the whitespace in facet 3 is quite glaring. There is no easy way to fix this in ggplot2 that I have seen yet (facet_wrap does not have a <code>space</code> option).</p> <p>Next step is to try to solve this using gridExtra:</p> <pre><code># create each of the three plots, don't worry about legend for now p1 &lt;- ggplot(data[data$TIME==1,], aes(x=ID, y=VAL, fill=TYPE)) + facet_wrap(~ TIME, ncol=1) + geom_bar(position="stack", show_guide=FALSE) + coord_flip() p2 &lt;- ggplot(data[data$TIME==2,], aes(x=ID, y=VAL, fill=TYPE)) + facet_wrap(~ TIME, ncol=1) + geom_bar(position="stack", show_guide=FALSE) + coord_flip() p3 &lt;- ggplot(data[data$TIME==3,], aes(x=ID, y=VAL, fill=TYPE)) + facet_wrap(~ TIME, ncol=1) + geom_bar(position="stack", show_guide=FALSE) + coord_flip() # use similar arithmetic to try and get layout correct require(gridExtra) heights &lt;- c(5, 3, 1) / sum(5, 3, 1) print(arrangeGrob(p1 ,p2, p3, ncol=1, heights=heights)) </code></pre> <p><img src="https://dzwonsemrish7.cloudfront.net/items/2p1E303z112a3J2p2N3j/Image%202012-07-22%20at%209.12.44%20PM.png" alt="widths wrong"></p> <p>You'll notice I used the same arithmetic previously suggested based off the number of bars per facet, but in this case it ends up horribly wrong. This seems to be because there are extra "constant height" elements that I need to take into consideration in the math. </p> <p>Another complication (I believe) is that the final output (and whether or not the widths match) will also depend on the width and height of where I'm outputting the final grob to, whether its in a R/RStudio environment, or to a PNG file.</p> <p>How can I accomplish this?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload