For various reasons I have a data frame that contains a variable with the name "log(foo)". I am trying to plot objects like this from within a function. I use aes_string() in my functions to refer to the name of the variable I want to plot, but this is failing when the string is a function call like "log(foo)". When the data for the plot is evaluated, ggplot is looking for a variable with name "foo" not "log(foo)".
Gah, sorry; in creating my example I forgot check.names = FALSE but I do have this set in the actual code where this error occurred. Turning on check.names = FALSE doesn't resolve the issue:
> df <- data.frame(y = runif(1:10), "log(foo)" = 1:10, check.names = FALSE)
> names(df)
[1] "y" "log(foo)"
> ggplot(df, aes_string(x = varname, y = "y")) + geom_point()
Error in FUN(X[[i]], ...) : object 'foo' not found
Thanks @alistaire ; I wondered if rlang was the way to go here. I hadn't seen the as.name() variant for aes_() before, having tried the quote() variant, that is documented, without success.
I think I'll use aes_() with as.name() for the time being as I haven't really studied rlang enough to be confident using it in a package.