Susie
Susie

Reputation: 197

I want to plot scatter plot by every 2 columns ,meet error as Error : non-numeric argument to binary operator

code as below:

Raw=mtcars
cn=colnames(Raw)
sapply(1:7,function(i)ggplot(data=Raw,aes(x=Raw[,i],y=Raw[,i+1])+
                                            geom_line()+geom_point(size=4,shape=20)+
                                            labs(x='totality_accuray',y=cn[i]))
)

can help out what's issue to get this error reminder:Error in aes(x = Raw[, i], y = Raw[, i + 1]) + geom_line() : non-numeric argument to binary operator

Upvotes: 0

Views: 51

Answers (1)

Adam Quek
Adam Quek

Reputation: 7153

Custom plotting function that take two named variables:

plotFn <- function(x,y, df){
           ggplot(df,aes_string(x,y)) +
           geom_line() +  
           geom_point(size=4,shape=20)
          }


plotFn("disp", "cyl", mtcars) 

enter image description here

Grabbing a list of variables for x and y axis:

Raw <- mtcars
cn <- colnames(Raw)

arg <- lapply(1:(ncol(Raw)-1), function(x) list(x = cn[x],
                                    y = cn[x+1]))

Plotting 10 plots:

lapply(arg, function(i) plotFn(i[["x"]], i[["y"]], Raw))

Upvotes: 1

Related Questions