title: Multiple plots to pdf using nested for loops in R
body: <p>My question is about using for loops to generate multiple PDFs of figures using ggplot2 in R. I have a set of water quality data for several sampling stations for a large number of parameters falling into one of several groups. </p>
  
  <p>My goal is to end up with a single PDF file for each station, containing some simple scatter plots for each parameter, grouped by the parameter group. My code is below. </p>
  
  <p>Initially, I used the inner for loop to generate plots for a single sampling station and saved it to a PDF. When I nested it in the outer for loop I ran into an issue where instead of generating unique PDFs containing only the data for each sampling station, it's generating identical PDFs for all sampling stations containing plots with data for all stations on top of each other. I haven't been able to find a fix so that each PDF only contains that station's data.    </p>
  
  <pre><code>full_set &lt;- list.files(pattern = "*xlsx") %&gt;% map_df(~read_excel(.))
  full_set_annual_means &lt;- full_set %&gt;% group_by(Station, Group, Constituent, Year) %&gt;% 
    summarize(annual_mean = mean(Monthly_Average))
  for (st in unique(full_set_annual_means$Station)) {
    filename &lt;- sprintf("%s plots.pdf", toString(st))
    pdf(file = filename, width = 10.5, height = 8, paper = "a4r")
    for (var in unique(full_set_annual_means$Group)) {
      print(ggplot(full_set_annual_means[full_set_annual_means$Group == var, ], aes(Year, annual_mean)) + 
            geom_point() + facet_wrap(~Constituent, scales = "free_y") + ggtitle(var))
  }
  dev.off()
  }
  </code></pre>
  
  <p>Edit:</p>
  
  <p>I figured out a way to make it work the way I intended. Initially I was trying to filter the data by station in the outer for loop by reassigning a filtered version of the dataset to the full_set_annual_means variable as follows:</p>
  
  <pre><code>full_set_annual_means &lt;- full_set_annual_means %&gt;% filter(Station == st)
  
  </code></pre>
  
  <p>But by doing so I was overwriting the full dataset with a filtered version and so the first iteration of the loop would work and then for the remaining iterations of the loop it would output blank pdf files. By saving the filtered data as an intermediate variable instead:</p>
  
  <pre><code>station_set &lt;- full_set_annual_means %&gt;% filter(Station == st)
  
  </code></pre>
  
  <p>I retained the full dataset for future iterations of the loop. Here's the updated code in full that worked the way I intended:</p>
  
  <pre><code>full_set &lt;- list.files(pattern = "*xlsx") %&gt;% map_df(~read_excel(.))
  full_set_annual_means &lt;- full_set %&gt;% group_by(Station, Group, Constituent, Year) %&gt;% 
    summarize(annual_mean = mean(Monthly_Average))
  for (st in unique(full_set_annual_means$Station)) {
   station_set &lt;- full_set_annual_means %&gt;% filter(Station == st)
     filename &lt;- sprintf("%s plots.pdf", toString(st))
    pdf(file = filename, width = 10.5, height = 8, paper = "a4r")
    for (var in unique(station_set$Group)) {
      print(ggplot(station_set[station_set$Group == var, ], aes(Year, annual_mean)) + 
            geom_point() + facet_wrap(~Constituent, scales = "free_y") + ggtitle(var))
  }
  dev.off()
  }
  </code></pre>