I like the patchwork package suggestion that was given in this post. So far I have come up with this solution to handle a variable number of graphs, but I can't figure out how to make it handle numbers not divisible by 3. Any suggestions? Can we make it simpler?
x <- c(5,7,2,4,9)
y <- c(2,10,15,8,14)
df <- data.frame(x,y)
myfunct <- function(i){
p<-ggplot(df, aes(x=x,y=y*i)) + geom_point()
return(p)
}
myGrobs <- lapply(1:10,myfunct)
library(patchwork)
pdf("test pdf.pdf", width = 6.5, height = 9, paper="letter")
for(i in c(1,4,7)){
print(myGrobs[[i]] / myGrobs[[i+1]] / myGrobs[[i+2]])
}
dev.off()