I'm trying to arrange plots in a publication-ready way with the use of cowplot
package.
I just want the panels to be equally sized and labeled.
Reproducible expample
library(ggplot2)
library(cowplot)
gg1 <- ggplot(mtcars)+
geom_point(aes(x=mpg,y=hp))+
theme_bw()+
theme(aspect.ratio=1)
gg2 <- ggplot(mtcars)+
geom_point(aes(x=mpg,y=hp,fill=cyl))+
facet_wrap(~cyl,ncol=2)+
theme_bw()+
theme(aspect.ratio=1,
legend.position='none')
output <- plot_grid(gg1,gg2, labels = c('A','B'),label_size = 20)
print(output)
As you may see, neither the horizontal axises match, nor do the upper edges of the panels.
The argument align
from cowplot
does not work with faceted plots.
Any ideas?
here's a solution based on this idea
Note that the gtable_frame function wraps plots based on their panels, but excluding the panel strips by design (I find it more pleasant).
Here's a hack until someone comes up with a more elegant answer: You can use
grid.arrange
from thegridExtra
package to change the relative sizes of the two plots so that the axes line up. Thew
parameter in the code below is what controls that by giving the left-hand plot a bit more of the horizontal width, thereby making it relatively larger, when compared with the right-hand plot.You can also use
arrangeGrob
andtextGrob
to add the "A" and "B" titles to each plot.In either case, you need to adjust
w
by hand to get the plots to line up (which is what makes this method, shall we say, sub-optimal). The appropriate value forw
will change depending on the physical size of the plot.w=0.512
seemed to work well when I saved the plot below as apng
of 1000 x 500 pixels.A better answer will probably involve something analogous to this SO answer, but adapted for lining up facetted and non-facetted plots (or, more generally, plots that don't have a one-to-one correspondence between their constituent grobs).
Since this is one of the highest voted question regarding cowplot and complex alignments, I wanted to point out that cowplot now does have some functionality for aligning faceted plots. (I'm the package author.) However, they don't work in this particular case!
For example, this works (using the
axis
option inplot_grid()
):We can also do this the following, to get a different type of alignment (depending on whether you want the facet strip to be counted as part of the plot or not):
Now why did I say it doesn't work for this case? Because, if you look at the original code in the question, you'll see that there was a
theme(aspect.ratio=1)
setting that I removed. cowplot can align plots as long as you don't force a specific aspect ratio, because the method it uses to align plots typically modifies the aspect ratio of the individual plots.Update:
egg
package is on CRAN nowhttps://cran.r-project.org/web/packages/egg/index.html
I just want to add that @baptiste has created a great experimental package egg, which accomplishes what he wrote in his answer:
Install it from github (https://github.com/baptiste/egg)
Then simply do
You can add labels manually:
(When I tried to first add the labels to the individual plots, the plots didn't get arranged properly.)