4.11 Patchwork with Venndir

The patchwork (Pedersen 2024) R package is widely used to assemble multi-panel R graphics, due to its ability to include plots from base R, ggplot2, and grid graphics. Venndir is produced using grid graphics, and is compatible with patchwork with some caveats.

  1. Venndir fonts are absolute point sizes, and should be adjusted before plotting.
  2. The grid objects must be used with patchwork, see Venndir gtree.
library(patchwork)
suppressPackageStartupMessages(library(ggplot2))

setlist <- make_venn_test(do_signed=TRUE)
v <- venndir(setlist, sets=c(1, 2), do_draw=FALSE)
v_gtree <- attr(v, "gtree")
v2 <- venndir(setlist, sets=c(1, 3), do_draw=FALSE)
v2_gtree <- attr(v2, "gtree")

gg <- ggplot(mtcars) + 
  geom_point(aes(mpg, disp)) + 
  ggtitle('ggplot2 example')

pw3 <- (gg + wrap_elements(v_gtree)) /
   (wrap_elements(v2_gtree) + gg) +
   plot_layout(widths=c(1, 1), heights=c(1, 1)) +
   plot_annotation(tag_levels='a')
pw3
Four panel figure produced using patchwork R package.

Figure 4.19: Four panel figure produced using patchwork R package.

References

Pedersen, Thomas Lin. 2024. Patchwork: The Composer of Plots. https://patchwork.data-imaginist.com.