title: How to use lapply with ggplot2 while indexing variables
body: <p>I would like to generate several hundred boxplots of continuous data from a large data frame, stratified by the factor "year". I started by creating a list from the original data frame that contains each dependent variable and the year. </p>
  
  <p>Here is an example data set that looks like mine:</p>
  
  <pre><code>l&lt;-list(data.frame(year=c(rep("2010",10),rep("2011",10),rep("2012",10)),     
  var1=sample(1:100,30,replace=T)), 
      data.frame(year=c(rep("2010",10),rep("2011",10),rep("2012",10)), 
  var2=sample(100:200,30,replace=T)),
      data.frame(year=c(rep("2010",10),rep("2011",10),rep("2012",10)), 
  var3=sample(25:50,30, replace=T)))
  </code></pre>
  
  <p>The next step was to apply a ggplot2 function over the list. Neither of these functions produce plots: </p>
  
  <pre><code>lapply(l, function (j) ggplot(j, aes(x=year, y=j[,2], fill=year)) +    
   geom_boxplot() + ylab(names(j[2])) )
  
  lapply(l, function (j) ggplot(j, aes(x=year, y=j[[1]][2], fill=year)) +  
   geom_boxplot() + ylab(names(j[2])) )
  </code></pre>
  
  <p>The same error message is generated from those scripts: </p>
  
  <blockquote>
    <p>Error: No layers in plot"</p>
  </blockquote>
  
  <p>In actuality, my data frame is much larger -- 2800 observations and over 250 different variables with unique descriptive names (e.g. "M2_loss", "SSC"). Each variable is on a different scale, so using facets is not a good solution. What makes this question different from other examples on stackoverflow is that I am trying to index the data rather than explicitly name it. It is important that I capture the unique name of each variable and use it to label the y-axis. </p>
  
  <p>Any ideas on how to proceed?</p>