passing parameters to ggplot

前端 未结 4 1406
没有蜡笔的小新
没有蜡笔的小新 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:34

    I think it's possible the following type of codes, which only build the aes component.

    require(ggplot2)
    
    DS <- data.frame(speed=rnorm(10), dist=rnorm(10))
    
    f <- function(DS, x, y, geom, opts=NULL) {
      aes <- eval(substitute(aes(x, y),
        list(x = substitute(x), y = substitute(y))))
      p <- ggplot(DS, aes) + geom + opts
    }
    
    p <- f(DS, speed, dist, geom_point())
    p
    

    However, it seems to be complicated approach.

提交回复
热议问题