Skip to content

Instantly share code, notes, and snippets.

@timedreamer
Created January 13, 2023 01:24
Show Gist options
  • Save timedreamer/a2e401a0b5409cc6d00562267f3218bb to your computer and use it in GitHub Desktop.
Save timedreamer/a2e401a0b5409cc6d00562267f3218bb to your computer and use it in GitHub Desktop.
It's useful to plot many gene expression plots on a pdf. Each page has 3x3 or 4x4 plots.
# Thanks for the suggestion from Joseph Elsherbini.
library(tidyverse)
library(ggforce)
n_pages(p) # use this to get the number of pages to print
# then run a for loop to loop over pages
pdf("test.pdf")
for (i in 1:5) {
p <- ggplot(diamonds) +
geom_point(aes(carat, price), alpha = 0.1) +
facet_wrap_paginate(~ cut:clarity, ncol = 3, nrow = 3, page = i)
print(p)
}
dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment