passing parameters to ggplot

前端 未结 4 1397
没有蜡笔的小新
没有蜡笔的小新 2020-12-05 03:00

would like to create a function that generates graphs using ggplot. For the sake of simplicity, the typical graph may be

ggplot(car, aes(x=speed, y=dist)) +          


        
4条回答
  •  有刺的猬
    2020-12-05 03:50

    One solution would be to pass x and y as string names of columns in data frame DS.

    f <- function(DS, x, y) {    
      ggplot(DS, aes_string(x = x, y = y)) + geom_point()  
    }
    

    And then call the function as:

    f(cars, "speed", "dist")
    

    However, it seems that you don't want that? Can you provide an example why you would need different functionality? Is it because you don't want to have the arguments in the same data frame?

提交回复
热议问题