【问题标题】:Non-numeric arguement error in ggplot2 user functionggplot2 用户函数中的非数字争论错误
【发布时间】:2017-10-12 18:24:31
【问题描述】:

我正在尝试编写一个通用函数来根据用户输入生成 ggplot2 散点图。这是我目前所拥有的:

G.Point <- function(USER2, USER3, USER4, X, Y){
Point <<- Plot.data + geom_point(
aes_string(x = USER2, y = USER3, colour = USER4) + 
  xlab(X) +
  ylab(Y))
}
G.Point("USER2", "USER3", "USER4", "X", "Y") #These are called something else in actual code.
plot(Point)

这不断返回错误“非数字参数到二元运算符”,但据我所知,我没有创建二元运算符,所以我的非数字输入应该没问题。在我开始尝试指定 x 和 y 标签之前,此代码运行良好。

我一定是错过了一些愚蠢的事情,因为我是新来用 R 编写自己的函数。

提前致谢。

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    我猜错误在于没有关闭大括号

    geom_point(
        aes_string(x = USER2, y = USER3, colour = USER4))
    

    另外,最好不要使用&lt;&lt;-。而是

    G.Point <- function(USER2, USER3, USER4, X, Y){
         print(Plot.data +
            geom_point(aes_string(x = USER2, y = USER3, colour = USER4)) + 
            xlab(X) +
            ylab(Y))
      }
    G.Point("USER2", "USER3", "USER4", "X", "Y") 
    

    注意:一个可重现的例子会更好

    【讨论】:

    • 谢谢你,我知道这很明显。很快,使用
    • @Matt.TW 这是全局赋值。在这种情况下,不需要
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多