It is probably better to not use lapply but a simple for-loop, or a loop like this:
oo <- function(i) {
ggplot() +
aes(sample = mtcars[[i]])+ # double square brackets for getting the vector not a data frame
geom_qq() +
geom_qq_line(color="red")+
labs(title=names(mtcars)[i])
}
map(1:length(mtcars), oo) # the map function from purrr package, similar to lapply
Thanks for your reply. Your method does what's needed, however I wanted to avoid defining dataframe inside the function to make it more universal, so that I could supply it with other dataframes (or different subsets of a larger dataframe).
I thought that lapply supplies vector (i.e. a column of a dataframe) to the function, so it could take the name of the vector as a title. I have not figured out how to get to that name, though. Guess I am wrong here..
Maybe define a function with two arguments? e.g. oo <- function(dataset, i) {...}
I replaced lapply with map because I found that lapply didn't take the 1:length(mtcars) vector. Essentially, both functions are alternative forms of a simple for-loop. There is a names function for getting the names of all variables in the dataframe mtcars, but, unfortunately, it only works for the dataframe object but not each variable in the dataframe. Good luck!