How to pass / evaluate function arguments within another function for use with ggplot?

后端 未结 1 1067
失恋的感觉
失恋的感觉 2021-01-13 09:00

Please consider the following code:

test <- function(x,n){

selection<-names(x)[n]
graph <- ggplot(x, aes(factor(selection)))
graph + geom_bar()
}

         


        
相关标签:
1条回答
  • 2021-01-13 09:47

    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()
    }
    
    0 讨论(0)
提交回复
热议问题