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.
Here is an example data set that looks like mine:
l<-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)))
The next step was to apply a ggplot2 function over the list. Neither of these functions produce plots:
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])) )
The same error message is generated from those scripts:
Error: No layers in plot"
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.
Any ideas on how to proceed?