ant*_*ant 6 r ggplot2 data.table
我写了一个绘制条形图的函数.但是,当我得到一个方面的包装时,"〜"符号会使事情变得困难.
rf.funct <- function(dat, predictor, feature){
ggplot(get(dat), aes(get(predictor), N)) +
geom_bar(stat = 'identity') +
facet_wrap(get(~feature)) # this is where the problem is
}
Run Code Online (Sandbox Code Playgroud)
我尝试过以下方法:
facet_wrap((get(~feature))) # invalid first argument
facet_wrap(paste0("~ ", get(feature))) # object 'feature' not found
Run Code Online (Sandbox Code Playgroud)
如何确保函数中包含"〜"符号?
你不需要使用get.您已使用dat参数将数据框传递到函数中,因此只需dat将ggplot 提供给它,它将从其环境中获取数据数据.
rf.funct <- function(dat, predictor, feature) {
ggplot(dat, aes_string(predictor, "N")) +
geom_bar(stat = 'identity') +
facet_wrap(feature)
}
Run Code Online (Sandbox Code Playgroud)
在predictor和feature参数应输入为字符串.然后你可以aes_string用来指定美学.facet_wrap现在可以直接使用字符向量,而不需要公式(如@WeihuangWong所指出的那样).
| 归档时间: |
|
| 查看次数: |
707 次 |
| 最近记录: |