Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

facet grid using .data pronoun on both formula sides not working

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
like image 260
Kozolovska Avatar asked Nov 12 '21 07:11

Kozolovska


People also ask

What is a facet grid in R?

Source: R/facet-grid-.r facet_grid () forms a matrix of panels defined by row and column faceting variables. It is most useful when you have two discrete variables, and all combinations of the variables exist in the data. If you have only one variable with many levels, try facet_wrap ().

How to create a small multiple grid using facet_grid?

Inside of facet_grid, we need to specify two variables, separated by a tilde symbol, ~. The first variable specifies the “rows” of the small multiple grid. There will be one row in the small multiple grid for every value of the first variable. The second variable specifies the “columns” of the small multiple grid.

How to use discrete variables with facet_grid () in Python?

We can use those discrete variables with facet_grid () to create a grid layout. So let’s say that var_1 has two values, A and B. And var_2 also has two values, 1 and 2. When we use these two variables with facet_grid, it will create a grid of small versions of our density plot.

What is the difference between facet_wrap and facet_grid?

These two techniques primarily differ in how they arrange the panels of the small multiple chart. facet_wrap “wraps” the panels around, row by row, whereas facet_grid creates a grid of panels using two discrete variables. facet_grid creates small multiple charts with a specific structure.


1 Answers

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]]))

like image 101
stefan Avatar answered Oct 16 '22 12:10

stefan