Looping on ggplot2 with columns

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)) #Looping across data frames lapply(list(df1,df2), function(i) ggplot(i,aes(y=response,x=value1))+geom_point()) 

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() ) ) 
+4
source share
1 answer

Inside aes_string() all variables must be represented as character . Add quotes around the "answer".

 lapply(list("value1","value2"), function(i) ggplot(df1, aes_string(x=i, y="response")) + geom_point()) 
+7
source

All Articles