I wish to use patchwork package in R to arrange four plots:
library(ggplot2)
library(patchwork)
d1 <- runif(500)
d2 <- rep(c("Treatment","Control"),each=250)
d3 <- rbeta(500,shape1=100,shape2=3)
d4 <- d3 + rnorm(500,mean=0,sd=0.1)
plotData <- data.frame(d1,d2,d3,d4)
p1 <- ggplot(data=plotData) + geom_point(aes(x=d3, y=d4))
p2 <- ggplot(data=plotData) + geom_boxplot(aes(x=d2,y=d1,fill=d2))+
theme(legend.position="none")
p3 <- ggplot(data=plotData) +
geom_histogram(aes(x=d1, color=I("black"),fill=I("orchid")))
p4 <- ggplot(data=plotData) +
geom_histogram(aes(x=d3, color=I("black"),fill=I("goldenrod")))
# Arrange plots
{p1 + p2 + plot_layout(ncol=2, widths = c(1, 2))}/
{p3 + p4 + plot_layout(ncol = 2, widths = c(2, 1))}/
{p3 + plot_layout(ncol = 2, widths = c(2, 1))}/
{p4 + plot_layout(ncol = 2, widths = c(2, 1))}
For both row 2 and 3 of the composite figure, I consistently specified the widths
argument to be c(2, 1). However, the figure shows that the left figure in row 2 and row 3 differ in width occupied. I want to ask how should I modify the code so that the left panel in row 2-4 are of the same width.
From my experience, for more complicated layouts and patches it is worthwhile to consider specifying the layout via the design=
argument of plot_layout
or wrap_plots
which seems to work well for your case:
library(patchwork)
design <-
"
ABB
CCD
EE#
FF#
"
list(p1, p2, p3, p4, p3, p4) |>
wrap_plots(design = design)