4

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!

Louise
  • 183
  • 1
  • 9

2 Answers2

2

You can use ggplot2 facetting to have all plots on the same page:

ggplot(df, aes(x=x, y=y1)) +
  geom_point() + 
  facet_grid(. ~ site)
Colonel Beauvel
  • 30,423
  • 11
  • 47
  • 87
  • thanks... but my question is how to group _different_ plots (x-y1 and x-y2) by site.. – Louise Mar 22 '15 at 12:29
  • @Louise, I think you should enclose the plotting code inside the [pdf - function](https://stat.ethz.ch/R-manual/R-devel/library/grDevices/html/pdf.html) and use the ‘onefile’ argument. [This answer](http://stackoverflow.com/questions/7534606/save-multiple-graphs-one-after-another-in-one-pdf-file) might also be of help. – hvollmeier Mar 22 '15 at 15:19
  • @hvollmeier: I tried, but still haven't found a solution that works correctly. I'll post one here when I find one... – Louise Mar 24 '15 at 21:16
1

@Luise, using your 'sample.csv' file this will give you the asked for file, 2 plots on each page, grouped by 'site'. ( Make sure that the variable 'site' is a factor. You should check it with 'str(df)' ). I used the 'grid.arrange' function from the 'grid.Extra' package to place the plots. Without a name argument in the pdf-function the plots are printed to the default 'Rplots.pdf' file.

require(ggplot2)
require(gridExtra)
pdf(onefile = TRUE)
for(i in 1:length(levels(df$site))){
  A <- ggplot(df[df$site == levels(df$site)[i],], aes(x=x, y=y1)) + geom_point() + ggtitle(levels(df$site)[i])
  B <- ggplot(df[df$site == levels(df$site)[i],], aes(x=x, y=y2)) + geom_point() + ggtitle(levels(df$site)[i])
  grid.arrange(A, B)
}
dev.off()

You can download the output file here.

hvollmeier
  • 2,956
  • 1
  • 12
  • 17
  • Thanks ever so much!! This is exactly the output that I needed to produce. I tried the code on my full dataset and it works perfectly - so I'm extremely grateful to you :) – Louise Mar 26 '15 at 14:18