Please consider the following code:
test <- function(x,n){
selection<-names(x)[n]
graph <- ggplot(x, aes(factor(selection)))
graph + geom_bar()
}
test(mtcars,1)
It throws an error cause R can't find selection. I also played around with substitute
, eval
and get
without success. I found this similar question and thought I understood Joris'
answer but can't use the same trick for arguments of ggplot as well.
you can use aes_string
for this purpose. So test
should be like this:
test <- function(x,n){
graph <- ggplot(x, aes_string(x = names(x)[n]))
graph + geom_bar()
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With