我正在嘗試設計一個互動式情節。我希望用戶指定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
uj5u.com熱心網友回復:
而不是使用一個公式您可以通過通過傳遞變數方面vars()參考函式的rows和cols的引數facet_grid:
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]]))

轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/356511.html
上一篇:%||%運算式在R中有什么作用
下一篇:基于其他列R的因素重命名級別
