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 uses grid and is compatible with patchwork.

There are two key steps:

  1. Call venndir() with arguments:
    • do_plot=TRUE which tells Venndir to create the graphical objects which will be drawn. By default it is already TRUE.
    • do_draw=FALSE which tells Venndir not to draw these objects. This argument is optional, to prevent drawing the figure.
    • capture the result into a variable:
      v <- venndir(..., do_draw=FALSE)
  2. Use the Venndir 'gTree' graphical objects:
    • attr(v, "gtree") contains the Venndir objects
    • Use patchwork syntax:
      wrap_elements(attr(v, "gtree"))

Figure 4.17 shows two Venn diagrams side-by-side.

library(patchwork)
suppressPackageStartupMessages(library(ggplot2))

setlist <- make_venn_test(do_signed=TRUE)
v1 <- venndir(setlist, sets=c(1, 2),
   expand_fraction=c(0.25, 0, -0.25, 0),
   do_draw=FALSE)
v2 <- venndir(setlist, sets=c(1, 3),
   expand_fraction=c(0.25, 0, -0.25, 0),
   do_draw=FALSE)

pw3 <- wrap_elements(attr(v1, "gtree")) +
   wrap_elements(attr(v2, "gtree")) +
   plot_annotation(tag_levels='a')
pw3
Two Venndir figures combined using the patchwork R package.

Figure 4.17: Two Venndir figures combined using the patchwork R package.

Tips:

  • The Venndir font sizes are absolute points, and should be adjusted accordingly.
  • The Venndir circles will adjust to the available output size, while the fonts will remain the same size.
  • It works well to decide a target canvas size for the combined figure output, usually in actual inches or centimeters, then define an appropriate font size in that context.
  • For more details on the Venndir grid data, see Venndir Graphics Objects.

References

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