Non-numeric arguement error in ggplot2 user function

时间:2017-10-12 09:52:34

标签: r ggplot2

I'm trying to write a universal function to generate ggplot2 scatter graphs based on user input. This is what I have so far:

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)

This keeps returning the error "non-numeric argument to binary operator" but as far as I can tell I'm not creating a binary operator so my non-numeric inputs should be fine. This code worked fine before I started trying to specify the x and y label.

I must be missing something silly since I'm new to writing my own functions in R.

Thanks in advance.

1 个答案:

答案 0 :(得分:0)

I guess the error would be in not closing the braces

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

Also, it is better not to use <<-. Instead

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") 

NOTE: A reproducible example would have been better