Q:

Saving individual pdfs to one file in R

I have data set as below
set.seed(10)
test <- data.frame(exp = rep(LETTERS[1:5], each = 4), x = rnorm(20, 10, 2), y = rnorm(20, 11, 1))

I'd like to plot several figures for each level in the data and save the result to the one pdf file without using Acrobat Pro
I made one attempt that create single plots but I don't knot if it is possible to make one file out of it.
for (i in levels(test$exp)){
  da <-  subset(test, exp == i)
    pdf(paste(i, ".pdf", sep=""))
      plot(da$x, da$y)
    dev.off()
}

A:

If we need a single pdf, then remove the pdf line and dev.off and keep it outside the for loop.
pdf("somefile.pdf")
for (i in levels(test$exp)){
   da <-  subset(test, exp == i)
   plot(da$x, da$y)
 }
dev.off()

