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

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

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

问题

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

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

  1. library(ggplot2)
  2. set.seed(1)
  3. dat <- data.frame(x=rnorm(100), y=rnorm(100))
  4. xvar <- 'x+100'
  5. yvar <- 'y'
  6. # 这个可以工作,但使用了不推荐使用的aes_string
  7. ggplot(dat, aes_string(x=xvar, y=yvar))
  8. # 这个可以工作
  9. ggplot(dat, ggplot2::aes(x=x+100, y=.data[[yvar]]) + geom_point()
  10. # 这个无法工作
  11. 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.

  1. library(ggplot2)
  2. set.seed(1)
  3. dat &lt;- data.frame(x=rnorm(100),y=rnorm(100))
  4. xvar &lt;- &#39;x+100&#39;
  5. yvar &lt;- &#39;y&#39;
  6. #This works but uses the deprecated aes_string
  7. ggplot(dat,aes_string(x=xvar,y=yvar))
  8. #This works
  9. ggplot(dat, ggplot2::aes(x=x+100, y=.data[[yvar]])) + geom_point()
  10. #This does not work
  11. 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

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

-output

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


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

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

It may need parse_expr/eval

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

-output

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


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

  1. eval(parse(text = glue::glue(&quot;ggplot(dat, aes(x = {xvar}, y = {yvar}))&quot;,
  2. &quot;+ geom_point()&quot;)))
  3. </details>
  4. # 答案2
  5. **得分**: 0
  6. 你可以使用 `eval(str2expression())`

library(ggplot2)

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

  1. 或者使用类似的 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)

  1. 请注意,你需要手动使用 `labs()` 来设置轴标签;否则,你会得到像 x 轴上的 `&quot;eval(str2expression(xvar))&quot;` 这样的标签。
  2. <details>
  3. <summary>英文:</summary>
  4. 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)

  1. 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]][1]
  2. Note youll want to manually set the axis labels using `labs()`; otherwise youll end up with e.g. `&quot;eval(str2expression(xvar))&quot;` for the x axis.
  3. [1]: https://i.stack.imgur.com/TMmr4.jpg
  4. </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:

确定