Search code examples
rggplot2facet-wrapfacet-grid

ggarrange: Make plots (excluding titles, axis titles, axis labels) the same size


I'm trying to recreate the style of facet_grid with individual plots and ggarrange. Using the height and width arguments of the ggarrange function allows me to make all the plots the same size but this includes the titles, axis titles etc. Is there a way to make the plots themselves the same size (other trial and error width the relative heights and widths)?

Example

library(ggplot2)
library(ggpubr)

df <- data.frame(x=runif(100), y=runif(100))

p1 <- ggplot(df) + geom_point(aes(x=x, y=y)) + theme(axis.title = element_text(size=100))
p2 <- p1 + xlab(NULL)
p3 <- p1 + ylab(NULL)
p4 <- p1 + xlab(NULL) + ylab(NULL)

p <- ggarrange(p2, p4, p1, p3, nrow=2, ncol=2)

print(p)

This makes the following plot, where the top right plot appears larger than the others (although including the axis titles they're all the same size).

enter image description here

Something like this seems to get closer to the desired outcome:

p_fixed <- ggarrange(p2, p4, p1, p3, nrow=2, ncol=2,
                     heights = c(1, 1.3, 1, 1.3),
                     widths=c(1.1, 1, 1.1, 1))

enter image description here

Alternatively, the reason I'm doing this is because the colour scales on each chart need to be different, so if there's a way to do that using facet_grid that would help too. And a more general question could be what is the best way to replicate the facet_grid layout with individual plots.


Solution

  • Try this solution with patchwork. Set yur plots in a list and wrap into a composed plot using wrap_plots() function:

    library(ggplot2)
    library(ggpubr)
    library(patchwork)
    #Data
    df <- data.frame(x=runif(100), y=runif(100))
    #Plots
    p1 <- ggplot(df) + geom_point(aes(x=x, y=y)) + theme(axis.title = element_text(size=100))
    p2 <- p1 + xlab(NULL)
    p3 <- p1 + ylab(NULL)
    p4 <- p1 + xlab(NULL) + ylab(NULL)
    #List
    List <- list(p2,p4,p1,p3)
    #Plot
    Plot <- wrap_plots(List,ncol = 2,nrow = 2)
    

    The output will be adjusted to keep same dimensions:

    enter image description here