根据ggplot2标准功能,创建ggplot2函数并指定参数作为数据中的变量 [英] Create ggplot2 function and specify arguments as variables in data as per ggplot2 standard functionality

查看:151
本文介绍了根据ggplot2标准功能,创建ggplot2函数并指定参数作为数据中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个绘图函数,在其中指定数据集,并以与ggplot2中相似的方式将绘图参数指定为函数参数,即在其中我们指定不带数据集的变量名,例如hp代替mtcars$hp.

I want to create a plotting function where I specify the data set, and specify the plotting arguments as function arguments in similar fashion as I can within ggplot2, i.e. where we specify the variable name without the dataset, e.g. hp instead of mtcars$hp.

例如:

ggfun <- function(dat, x.var, y.var){

  ggp <- ggplot(data = dat,
                aes(x = x.var,
                    y = y.var)) +
    geom_point()

  return(ggp)
}


ggfun(dat = mtcars, x.var = drat, y.var = hp)

将返回:

但是它返回:

> ggfun(dat = mtcars, x.var = drat, y.var = hp)
Error in eval(expr, envir, enclos) : object 'drat' not found
In addition: Warning message:
In eval(expr, envir, enclos) : restarting interrupted promise evaluation

我知道使用aes_string而不是aes可以正常工作,例如:

I know using aes_string instead of aes works, e.g.:

ggfun <- function(dat, x.var, y.var){

  ggp <- ggplot(data = dat,
                aes_string(x = x.var,
                    y = y.var)) +
    geom_point()

  return(ggp)
}

ggfun(dat = mtcars, x.var = "drat", y.var = "hp")

但是我宁愿避免使用字符串形式.

But I'd prefer to avoid using string form.

其他尝试包括ggfun(dat = mtcars, x.var = mtcars$drat, y.var = mtcars$hp),它返回正确的图形但部分破坏了练习的目的,并产生标签"x.var"和"y.var",而不是"drat"和"hp".

Other attempts include ggfun(dat = mtcars, x.var = mtcars$drat, y.var = mtcars$hp), which returns the right graph but partially defeats the purpose of the exercise, and produces lables "x.var" and "y.var" instead of "drat" and "hp".

解决这个问题的任何已知和简单方法?

Any known and reasonably simple way around this?

推荐答案

使用dec版本的ggplot2,我们可以传递未加引号的参数,将其转换为quosure(使用enquo)并对其求值()

With the devel version of ggplot2, we can pass unquoted arguments, convert it to quosure (with enquo) and evaluate it (!!)

ggfun <- function(dat, x.var, y.var){
  x.var <- enquo(x.var)
  y.var <- enquo(y.var)
  ggp <- ggplot(data = dat,
                aes(x = !! x.var,
                    y = !! y.var)) +
    geom_point()

  return(ggp)
}

ggfun(dat = mtcars, x.var = drat, y.var = hp)

对于带引号的字符串,请使用sym(来自rlang)将其转换为符号并进行求值

For quoted strings, convert it to symbol with sym (from rlang) and do the evaluation

ggfun <- function(dat, x.var, y.var){
  x.var <- rlang::sym(x.var)
  y.var <- rlang::sym(y.var)
  ggp <- ggplot(data = dat,
                aes(x = !! x.var,
                    y = !! y.var)) +
    geom_point()

  return(ggp)
}

ggfun(dat = mtcars, x.var = "drat", y.var = "hp")

如果要传递带引号或不带引号,则将等价更改为字符(quo_name),然后更改为符号(sym)并求值(!!)

If we want to pass either quoted or unquoted, the quosure is changed to character (quo_name), then to symbol (sym) and evaluate (!!)

ggfun <- function(dat, x.var, y.var){
  x.var <- rlang::sym(quo_name(enquo(x.var)))
  y.var <- rlang::sym(quo_name(enquo(y.var)))
  ggp <- ggplot(data = dat,
                aes(x = !! x.var,
                    y = !! y.var)) +
    geom_point()

  return(ggp)
}

p1 <- ggfun(dat = mtcars, x.var = drat, y.var = hp)
p2 <- ggfun(dat = mtcars, x.var = "drat", y.var = "hp")

all.equal(p1, p2)
#[1] TRUE

这篇关于根据ggplot2标准功能,创建ggplot2函数并指定参数作为数据中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆