string with interaction
Using aes_string makes it easy to construct functions to take parameters to plot:
p <- ggplot(mtcars, aes_string(x="mpg", y="wt", group=interaction("cyl","gear"))) +
geom_point()
Now to write the the function
make_plot <- function(x,y, interact) {
p <- ggplot(mtcars, aes_string(x=x, y=y, group=interact)) +
geom_point()
}
and to call the function
make_plot("mpg","wt",c("cyl","gear"))
But here the interaction is not used, ie, it is not interpreted. I don't want to use separate variables for interaction bcos the same function could be used for other plots. How should I go about making the interaction variable such that it is accepted and understood by ggplot?
根据这个答案,这应该工作(没有引用colnames):
p <- ggplot(mtcars, aes_string(x=x, y=y, group=paste0("interaction(", paste0(interact,
collapse = ", "), ")"))) + geom_point()
链接地址: http://www.djcxy.com/p/70204.html
上一篇: java.net.URISyntaxException:索引为75的路径中的非法字符
下一篇: 与交互的字符串