使用整洁评估(tidy evaluation)将表达式作为字符传递给 ggplot2::aes()。

huangapple go评论58阅读模式
英文:

Use tidy evaluation when passing an expression as a character to ggplot2::aes()

问题

我正在尝试将我在函数中使用的ggplot2转换为使用整洁评估,以避免评估的警告消息。特别是,我广泛使用了aes_string(),这些需要转换为aes()。当仅传递列的名称作为字符时,我可以处理这种情况。然而,我一直无法解决当字符是数学表达式时该如何处理的情况。

这里是一个小的可重现问题示例,我正在尝试解决这个问题。

library(ggplot2)
set.seed(1)

dat <- data.frame(x=rnorm(100), y=rnorm(100))
xvar <- 'x+100'
yvar <- 'y'

# 这个可以工作,但使用了不推荐使用的aes_string
ggplot(dat, aes_string(x=xvar, y=yvar))

# 这个可以工作
ggplot(dat, ggplot2::aes(x=x+100, y=.data[[yvar]]) + geom_point()

# 这个无法工作
ggplot(dat, aes(x={{xvar}}, y=.data[[yvar]]) + geom_point()

我的问题是,我需要使用哪些整洁评估技巧来使用xvar 来指定 x 变量,就像aes_string() 中那样可能?

英文:

I am trying to convert my use of ggplot2 in functions to using tidy evaluation so as to avoid the warning messages that are evaluated. I particular, I have extensively used aes_string() and these need to be converted to aes(). I can handle cases when just the name of a column is passed as a characater. However, I have been unable to work how to deal with the case when the character is a mathematical expression.

Here is a small reproducible example of the problem that I an trying to solve.

library(ggplot2)
set.seed(1)

dat &lt;- data.frame(x=rnorm(100),y=rnorm(100))
xvar &lt;- &#39;x+100&#39;
yvar &lt;- &#39;y&#39;

#This works but uses the deprecated aes_string
ggplot(dat,aes_string(x=xvar,y=yvar))

#This works
ggplot(dat, ggplot2::aes(x=x+100, y=.data[[yvar]]))  + geom_point()

#This does not work
ggplot(dat, aes(x={{xvar}}, y=.data[[yvar]])) + geom_point()

My question is what tidy evaluation techniques do I need to employ to use xvar to specify the x variable as is possible with aes_string()?

答案1

得分: 0

可能需要 parse_expr/eval

library(ggplot2)
ggplot(dat, aes(x=eval(rlang::parse_expr(xvar)), y=.data[[yvar]])) + 
     geom_point() +
     xlab(xvar)

-output

使用整洁评估(tidy evaluation)将表达式作为字符传递给 ggplot2::aes()。


或者另一种选择是插值和执行 eval/parse

eval(parse(text = glue::glue("ggplot(dat, aes(x = {xvar}, y = {yvar}))",
    "+ geom_point()")))
英文:

It may need parse_expr/eval

library(ggplot2)
ggplot(dat, aes(x=eval(rlang::parse_expr(xvar)), y=.data[[yvar]])) + 
     geom_point() +
     xlab(xvar)

-output

使用整洁评估(tidy evaluation)将表达式作为字符传递给 ggplot2::aes()。


Or another option would be to interpolate and do the eval/parse

eval(parse(text = glue::glue(&quot;ggplot(dat, aes(x = {xvar}, y = {yvar}))&quot;,
    &quot;+ geom_point()&quot;)))


</details>



# 答案2
**得分**: 0

你可以使用 `eval(str2expression())`:

library(ggplot2)

ggplot(dat, aes(x = eval(str2expression(xvar)), y = eval(str2expression(yvar)))) +
geom_point() +
labs(x = xvar, y = yvar)


或者使用类似的 rlang 函数:

library(rlang)

ggplot(dat, aes(x = eval_tidy(parse_expr(xvar)), y = eval_tidy(parse_expr(yvar)))) +
geom_point() +
labs(x = xvar, y = yvar)


请注意,你需要手动使用 `labs()` 来设置轴标签;否则,你会得到像 x 轴上的 `&quot;eval(str2expression(xvar))&quot;` 这样的标签。

<details>
<summary>英文:</summary>

You could use `eval(str2expression())`:

library(ggplot2)

ggplot(dat, aes(x = eval(str2expression(xvar)), y = eval(str2expression(yvar)))) +
geom_point() +
labs(x = xvar, y = yvar)

Or using the analogous rlang functions:

library(rlang)

ggplot(dat, aes(x = eval_tidy(parse_expr(xvar)), y = eval_tidy(parse_expr(yvar)))) +
geom_point() +
labs(x = xvar, y = yvar)

[![][1]][1]

Note you’ll want to manually set the axis labels using `labs()`; otherwise you’ll end up with e.g. `&quot;eval(str2expression(xvar))&quot;` for the x axis. 

  [1]: https://i.stack.imgur.com/TMmr4.jpg

</details>



huangapple
  • 本文由 发表于 2023年2月19日 12:25:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75497981.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定