Ggplot aes_string with interaction

Using aes_string simplifies the construction of functions to determine the parameters to build:

p <- ggplot(mtcars, aes_string(x="mpg", y="wt", group=interaction("cyl","gear"))) + geom_point() 

Now to write a function

 make_plot <- function(x,y, interact) { p <- ggplot(mtcars, aes_string(x=x, y=y, group=interact)) + geom_point() } 

and call the function

 make_plot("mpg","wt",c("cyl","gear")) 

But here the interaction is not used, i.e. it is not interpreted. I do not want to use separate variables for bcos interaction, the same can be used for other graphs. How should I make the interaction variable such that ggplot accepts and understands it?

+6
source share
1 answer

Accordingly , the answer should work (without quoting names):

 p <- ggplot(mtcars, aes_string(x=x, y=y, group=paste0("interaction(", paste0(interact, collapse = ", "), ")"))) + geom_point() 
+3
source

All Articles