Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

non-numeric argument to binary operator [closed]

Tags:

r

ggplot2

I'm attempting to create my first function in R. The function should take in a data frame, x-series from data frame, y-series from data frame, and plot a scatter plot. Seems simple enough, but I run into trouble when I attempt to check for an optional boolean argument.

R Script

plotScatterChart <- function(data,x,y,scale=y,line=FALSE) {

    require(ggplot2)
    data$x <- as.numeric(x)
    data$y <- as.numeric(y)

    plot <- ggplot(data, aes(x, y)) + 
            geom_point() +  # aes(alpha=0.3,color=scale)
            #scale_color_gradient(high="red")

    if(line) {
        plot <- plot + geom_smooth(method="lm") 
    }

    ggsave(file="plot.svg", plot=plot, height=10, width=10)

    return(plot)
}

plotScatterChart(data=iris,x=iris$Petal.Length,y=iris$Petal.Width,line=TRUE)

Error

non-numeric argument to binary operator

Extra

Other suggestions for improving this function are welcome.

like image 516
Jefftopia Avatar asked Feb 04 '14 18:02

Jefftopia


People also ask

What is non-numeric argument in binary operator?

The “non-numeric argument to binary operator” error occurs when we perform arithmetic operations on non-numeric elements.

What is a binary operator in R?

Most of the operators that we use in R are binary operators (having two operands). Hence, they are infix operators, used between the operands. Actually, these operators do a function call in the background.

How do I convert non-numeric to numeric in R?

To convert factors to the numeric value in R, use the as. numeric() function. If the input is a vector, then use the factor() method to convert it into the factor and then use the as. numeric() method to convert the factor into numeric values.


3 Answers

The error is because of the trailing + after geom_point(). Remove that and it should work.

like image 188
Christopher Louden Avatar answered Sep 24 '22 22:09

Christopher Louden


Christopher's answer is perfectly correct. Let me add that ggplot also seems to accept lists:

plot <- ggplot(data, aes(x, y)) +   list(     geom_point(),     aes(alpha=0.3,color=scale),     scale_color_gradient(high="red"),     NULL   ) 

Unfortunately, unlike Python where you can write [1, 2, 3, ], the construct list(1, 2, 3, ) produces an error in R. This is the reason for the final NULL, which is happily ignored by ggplot2.

Another possible workaround is to write

plot <- ggplot(data, aes(x, y)) +   geom_point() +   #aes(alpha=0.3,color=scale) +   #scale_color_gradient(high="red") +   list() 

The final list() is supposed to stay in place to cancel the effects of the last + sign; it is a no-op otherwise.

like image 44
krlmlr Avatar answered Sep 22 '22 22:09

krlmlr


With version 2.0.0, ggplot2 has gained a new geometry, geom_blank() which draws nothing.

It can be used to avoid this type of errors when it is placed as last layer.

plot <- ggplot(data, aes(x, y)) +
  geom_point() +
  #aes(alpha=0.3,color=scale) +
  #scale_color_gradient(high="red") +
  geom_blank()

Using geom_blank() in this way is similar to the workaround in @krlmlr's answer which uses list() as last layer.

like image 30
Uwe Avatar answered Sep 25 '22 22:09

Uwe