Setting absolute size of facets in ggplot2
I've used the following function for this purpose,
set_panel_size <- function(p=NULL, g=ggplotGrob(p), file=NULL, margin = unit(1,"mm"), width=unit(4, "cm"), height=unit(4, "cm")){ panels <- grep("panel", g$layout$name) panel_index_w<- unique(g$layout$l[panels]) panel_index_h<- unique(g$layout$t[panels]) nw <- length(panel_index_w) nh <- length(panel_index_h)if(getRversion() < "3.3.0"){ # the following conversion is necessary # because there is no `[<-`.unit method # so promoting to unit.list allows standard list indexing g$widths <- grid:::unit.list(g$widths) g$heights <- grid:::unit.list(g$heights) g$widths[panel_index_w] <- rep(list(width), nw) g$heights[panel_index_h] <- rep(list(height), nh)} else { g$widths[panel_index_w] <- rep(width, nw) g$heights[panel_index_h] <- rep(height, nh)} if(!is.null(file)) ggsave(file, g, width = grid::convertWidth(sum(g$widths) + margin, unitTo = "in", valueOnly = TRUE), height = grid::convertHeight(sum(g$heights) + margin, unitTo = "in", valueOnly = TRUE)) g}g1 <- set_panel_size(plot1)g2 <- set_panel_size(plot2)gridExtra::grid.arrange(g1,g2)