Note that there are some explanatory texts on larger screens.

plurals
  1. POTop to bottom alignment of two ggplot2 figures
    primarykey
    data
    text
    <p>I realize that the <code>align.plots</code> function from the <code>ggExtra</code> package has been deprecated and removed. However, I am using my own version as it seems to provide the specific functionality I need. I have looked into faceting to solve my problem but I don't think it will work for my particular issue. What seems to be the problem is that the top-to-bottom images don't align when I use <code>coord_equal</code> on one of them. This doesn't seem to affect left-to-right though. Here is a simplified (or at least as simple as I can make it) version of what I am trying to achieve.</p> <p>Create some dummy data frames:</p> <pre><code>source('https://raw.github.com/jbryer/multilevelPSA/master/r/align.R') require(psych) df = data.frame(x=rnorm(100, mean=50, sd=10), y=rnorm(100, mean=48, sd=10), group=rep(letters[1:10], 10)) dfx = describe.by(df$x, df$group, mat=TRUE)[,c('group1', 'mean', 'n', 'min', 'max')] names(dfx) = c('group', 'x', 'x.n', 'x.min', 'x.max') dfy = describe.by(df$y, df$group, mat=TRUE)[,c('group1', 'mean', 'n', 'min', 'max')] names(dfy) = c('group', 'y', 'y.n', 'y.min', 'y.max') df2 = cbind(dfx, dfy[,2:ncol(dfy)]) range = c(0,100) </code></pre> <p>This will setup the three plots:</p> <pre><code>p1a = ggplot(df2, aes(x=x, y=y, colour=group)) + geom_point() + opts(legend.position='none') + scale_x_continuous(limits=range) + scale_y_continuous(limits=range) p1 = p1a + coord_equal(ratio=1) p2 = ggplot(df, aes(x=x, y=group, colour=group)) + geom_point() + scale_x_continuous(limits=range) + opts(legend.position='none') p3 = ggplot(df, aes(x=group, y=y, colour=group)) + geom_point() + scale_y_continuous(limits=range) + opts(legend.position='none') </code></pre> <p>The alignment top to bottom does not work with coord_equal</p> <pre><code>grid_layout &lt;- grid.layout(nrow=2, ncol=2, widths=c(1,2), heights=c(2,1)) grid.newpage() pushViewport( viewport( layout=grid_layout, width=1, height=1 ) ) align.plots(grid_layout, list(p1, 1, 2), list(p3, 1, 1), list(p2, 2, 2)) </code></pre> <p><a href="http://bryer.org/alignplots1.png" rel="nofollow noreferrer">Broken Plot http://bryer.org/alignplots1.png</a></p> <p><strong>The fix is to add <code>respect=TRUE</code> to the <code>grid.layout</code> call:</strong></p> <pre><code>grid_layout &lt;- grid.layout(nrow=2, ncol=2, widths=c(1,2), heights=c(2,1), respect=TRUE) </code></pre> <p>But if I don't use coord_equal the alignment works fine:</p> <pre><code>grid_layout &lt;- grid.layout(nrow=2, ncol=2, widths=c(1,2), heights=c(2,1)) grid.newpage() pushViewport( viewport( layout=grid_layout, width=1, height=1 ) ) align.plots(grid_layout, list(p1a, 1, 2), list(p3, 1, 1), list(p2, 2, 2)) </code></pre> <p><a href="http://bryer.org/alignplots2.png" rel="nofollow noreferrer">Working Plot http://bryer.org/alignplots2.png</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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