So I have a data frame, set up a bit like this:
Sample V1 V2 V3 Group1 Group2
bob 12 32 12 G1 G2
susan 43 23 54 G2 G2
mary 23 65 34 G1 G2
I am able to do a grouped boxplot of each variable (V1, V2, V3) individually, grouped by Group1 and Group2 variables, but my real dataset has WAY more variables, and will be tedious to code individually. Is there a way that I can loop the process, and automate plot generation and export? For loops are still a bit of an obscure topic for me.
Here is the code I use to generate an individual plot:
png(filename= "filename.jpg")
ggplot(aes(y=data$V1, x=data$Group1, fill=data$Group2), data=data) + geomboxplot()
dev.off()
Thanks!
Here are several approaches for you. I'm guessing there is a duplicate, but if you're just starting out it's not always easy to apply those answers to your data.
approach 1: melt and facet
As you can see, this is not feasible when you have too many grouping variables.
approach 2: different plots/images per variable, still using the long data. I have split the long data by variable, and created a plot for each chunk. The current code plots to the console; file-saving code is commented out.
And a third approach is to use the strings of columns you're interested in: