4

enter image description hereI am trying to plot two side by side plots with ggplot. However, as the two data sets contain different number of observations, the width of the grid is automatically adjusted so that the overall width of the two plots is the same.

However, I need to have the same width between each vertical line-segment on the plot and different total widths to reflect the different sample size in the two cases.

Thanks in advance for any advice.

Here is my code

dat1 <- data.frame(a=1:10,b=letters[1:10])
dat2 <- data.frame(a=1:6, b=letters[12:17])

require(gridExtra)
plot1 <- ggplot(dat1, aes(x=b, y=a)) + geom_point(size=4)
plot2 <- ggplot(dat2, aes(x=b, y=a)) + geom_point(size=4)
grid.arrange(plot1, plot2, ncol=2)
ZMacarozzi
  • 697
  • 1
  • 5
  • 11
  • 2
    `grid.arrange(plot1, plot2, ncol=2, widths=c(10,6))` – eipi10 Jul 01 '16 at 15:19
  • 2
    Also, isn't the library `gridExtra` and not `grid.extra`? – Twitch_City Jul 01 '16 at 15:20
  • great thanks so much, I really appreciate the help; – ZMacarozzi Jul 01 '16 at 15:24
  • also thanks for pointing out the library issue, i have fixed the question text now – ZMacarozzi Jul 01 '16 at 15:24
  • FYI: (1) If you're going to do this a lot, instead of hardocding `widths` you can set this programmatically by the number of observations in each panel. For example, `widths = c(length(dat1$a), length(dat2$a))`. (2) The code in my comment won't give you exactly a 10:6 ratio in the plot area, because the widths of the axis labels and ticks are included. To get an exact ratio, you could use [something like this SO answer](http://stackoverflow.com/a/13295880/496488) and play around with the component widths. – eipi10 Jul 01 '16 at 15:26
  • thanks a lot, i also tried to fix the widths like you suggested just I used the nrow(dat) and it works just fine - the only thing I realized is that it counts the legend as part of the width of the plot (the data I use are more complex so there is also a legend next to the plot), so now I should move it underneath. For the other solution I will definitely look into it too, thanks again – ZMacarozzi Jul 01 '16 at 15:34

1 Answers1

2

You can set widths in grid.arrange(). The following will automatically adjust the width of the graphs based on the number of levels in each data frame.

I renamed your variables so that row name and elements are not repeated:

dat1 <- data.frame(A1=1:10,B1=letters[1:10])
dat2 <- data.frame(A2=1:6, B2=letters[12:17])

Load the necessary libraries:

require(gridExtra)
require(ggplot2)

Now plot:

plot1 <- ggplot(dat1, aes(x=B1, y=A1)) + geom_point(size=4)
plot2 <- ggplot(dat2, aes(x=B2, y=A2)) + geom_point(size=4)

grid.arrange(plot1, plot2, ncol=2, 
         widths=c(nlevels(dat1$B1),nlevels(dat2$B2)))

Which gives you the following plot:

enter image description here

As you can see, each level is plotted equidistant from each other across both plots.

user20650
  • 24,654
  • 5
  • 56
  • 91
Dave Gruenewald
  • 5,329
  • 1
  • 23
  • 35