0

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

Susie
  • 197
  • 7

1 Answers1

1

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))
Adam Quek
  • 6,973
  • 1
  • 17
  • 23
  • thank you, it works! if want to display all 10 plots in one page, how to operate? – Susie Apr 19 '17 at 09:31
  • It's a little bit more complicated, but basically (i) `pp <- lapply(arg, function(i) plotFn(i[["x"]], i[["y"]], Raw))`; (ii) `library(gridExtra)`; (iii) `do.call("grid.arrange", c(pp, ncol=5))` – Adam Quek Apr 19 '17 at 09:37
  • Check [How do I arrange a variable list of plots using grid.arrange?](http://stackoverflow.com/questions/10706753/how-do-i-arrange-a-variable-list-of-plots-using-grid-arrange) – Adam Quek Apr 19 '17 at 09:38
  • ,Thank you. It really works , that the one what I want ,thanks a lot ! – Susie Apr 19 '17 at 09:45