I am writing a loop. The purpose of the loop is to create many plots and save them as a PDF. After selecting a subset of my data I do the following:
    pdf("path to the desired filename", width = 16, height = 7)
          some ggplot operations...
    dev.off()
This in itself works if I do it manually for all the subsets of data that I want to plot. If I try this in a loop the PDF device saves a lot of "empty" pictures.
I don't understand why this will not work in a loop. It seems like the loop does not wait until the plot has been properly exported.
 
     
    