I wish to generate error bar for each data point in ggplot2
using a generic function that extracts column names for the same using the names
function. Following is a demo code:
plotfn <- function(data, xind, yind, yerr) {
yerrbar <- aes_string(ymin=names(data)[yind]-names(data)[yerr], ymin=names(data) [yind]+names(data)[yerr])
p <- ggplot(data, aes_string(x=names(data)[xind], y=names(data)[yind]) + geom_point() + geom_errorbar(yerrbar)
p
}
errdf <- data.frame('X'=rnorm(100, 2, 3), 'Y'=rnorm(100, 5, 6), 'eY'=rnorm(100))
plotfn(errdf, 1, 2, 3)
Running this gives the following error:
Error in names(data)[yind] - names(data)[yerr] :
non-numeric argument to binary operator
Any suggestions? Thanks.