本文介绍了将函数中的字符串传递给ggplot2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 通常我在我的工作中使用 ggplot2 ,并构建包装函数以加快我的工作流程。使用非标准评估(NSE) aes 强制我使用实际的变量名称而不是传递字符串。所以我复制并重命名数据框和变量名以安抚ggplot2。有一个更好的方法。我如何使 ggplot2 通过函数包装器接受未知的数据框和列名,而不复制数据框并使用通用列名称? 这是有效的: ggplot(mtcars,aes(x = mpg,y = hp))+ geom_point() FUN ggplot(dat,aes(x = x,y = y))+ geom_point()} FUN(mtcars,mpg ,hp) 解决方案 http://docs.ggplot2.org/current/aes_string.html> aes_string 功能,我并没有真正看到给定的突出,哪这正是: FUN ggplot(dat,aes_string( x = x,y = y))+ geom_point()} FUN(mt汽车,mpg,hp) Often I use ggplot2 in my work and build wrapper functions to speed up my work flow. The use of the non-standard evaluation (NSE) aes forces me to use the actual variable names rather than passing character strings. So I copy and rename dataframes and variable names to appease ggplot2. There's got to be a better way. How can I make ggplot2 accept unknown dataframes and column names via a function wrapper without replicating the dataframe and using generic column names?This works:ggplot(mtcars, aes(x=mpg, y=hp)) + geom_point()This does not:FUN <- function(dat, x, y) { ggplot(dat, aes(x = x, y = y)) + geom_point()}FUN(mtcars, "mpg", "hp") 解决方案 There's the aes_string function, that I don't really see given prominence, which does exactly this:FUN <- function(dat, x, y) { ggplot(dat, aes_string(x = x, y = y)) + geom_point()}FUN(mtcars, "mpg", "hp") 这篇关于将函数中的字符串传递给ggplot2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-16 16:30