简体   繁体   中英

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).

在此处输入图片说明

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))

在此处输入图片说明

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.

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:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM