I am trying to loop on both data frames and columns to create multiple graphs. I have a list of data frames, and for each of them I want to build an answer to one of several predictors.
For example, I can easily navigate data frames:
df1=data.frame(response=rpois(10,1),value1=rpois(10,1),value2=rpois(10,1)) df2=data.frame(response=rpois(10,1),value1=rpois(10,1),value2=rpois(10,1))
But I ran into errors in the columns inside the data frame:
lapply(list("value1","value2"), function(i) ggplot(df1,aes_string(x=i,y=response))+geom_point())
I suspect that this has something to do with how I feel about aesthetics.
Ultimately, I want to join together lapply generate all combinations of frames and data columns.
Any help is appreciated!
EDITOR: Joran has it! You need to put the answers without a list in quotation marks when using aes_string
lapply(list("value1","value2"), function(i) ggplot(df1,aes_string(x=i,y="response"))+geom_point())
For reference, lapply functions are built here to generate all combinations:
lapply(list(df1,df2), function(x) lapply(list("value1","value2"), function(i) ggplot(x,aes_string(x=i,y="response"))+geom_point() ) )