gpt4 book ai didi

r - ggplot aes_string 与交互

转载 作者:行者123 更新时间:2023-12-03 20:14:47 29 4
gpt4 key购买 nike

使用 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 接受和理解?

最佳答案

根据 this回答这应该有效(不引用列名):

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

关于r - ggplot aes_string 与交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15502263/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com