问题在于使用ggmosaic::product创建的ggplot自定义函数内部的准引用。

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

Problems with quasiquotation inside ggplot custom function using ggmosaic::product

问题

I want to create a function that taking a data base, variable_1 y variable_2 builds a mosaic plot!

var_1<-rep(c("A","B","C","B","A","C"),10)
var_2<-c(rep(c("1","0"),10),rep("0",5),rep(c("0","1","0"),10),rep("1",5) )
dat<-data.frame(var_1,var_2)

The function takes 3 arguments as explained:

mosaic_fig<-function(db,var1,var2){
  library(ggmosaic)
  mosac <- ggplot2::ggplot(data = db) +
    geom_mosaic(ggplot2::aes(x = product({{var2}}), fill = {{var1}})) +   
    ggplot2::labs(y="", x="", title = "")
  return(mosac)
}

Now I try to use the function with the data I just provided and this happens:

windows()
mosaic_fig(dat,var_1,var_2)

Warning message:
Computation failed in `stat_mosaic()`
Caused by error in `[.data.frame`:
! undefined columns selected 

It is clear I'm having problems with the quasiquotation in the product() function. I have used quasiquotation in other ggplot functions and it worked nicely... why not here?

英文:

I want to create a function that taking a data base, variable_1 y variable_2 builds a mosaic plot!

var_1&lt;-rep(c(&quot;A&quot;,&quot;B&quot;,&quot;C&quot;,&quot;B&quot;,&quot;A&quot;,&quot;C&quot;),10)
var_2&lt;-c(rep(c(&quot;1&quot;,&quot;0&quot;),10),rep(&quot;0&quot;,5),rep(c(&quot;0&quot;,&quot;1&quot;,&quot;0&quot;),10),rep(&quot;1&quot;,5) )
dat&lt;-data.frame(var_1,var_2)

The function takes 3 arguments as explained:

mosaic_fig&lt;-function(db,var1,var2){
  library(ggmosaic)
  mosac &lt;- ggplot2::ggplot(data = db) +
    geom_mosaic(ggplot2::aes(x = product({{var2}}), fill = {{var1}})) +   
    ggplot2::labs(y=&quot;&quot;, x=&quot;&quot;, title = &quot;&quot;)
  return(mosac)
}

Now I try to use the function with the data I just provided and this happens:

windows()
mosaic_fig(dat,var_1,var_2)

Warning message:
Computation failed in `stat_mosaic()`
Caused by error in `[.data.frame`:
! undefined columns selected 

It is clear I'm having problems with the quasiquotation in the product() function. I have used quasiquotation in other ggplot functions and it worked nicely... why not here?

答案1

得分: 4

你需要使用 rlang::enexprs 替代 rlang::exprsggmosaic::product 调用的函数)。

help("topic-defuse") 中:

你可以使用 expr() 来解析你自己的 R 表达式(...)
你可以使用以 en 为前缀的操作符来解析用户提供的表达式(...)

var_1 <- rep(c("A", "B", "C", "B", "A", "C"), 10)
var_2 <- c(rep(c("1", "0"), 10), rep("0", 5), rep(c("0", "1", "0"), 10), rep("1", 5))
dat <- data.frame(var_1, var_2)

## 最好不要在函数内部要求加载库
library(ggmosaic)
#> 加载需要的包: ggplot2
mosaic_fig <- function(db, var1, var2) {
  ggplot2::ggplot(data = db) +
    ggmosaic::geom_mosaic(ggplot2::aes(x = rlang::enexprs(var2), fill = {{ var1 }})) +
    ggplot2::labs(y = "", x = "", title = "")
}
mosaic_fig(dat, var_1, var_2)
#> 警告: `unite_()` 在 tidyr 1.2.0 中已弃用。
#> 请使用 `unite()` 代替。
#> 弃用功能可能在 ggmosaic 包中使用,请在 <https://github.com/haleyjeppson/ggmosaic> 报告问题。

![](https://i.stack.imgur.com/vSb9c.png)<!-- -->

<sup>创建于 2023-04-10,使用 [reprex v2.0.2](https://reprex.tidyverse.org)</sup>


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

You will need `rlang::enexprs` instead of `rlang::exprs` (which is called by `ggmosaic::product`). 

From `help(&quot;topic-defuse&quot;)`:
&gt; You can defuse your own R expressions with expr(). (...)
You can defuse the expressions supplied by the user of your function with the en-prefixed operators (...)

``` r
var_1 &lt;- rep(c(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;B&quot;, &quot;A&quot;, &quot;C&quot;), 10)
var_2 &lt;- c(rep(c(&quot;1&quot;, &quot;0&quot;), 10), rep(&quot;0&quot;, 5), rep(c(&quot;0&quot;, &quot;1&quot;, &quot;0&quot;), 10), rep(&quot;1&quot;, 5))
dat &lt;- data.frame(var_1, var_2)

## better not to require libraries within a function 
library(ggmosaic)
#&gt; Loading required package: ggplot2
mosaic_fig &lt;- function(db, var1, var2) {
  ggplot2::ggplot(data = db) +
    ggmosaic::geom_mosaic(ggplot2::aes(x = rlang::enexprs(var2), fill = {{ var1 }})) +
    ggplot2::labs(y = &quot;&quot;, x = &quot;&quot;, title = &quot;&quot;)
}
mosaic_fig(dat, var_1, var_2)
#&gt; Warning: `unite_()` was deprecated in tidyr 1.2.0.
#&gt; ℹ Please use `unite()` instead.
#&gt; ℹ The deprecated feature was likely used in the ggmosaic package.
#&gt;   Please report the issue at &lt;https://github.com/haleyjeppson/ggmosaic&gt;.

问题在于使用ggmosaic::product创建的ggplot自定义函数内部的准引用。<!-- -->

<sup>Created on 2023-04-10 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年4月10日 22:01:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75977781.html
匿名

发表评论

匿名网友

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

确定