ikashnitsky ikashnitsky - 9 months ago 45
R Question

How to align an ordinary ggplot with a faceted one in cowplot?

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)


The code produces this plot.
enter image description here

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?

Answer Source

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)

library("devtools")
install_github("baptiste/egg")

Then simply do

library("egg")
ggarrange(gg1, gg2, ncol=2)

You can add labels manually:

ap <- ggarrange(gg1,gg2, ncol=2)
ggdraw(ap) + draw_plot_label(label=c("a","b"), x=c(0,0.5), y=c(1,1))

(When I tried to first add the labels to the individual plots, the plots didn't get arranged properly.)