Compose and align plots with ggplus()

Align a 2 × 2 grid with ggplus()
Base R, Lattice and two ggplot2 styles show the same height and weight data. Set nrow to 2 and use gap to add 4 mm between columns and 6 mm between rows. The data panels align in both directions.

Install gridGraphics for Base R capture. Pass the four original plots to ggplus(). With nrow = 2, ggplus() works out the number of columns. Set gap = c(x = 4, y = 6) for 4 mm between columns and 6 mm between rows.
library(ggforma)
library(ggplot2)
library(lattice)
library(patchwork)
# Install gridGraphics for Base R capture.
p1 <- ~plot(women)
p2 <- xyplot(weight ~ height, data = women)
p3 <- ggplot(women, aes(height, weight)) + geom_point()
p4 <- p3 + theme_forma()
figure <- ggplus(p1, p2, p3, p4, nrow = 2, align = "panel",
gap = c(x = 4, y = 6))
# Enter figure in the R console to preview it.
render_example <- function(file, device = ragg::agg_png) {
ggsave(file, figure, width = 190, height = 150, units = "mm",
device = device, dpi = 144, bg = "white")
}
png.file <- file.path(tempdir(), "composition-alignment.png")
render_example(png.file)
png.fileBase R drawings start with compact margins. Any par() settings in your drawing code take precedence. A panel narrower or shorter than 15 mm triggers a warning with its size and the limiting plots.
Preview and export the same figure
Enter this line in the R console. The panels are measured again when the Plots pane changes size.
figure
Export the same object with the overall figure dimensions. No per-panel slot is needed.
ggsave("women-aligned.png", figure, width = 190, height = 150,
units = "mm", dpi = 144, device = ragg::agg_png)
ggsave("women-aligned.svg", figure, width = 190, height = 150,
units = "mm", device = svglite::svglite)
ggsave("women-aligned.pdf", figure, width = 190, height = 150,
units = "mm", device = grDevices::cairo_pdf)
Keep a fixed size or use patchwork operators
Use align_forma() when each child needs a fixed physical width and height or when you want to inspect the measured edges. Use | for a row, / for a stack and + to add an annotation. You can also combine an entire ggplus() grid with another plot.
plan <- align_forma(p1, p2, p3, p4, axis = "both", slot = c(80, 65))
p <- plan$plots
fixed <- ((p[[1]] | p[[2]]) / (p[[3]] | p[[4]])) +
plot_annotation(title = "Height and weight")
ggsave("women-fixed.pdf", fixed, width = 174, height = 148,
units = "mm", device = grDevices::pdf)
Align a radar chart with a Base R scatter plot
The radar compares average scores for two clinic networks. The Base R scatter plot shows scores by clinic. The radar stays circular, and the shaded data panels line up at the top and bottom.

Use a one-sided formula for the Base R scatter plot. Install gridGraphics for Base R capture.
library(ggforma)
library(ggplot2)
clinics <- data.frame(
Network = rep(c("North", "South"), each = 8),
Coverage = c(69, 78, 85, 79, 89, 85, 82, 91, 53, 62, 64, 69, 72, 75, 77, 78),
Testing = c(60, 62, 65, 70, 73, 65, 72, 75, 68, 70, 74, 75, 74, 82, 83, 89),
Outreach = c(77, 83, 86, 82, 89, 88, 78, 85, 70, 76, 78, 72, 81, 77, 83, 81),
Followup = c(62, 77, 75, 63, 85, 80, 73, 88, 54, 61, 58, 69, 67, 72, 70, 73)
)
profiles <- aggregate(. ~ Network, clinics, mean)
colours <- setNames(forma_palette(9)[c(6, 7)], c("North", "South"))
radar <- ggradar(profiles, vars = c(Coverage, Testing, Outreach, Followup),
group = Network, limits = c(0, 100), grid = "circle") +
labs(title = "Average clinic profiles") +
theme(panel.background = element_rect(fill = "#EEF6F4", colour = NA))
# Install gridGraphics for Base R capture.
scatter <- ~{
par(mar = c(3.6, 3.8, 2.5, 0.8), mgp = c(2.15, 0.65, 0),
las = 1, cex = 0.85, tcl = -0.2)
plot(Followup ~ Coverage, data = clinics, type = "n", bty = "n",
main = "Clinic scores · Base R", xlab = "Coverage score", ylab = "Follow-up score")
region <- par("usr")
rect(region[1], region[3], region[2], region[4], col = "#F1F5FA", border = NA)
points(Followup ~ Coverage, data = clinics, pch = 16, col = colours[clinics$Network])
}
figure <- ggplus(radar, scatter, nrow = 1, align = "panel", gap = 4)
# Enter figure in the R console to preview it.
render_example <- function(file, device = ragg::agg_png) {
ggsave(file, figure, width = 150, height = 110, units = "mm",
device = device, dpi = 144, bg = "white")
}
png.file <- file.path(tempdir(), "composition-radar-base.png")
render_example(png.file)
png.fileUse nrow = 1, align = "panel" and gap = 4. This layout pairs one single-panel fixed-aspect ggplot with one flexible plot. The two plots can swap places. The 4 mm gap adds space between the cells, alongside room for axes and margins.
Align a radar chart with a Lattice scatter plot
The radar compares average scores for two clinic networks. The Lattice scatter plot shows scores by clinic. The radar stays circular, and the shaded data panels line up at the top and bottom.

Pass the original xyplot() object and set aspect = "fill" so the scatter panel can adjust its height.
library(ggforma)
library(ggplot2)
library(lattice)
library(grid)
clinics <- data.frame(
Network = rep(c("North", "South"), each = 8),
Coverage = c(69, 78, 85, 79, 89, 85, 82, 91, 53, 62, 64, 69, 72, 75, 77, 78),
Testing = c(60, 62, 65, 70, 73, 65, 72, 75, 68, 70, 74, 75, 74, 82, 83, 89),
Outreach = c(77, 83, 86, 82, 89, 88, 78, 85, 70, 76, 78, 72, 81, 77, 83, 81),
Followup = c(62, 77, 75, 63, 85, 80, 73, 88, 54, 61, 58, 69, 67, 72, 70, 73)
)
profiles <- aggregate(. ~ Network, clinics, mean)
colours <- setNames(forma_palette(9)[c(6, 7)], c("North", "South"))
radar <- ggradar(profiles, vars = c(Coverage, Testing, Outreach, Followup),
group = Network, limits = c(0, 100), grid = "circle") +
labs(title = "Average clinic profiles") +
theme(panel.background = element_rect(fill = "#EEF6F4", colour = NA))
scatter <- xyplot(Followup ~ Coverage, data = clinics, aspect = "fill",
main = "Clinic scores · Lattice",
xlab = "Coverage score", ylab = "Follow-up score",
panel = function(x, y, subscripts, ...) {
grid.rect(gp = gpar(fill = "#F1F5FA", col = NA))
panel.xyplot(x, y, pch = 16, col = colours[clinics$Network[subscripts]], ...)
}, par.settings = list(axis.line = list(col = "transparent")))
figure <- ggplus(radar, scatter, nrow = 1, align = "panel", gap = 4)
# Enter figure in the R console to preview it.
render_example <- function(file, device = ragg::agg_png) {
ggsave(file, figure, width = 150, height = 110, units = "mm",
device = device, dpi = 144, bg = "white")
}
png.file <- file.path(tempdir(), "composition-radar-lattice.png")
render_example(png.file)
png.fileUse nrow = 1, align = "panel" and gap = 4. This layout pairs one single-panel fixed-aspect ggplot with one flexible plot. The two plots can swap places. The 4 mm gap adds space between the cells, alongside room for axes and margins.