r - 自定义绘图函数错误 R - FUN 中的错误(X[[i]],...):找不到对象“物种”
问题描述
我正在尝试编写一个函数来输出要在多个标准化数据帧上使用的图。我一直在努力思考我做错了什么,但我无法弄清楚。
# function to plot
plotify <- function(data, x, y){
ggplot2::ggplot(data, aes(x, y)) +
geom_bar(stat = "identity")
}
plotify(iris, Species, Sepal.Length)
## Error in FUN(X[[i]], ...) : object 'Species' not found
上面的代码如何给我一个错误但是如果要运行下面的代码,
ggplot(iris, aes(Species, Sepal.Length)) + geom_bar(stat = "identity")
我得到了我需要的地块?我在编写函数时是怎么搞砸的?
解决方案
这是关于非标准评价。在这里,您可以使用curly-curly使其工作
plotify <- function(data, x, y){
ggplot2::ggplot(data, ggplot2::aes({{ x }}, {{ y }})) +
ggplot2::geom_bar(stat = "identity")
}
plotify(iris, Species, Sepal.Length)
推荐阅读
- python-3.x - 将 google 文档批量导出为 .zip 压缩的 html 文件
- css - 盖茨比:className 的 CSS 有时有效
- javascript - 在对象数组中查找属性(作为字符串存储在变量中)
- unity3d - Unity - 随机生成的蠕虫,如可破坏的地形建议
- css - 悬停后变换比例更改位置元素
- c# - 查找两个日期之间的小时数
- python - FLASK_ENV 似乎被忽略(无法进入调试环境)
- bash - for循环子命令中的bash变量扩展
- vba - 是否可以在access中根据combobox.value做动态sql语句?
- c# - 如何在 C# 中将粘贴处理程序添加到 IOleWindow