Reputation: 439
I would like to understand how the apply functions or anything similar can be used to make plots for several subsets of a data frame. Let's assume I want to use the mtcars data frame and I want to make a plot for each combination of "gear" and "carb".
I read here, that one can just write a function and then use lapply to create subsets of a data frame and plots for each of them. So how could I rewrite this to use lapply or anything similar?
gg_test = function(data) {
title = paste("gear",data[,"gear"][1],"carb",data[,"carb"][1])
require(ggplot2)
gg1 = ggplot(data, aes(x = mpg, y = cyl, color = disp)) + geom_point() +
ggtitle(title)
print(gg1)
}
for (g in unique(mtcars[,"gear"])){
for(ca in unique(mtcars[,"carb"])){
df_sub = subset(mtcars, gear == g & carb == ca)
gg_test(df_sub)
}
}
Upvotes: 1
Views: 2480
Reputation: 40648
You can use split
and interaction
to divide a data.frame into chunks that you can then apply your plot function to:
gg_test = function(data) {
title = paste("gear",data[,"gear"][1],"carb",data[,"carb"][1])
require(ggplot2)
ggplot(data, aes(x = mpg, y = cyl, color = disp)) + geom_point() +
ggtitle(title)
}
I modified your plot function to return the plot instead of print it out.
plots <- lapply(split(mtcars, interaction(mtcars[,c("gear","carb")]), drop = TRUE), gg_test)
Then I store each plot in another object that can be printed on demand.
Upvotes: 3