在两个公式端都使用 .data 代词的小平面网格不起作用
facet grid using .data pronoun on both formula sides not working
我正在尝试设计一个互动情节。我希望用户指定 facet_grid
公式,但是如果在公式的两边 .data
代词它不起作用。有什么解决方法吗?
left <- 'Species'
right <- 'Petal.Width'
### not working
ggplot(iris, aes(y = Sepal.Length, x = Sepal.Width)) +
geom_line() +
facet_grid(.data[[left]] ~ .data[[right]])
### working
ggplot(iris, aes(y = Sepal.Length, x = Sepal.Width)) +
geom_line() +
facet_grid(.data[[left]] ~ .)
这是 R 会话的详细信息
R version 4.0.4 (2021-02-15)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044)
Matrix products: default
locale:
[1] LC_COLLATE=English_Israel.1252 LC_CTYPE=English_Israel.1252 LC_MONETARY=English_Israel.1252
[4] LC_NUMERIC=C LC_TIME=English_Israel.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] shiny_1.6.0 forcats_0.5.1 stringr_1.4.0 dplyr_1.0.7 purrr_0.3.4 readr_1.4.0
[7] tidyr_1.1.4 tibble_3.1.5 ggplot2_3.3.5 tidyverse_1.3.1
您可以通过 vars()
引用函数将变量传递给 facet_grid
的 rows
和 cols
参数,而不是使用公式:
left <- 'Species'
right <- 'Petal.Width'
library(ggplot2)
ggplot(iris, aes(y = Sepal.Length, x = Sepal.Width)) +
geom_line() +
facet_grid(rows = vars(.data[[left]]), cols = vars(.data[[right]]))
我正在尝试设计一个互动情节。我希望用户指定 facet_grid
公式,但是如果在公式的两边 .data
代词它不起作用。有什么解决方法吗?
left <- 'Species'
right <- 'Petal.Width'
### not working
ggplot(iris, aes(y = Sepal.Length, x = Sepal.Width)) +
geom_line() +
facet_grid(.data[[left]] ~ .data[[right]])
### working
ggplot(iris, aes(y = Sepal.Length, x = Sepal.Width)) +
geom_line() +
facet_grid(.data[[left]] ~ .)
这是 R 会话的详细信息
R version 4.0.4 (2021-02-15)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044)
Matrix products: default
locale:
[1] LC_COLLATE=English_Israel.1252 LC_CTYPE=English_Israel.1252 LC_MONETARY=English_Israel.1252
[4] LC_NUMERIC=C LC_TIME=English_Israel.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] shiny_1.6.0 forcats_0.5.1 stringr_1.4.0 dplyr_1.0.7 purrr_0.3.4 readr_1.4.0
[7] tidyr_1.1.4 tibble_3.1.5 ggplot2_3.3.5 tidyverse_1.3.1
您可以通过 vars()
引用函数将变量传递给 facet_grid
的 rows
和 cols
参数,而不是使用公式:
left <- 'Species'
right <- 'Petal.Width'
library(ggplot2)
ggplot(iris, aes(y = Sepal.Length, x = Sepal.Width)) +
geom_line() +
facet_grid(rows = vars(.data[[left]]), cols = vars(.data[[right]]))