首页 > 解决方案 > 自定义绘图函数错误 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")

我得到了我需要的地块?我在编写函数时是怎么搞砸的?

标签: rfunctionggplot2

解决方案


这是关于非标准评价。在这里,您可以使用curly-curly使其工作

plotify <- function(data, x, y){
  
  ggplot2::ggplot(data, ggplot2::aes({{ x }}, {{ y }})) + 
    ggplot2::geom_bar(stat = "identity")
} 

plotify(iris, Species, Sepal.Length)

推荐阅读