Addressing x and y in aes by variable number

Your problem is that aes doesn’t know your function’s environment and it only looks within global environment. So, the variable dat declared within the function is not visible to ggplot2‘s aes function unless you pass it explicitly as:

showplot1<-function(indata, inx, iny) {
    dat <- indata
    p <- ggplot(dat, aes(x=dat[,inx], y=dat[,iny]), environment = environment())
    p <- p + geom_point(size=4, alpha = 0.5)
    print(p)
}

Note the argument environment = environment() inside the ggplot() command. It should work now.

Leave a Comment