【问题标题】:ggplot aes_string with interactionggplot aes_string 与交互
【发布时间】:2013-03-19 14:30:39
【问题描述】:

使用 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"))

但是这里没有使用交互,也就是说,它没有被解释。我不想为交互使用单独的变量,因为相同的函数可以用于其他绘图。我应该如何制作交互变量以使其被 ggplot 接受和理解?

【问题讨论】:

  • paste0("interaction(", paste0('"', interact, '"', collapse = ", "), ")") ?

标签: r ggplot2 parameter-passing


【解决方案1】:

根据this 的回答,这应该可以工作(不引用列名):

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-31
    • 2014-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-01
    • 2020-12-01
    • 2013-09-20
    相关资源
    最近更新 更多