I am trying designing an interactive plot. I want the user to specify the facet_grid
formula, however if in both sides of the formula .data
pronoun it does not work. Any workaround?
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]] ~ .)
Here are the R session details
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
CodePudding user response:
Instead of using a formula you could pass the variables to facet by via the vars()
quoting function to the rows
and cols
arguments of 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]]))