Here is the dataframe structure I am working with :
> str(final.c)
'data.frame': 218916 obs. of 7 variables:
$ strain : chr "CX11285" "ED3048" "JU1200" "CX11315" ...
$ row : Factor w/ 8 levels "A","B","C","D",..: 1 1 1 1 1 1 2 2 2 2 ...
$ col : Factor w/ 12 levels "1","2","3","4",..: 1 3 5 7 9 11 1 3 5 7 ...
$ variable: Factor w/ 79 levels "n","meanTOF",..: 1 1 1 1 1 1 1 1 1 1 ...
$ value : num 312 697 159 381 175 110 324 198 179 375 ...
$ L1 : int 1 1 1 1 1 1 1 1 1 1 ...
$ set : num 1 1 1 1 1 1 1 1 1 1 ...
Here is an example of a histogram generated by ggplot :
var <- "norm.n"
plate <- c(1,5)
dat <- final.c
day <- 1
plot <- ggplot(subset(dat, variable %in% var & L1 %in% plate & set %in% day),
aes(value, fill = factor(L1))) + geom_histogram()
The above plot looks at a subset of this large melted data.frame (I say it is melted because I ran melt on a list of data.frames prior to "final.c"). Specifically, the subset is when set = 1 & when variable = "norm.n" & when L1 = 1 or 5. This generates two histograms, which I split up by color.
What I would like to do is devise a function that generates multiple plots. The plots would include: Histograms plotting "plates" (L1 in final.c) 1 to plates 5-30 for each var (variable in final.c). This would have to be grouped by set also, of which there are 2 (set 1 and set 2)
This would therefore generate 26 (# of plate comparisons) X 80 (# of variables in final.c) = 2080 plots. I will subset the list of variables to make the plots more manageable, but the idea should be the same.
For the sake of the question lets just call the variables in this example, v1...v80
Thanks for any and all help, I'll be checking periodically for anything that might not be clear.
EDIT ::
Following the suggestion in the comments, I made a simple for loop to generate plots for each plate (L1 in final.c) comparison, it goes as follows :
for(i in 5:10){
var <- "norm.n"
plate <- c(1, i)
dat <- final.c
day <- 1
plot <- ggplot(subset(dat, variable %in% var & L1 %in% plate & set %in% day),
aes(value, fill = factor(L1))) + geom_histogram()
setwd("~/Dropbox/Andersen lab/LabFolders/Stefan/testplots/")
ggsave(filename = paste(var, i, ".jpg", sep = ""), plot = plot, height=4, width=8, units="in", dpi=300)
}
This gives me 6 plots of histograms comparing two different L1 values from final.c. I can easily extend this to make all plate comparisons (e.g for(i in 5:30)).
Here is the remaining question - How do I loop through plates and simultaneously through variables. The "variable" column in final.c is a set of factors with 79 levels. What I would like to do is loop through those factors, but I am not sure how to. Like below:
for(i in 5:10){
var <- ***LOOP THROUGH VARIABLES COLUMN IN final.c***
plate <- c(1, i)
dat <- final.c
day <- 1
I am just unsure about the syntax regarding this.
for
loop to go through the sets of plates you want plotted? Try to break your problem into pieces and tackle them one at a time. - Gregor Thomas