I'm quite new to R and this is my first question on Stackoverflow so please do let me know if anything is unclear!
I have a large dataset which is split by a factor variable df$site.
For each site, I need to print several plots (say A and B). So far, I have managed to print all of the plots individually as separate PDF files (using the code below).
However, I need to group the plots in one large PDF - where each page contains plot A and plot B for each site, e.g.
Page 1 - site 1 - plot A1, plot B1
Page 2 - site 2 - plot A2, plot B2 (and so on).
I would be extremely grateful for some advice on how to do this!
doplot = invisible(by(df, df$site, function(i) {
A <- ggplot(i, aes(x, y1))+ geom_point() + ggtitle(unique(i$site))
B <- ggplot(i, aes(x, y2))+ geom_point() + ggtitle(unique(i$site))
ggsave(sprintf("%s_A.pdf", unique(i$site)), A)
ggsave(sprintf("%s_B.pdf", unique(i$site)), B)
}))
lapply(unique(df$site), doplot)
I'm not sure how to post sample data, so here is a simplified sample:
site x y1 y2
site_1 3.56 0.337674318 1.935941597
site_1 4.31 1.559665628 2.402924583
site_1 4.36 1.523297984 2.600534832
site_1 5.03 2.261701756 2.394485714
site_1 5.23 2.573428703 2.686396123
site_1 5.33 2.582030907 2.600534832
site_1 5.69 2.842581635 2.723667517
site_2 3.79 2.793543915 3.265708161
site_2 4.49 3.353407259 3.701443333
site_2 4.88 3.363259555 3.733048672
site_2 4.94 3.550471097 3.780780075
site_2 4.88 3.590317006 3.848359519
site_2 5.31 3.708100041 3.922750337
site_2 5.26 3.735499016 3.906067837
site_2 4.22 3.074269438 4.091499816
site_2 6.72 4.329273722 4.35481919
site_2 6.46 4.283464186 4.326989203
I have uploaded a sample .csv file here. Thanks!