R pass variable column indices to ggplot2 [duplicate]

You can use the aes_string in stead of aes to pass string in stead of using objects, i.e.:

myplot = function(df, x_string, y_string) {
   ggplot(df, aes_string(x = x_string, y = y_string)) + geom_point()
 }
myplot(df, "A", "B")
myplot(df, "B", "A")

Leave a Comment