ggplot2 facet grid with conditional facets 和 tidy evaluation
ggplot2 facet grid with conditional facets and tidy evaluation
我想创建一个生成 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
可选参数 returns 由 idx
而不是 Species
分面的图 - 单行有一个分面标签,标记为"1".
plot_iris3(iris, Species)
是否有任何替代方案在单个 facet_grid
调用中使用条件逻辑,并且在可选参数为 NULL
时有效?
我认为您需要第二种方法。关键是你需要 switch()
到 return NULL
,而不是 ifelse()
。请参阅示例 here and discussion here。
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)
plot_iris2(iris, facet_var = Species)
也许我们应该从 vars 规范中删除 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))
}
我想创建一个生成 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
可选参数 returns 由 idx
而不是 Species
分面的图 - 单行有一个分面标签,标记为"1".
plot_iris3(iris, Species)
是否有任何替代方案在单个 facet_grid
调用中使用条件逻辑,并且在可选参数为 NULL
时有效?
我认为您需要第二种方法。关键是你需要 switch()
到 return NULL
,而不是 ifelse()
。请参阅示例 here and discussion here。
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)
plot_iris2(iris, facet_var = Species)
也许我们应该从 vars 规范中删除 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))
}