ggplot与互动aes_string(ggplot aes_string with interac

2019-07-22 00:03发布

使用aes_string可以很容易地构造函数取参数绘制:

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

现在写功能

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

并调用函数

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

但这里的互动不使用,即它不被解释。 我不希望使用独立的变量相互作用BCOS相同的功能,可用于其他地块。 我应该如何去作出这样,它是接受和ggplot理解交互变量?

Answer 1:

根据这个答案这应该工作(不引述colnames):

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


文章来源: ggplot aes_string with interaction