将字符串传递给facet_grid:ggplot2 [英] pass string to facet_grid : ggplot2
本文介绍了将字符串传递给facet_grid:ggplot2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在ggplot2中,您可以使用 aes_string
在用户定义的函数中传递字符参数。你怎么能做同样的方面网格需要一个公式,而不是 aes
?
FUN < - 函数(data,x,y,fac1,fac2){
ggplot(data = data,aes_string(x = x,y = y))+
geom_point + facet_grid(as.formula(substitute(fac1〜fac2)))
}
FUN(mtcars,'hp','mpg','cyl','am ')
解决方案 似乎工作得很好。
FUN < - function(data,x,y,fac1 ,fac2){
ggplot(data = data,aes_string(x = x,y = y))+
geom_point()+ facet_grid(reconfulate(fac2,fac1))
}
FUN(mtcars,'hp','mpg','cyl','am')
In ggplot2 you can pass character arguments inside a user defined function using aes_string
. How can you do the same for facet grid which takes a formula, not aes
?
FUN <- function(data, x, y, fac1, fac2) {
ggplot(data = data, aes_string(x=x, y=y)) +
geom_point() + facet_grid(as.formula(substitute(fac1 ~ fac2)))
}
FUN(mtcars, 'hp', 'mpg', 'cyl', 'am')
解决方案
reformulate()
seems to work just fine.
FUN <- function(data, x, y, fac1, fac2) {
ggplot(data = data, aes_string(x=x, y=y)) +
geom_point() + facet_grid(reformulate(fac2,fac1))
}
FUN(mtcars, 'hp', 'mpg', 'cyl', 'am')
这篇关于将字符串传递给facet_grid:ggplot2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文