The idea is to patch a call to ggplot in a function.
The example:
library(dplyr)
library(ggplot2)
library(lazyeval)
df <- data.frame(A=letters[1:10], B=2:11, C=3:12))
func <- function(name, dat=df) {
output <- dat %>%
select_(~A,name) %>%
arrange_(interp(~desc(var), var=as.name(name)))
plot <-
ggplot(output, aes_string(x=reorder(~A,-name), y=B)) +
geom_bar(stat='identity')
print(plot)
return(plot)
}
result <- func("B")
Compiling gives:
Error in -name : invalid argument to unary operator.
I tried deparse
and substitute
. Not sure I got the right combo. Any ideas?