passing parameters to ggplot

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

    One approach that I can think of is using match.call() to reach the variable names contained by the parameters/arguments passed to the custom plotting function and then use eval() on them. In this way you avoid passing them as quoted to your custom function, if you do not like that.

    library(ggplot2)
    
    fun <- function(df, x, y) {
        arg <- match.call()
        ggplot(df, aes(x = eval(arg$x), y = eval(arg$y))) + geom_point()
    } 
    fun(mpg, cty, hwy) # no need to pass the variables (column names) as quoted / as strings
    

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-05 03:45

    Another option is to use do.call. Here is a one line copy paste from a working code:

    gg <- gg + geom_rect( do.call(aes, args=list(xmin=xValues-0.5, xmax=xValues+0.5, ymin=yValues, ymax=rep(Inf, length(yValues))) ), alpha=0.2, fill=colors )
    
    0 讨论(0)
  • 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?

    0 讨论(0)
提交回复
热议问题