简体   繁体   English

ggarrange:使绘图(不包括标题、轴标题、轴标签)大小相同

[英]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 .我正在尝试使用单独的图和ggarrange重新创建facet_grid的样式。 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)?使用ggarrange函数的高度和宽度参数允许我使所有绘图的大小相同,但这包括标题、轴标题等。 有没有办法使绘图本身具有相同的大小(其他试错宽度相对于高度和宽度)?

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.或者,我这样做的原因是因为每个图表上的色阶需要不同,所以如果有办法使用facet_grid来做到这一点也会有所帮助。 And a more general question could be what is the best way to replicate the facet_grid layout with individual plots.一个更普遍的问题可能是用单个图复制facet_grid布局的最佳方法是什么。

Try this solution with patchwork .试试这个patchwork解决方案。 Set yur plots in a list and wrap into a composed plot using wrap_plots() function:在列表中设置你的图并使用wrap_plots()函数包装成一个组合图:

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:输出将被调整以保持相同的尺寸:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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