align_plots()
Published

August 7, 2025

Plot Composer - Inset

library(ggalign)
#> Loading required package: ggplot2
#> 
#> Attaching package: 'ggalign'
#> The following object is masked from 'package:ggplot2':
#> 
#>     element_polygon
library(grid)
# Create the main scatter plot: mpg vs disp
p1 <- ggplot(mtcars) +
    geom_point(aes(mpg, disp))

# Create the inset plot: boxplot of disp by gear
p2 <- ggplot(mtcars) +
    geom_boxplot(aes(gear, disp, group = gear))

# Compose the plots: insert p2 into p1 using a viewport
p1 + inset(p2, vp = viewport(0.6, 0.6,
    just = c(0, 0), width = 0.4, height = 0.4
))