使用条件分面和整洁评估的ggplot2分面网格

7
我想创建一个函数,生成一个 ggplot 绘图,并提供一个用于分面变量到 facet_grid() 的可选参数。
特别地,如果可能的话,我希望在 facet_grid 中内嵌条件逻辑。我还想使用整洁评估框架 - 因此不使用公式字符串!
但是,我所做的所有尝试都失败了。
library(tidyverse)
iris <- iris %>% add_column(idx = rep(1:2, 75))

我的第一次尝试失败了,因为 facet_grid 试图找到一个名为 NULL(用反引号括起来)的变量。

plot_iris <- function(df_in, facet_var = NULL){
  ggplot(df_in) +
    geom_point(aes(Sepal.Length, Sepal.Width)) +
    facet_grid(vars(!!enquo(facet_var)), vars(idx))
}

plot_iris(iris)

#> Error: At least one layer must contain all faceting variables: `NULL`.
#> * Plot is missing `NULL`
#> * Layer 1 is missing `NULL`

运行plot_iris(iris, Species)没有问题。

我的第二次尝试也失败了,但是错误信息不同。

plot_iris2 <- function(df_in, facet_var = NULL){
  facet_quo <- enquo(facet_var)

  ggplot(df_in) +
    geom_point(aes(Sepal.Length, Sepal.Width)) +
    facet_grid(rows = ifelse(identical(facet_quo, quo(NULL)), NULL,
                             vars(!!facet_quo)),
               cols = vars(idx))
}

plot_iris2(iris)

#> Error in ans[test & ok] <- rep(yes, length.out = length(ans))[test & ok] : 
#>   replacement has length zero
#> In addition: Warning message:
#> In rep(yes, length.out = length(ans)) :

运行这个尝试时,非NULL可选参数也会失败:

plot_iris2(iris, Species)

#> Error: `rows` must be `NULL` or a `vars()` list if `cols` is a `vars()` list

我的第三次尝试也失败了,出现了相同的错误信息,但是警告不同:

plot_iris3 <- function(df_in, facet_var = NULL){
  facet_quo <- enquo(facet_var)

  ggplot(df_in) +
    geom_point(aes(Sepal.Length, Sepal.Width)) +
    facet_grid(rows = vars(ifelse(identical(facet_quo, quo(NULL)), NULL,
                                  !!facet_quo)))
}

plot_iris3(iris)

#> Error in ans[test & ok] <- rep(yes, length.out = length(ans))[test & ok] : 
#>   replacement has length zero
#> In addition: Warning message:
#> In rep(yes, length.out = length(ans)) :
#>   'x' is NULL so the result will be NULL

使用一个非NULL的可选参数返回一个按idx分面绘制的图表,但不会按Species分面 - 单行有一个分面标签,标记为“1”。
plot_iris3(iris, Species)

有没有其他替代方案,在单个facet_grid调用内使用条件逻辑,并且在可选参数为NULL时也能正常工作?

2个回答

3
也许我们应该从变量规范中移除NULL元素以使其更容易。我已经发布了一个问题:https://github.com/tidyverse/ggplot2/issues/2986 你可以使用rlang::quo_is_null()来检查quo(NULL)。为了清晰起见,我会将其分成单独的步骤进行操作。
plot_iris <- function(df_in, facet_var = NULL){
  facet_quo <- enquo(facet_var)

  if (rlang::quo_is_null(facet_quo)) {
    rows <- vars()
  } else {
    rows <- vars(!!facet_quo)
  }

  ggplot(df_in) +
    geom_point(aes(Sepal.Length, Sepal.Width)) +
    facet_grid(rows, vars(idx))
}

我尝试通过添加两个可选的facet参数来扩展它,但在这种情况下,似乎facet_grid(vars() vars())会失败?谢谢! - Matifou
这个问题是否相关?https://github.com/tidyverse/ggplot2/issues/3070 - Lionel Henry

1

我认为你需要采用第二种方法。关键在于你需要使用switch()返回NULL,而不是ifelse()。请参考示例这里和讨论这里

plot_iris2 <- function(df_in, facet_var = NULL){
    facet_quo <- enquo(facet_var)

    ggplot(df_in) +
        geom_point(aes(Sepal.Length, Sepal.Width)) +
        facet_grid(rows = switch(identical(facet_quo, quo(NULL)) + 1, 
                                 vars(!!facet_quo),
                                 NULL),
                   cols = vars(idx))
}

plot_iris2(iris, facet_var = NULL)

enter image description here

plot_iris2(iris, facet_var = Species)

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接