tidygate: high-level data analysis and manipulation in tidyverse style

2024-09-17

Lifecycle:maturing

Introduction

tidygate allows you to interactively gate points on a scatter plot. Interactively drawn gates are recorded and can be applied programmatically to reproduce results exactly. Programmatic gating is based on the package gatepoints by Wajid Jawaid.

For more tidy data analysis:

Installation

# From Github
devtools::install_github("stemangiola/tidygate")

# From CRAN
install.package("tidygate")

Example usage

tidygate provides a single user-facing function: gate. The following examples make use of this function, four packages from the tidyverse and the inbuilt mtcars dataset.

library(dplyr)
library(ggplot2)
library(stringr)
library(readr)
library(tidygate)

mtcars |>
  head()
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

By default, gate creates an interactive scatter plot based on user-defined X and Y coordinates. Colour, shape, size and alpha can be defined as constant values, or can be controlled by values in a specified column.

Once the plot has been created, multiple gates can be drawn with the mouse. When you have finished, click continue. gate will then return a vector of strings, recording the gates each X and Y coordinate pair is within.

mtcars_gated <- 
  mtcars |>
  mutate(gated = gate(x = mpg, y = wt, colour = disp))

To select points which appear within any gates, filter for non-NA values. To select points which appear within a specific gate, string pattern matching can be used.

# Select points within any gate
mtcars_gated |> 
  filter(!is.na(gated))
##                    mpg cyl  disp  hp drat    wt  qsec vs am gear carb gated
## Hornet 4 Drive    21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1     2
## Hornet Sportabout 18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2   1,2
## Valiant           18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1   1,2
## Duster 360        14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4     1
## Merc 240D         24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2     2
## Merc 230          22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2     2
## Merc 280          19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4   1,2
## Merc 280C         17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4   1,2
## Merc 450SE        16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3     1
## Merc 450SL        17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3   1,2
## Merc 450SLC       15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3     1
## Dodge Challenger  15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2     1
## AMC Javelin       15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2     1
## Camaro Z28        13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4     1
## Pontiac Firebird  19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2   1,2
## Ford Pantera L    15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4     1
## Maserati Bora     15.0   8 301.0 335 3.54 3.570 14.60  0  1    5    8     1
# Select points within gate 2
mtcars_gated |>
  filter(str_detect(gated, "2"))
##                    mpg cyl  disp  hp drat    wt  qsec vs am gear carb gated
## Hornet 4 Drive    21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1     2
## Hornet Sportabout 18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2   1,2
## Valiant           18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1   1,2
## Merc 240D         24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2     2
## Merc 230          22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2     2
## Merc 280          19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4   1,2
## Merc 280C         17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4   1,2
## Merc 450SL        17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3   1,2
## Pontiac Firebird  19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2   1,2

Details of the interactively drawn gates are saved to tidygate_env$gates. This variable is overwritten each time interactive gates are drawn, so save it right away if you would like to access it later.

# Inspect previously drawn gates
tidygate_env$gates |>
  head()
## # A tibble: 6 × 3
##       x     y .gate
##   <dbl> <dbl> <dbl>
## 1  20.4  3.60     1
## 2  20.3  3.86     1
## 3  18.7  4.26     1
## 4  16.0  4.34     1
## 5  12.1  4.34     1
## 6  11.7  4.26     1
# Save if needed
tidygate_env$gates |>
  write_rds("important_gates.rds")

If previously drawn gates are supplied to the programmatic_gates argument, points will be gated programmatically. This feature allows the reproduction of previously drawn interactive gates.

important_gates <-
  read_rds("important_gates.rds")

mtcars |>
  mutate(gated = gate(x = mpg, y = wt, programmatic_gates = important_gates)) |>
  filter(!is.na(gated))
##                    mpg cyl  disp  hp drat    wt  qsec vs am gear carb gated
## Hornet 4 Drive    21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1     2
## Hornet Sportabout 18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2   1,2
## Valiant           18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1   1,2
## Duster 360        14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4     1
## Merc 240D         24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2     2
## Merc 230          22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2     2
## Merc 280          19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4   1,2
## Merc 280C         17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4   1,2
## Merc 450SE        16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3     1
## Merc 450SL        17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3   1,2
## Merc 450SLC       15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3     1
## Dodge Challenger  15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2     1
## AMC Javelin       15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2     1
## Camaro Z28        13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4     1
## Pontiac Firebird  19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2   1,2
## Ford Pantera L    15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4     1
## Maserati Bora     15.0   8 301.0 335 3.54 3.570 14.60  0  1    5    8     1
sessionInfo()
## R version 4.4.1 (2024-06-14)
## Platform: x86_64-apple-darwin20
## Running under: macOS Sonoma 14.6.1
## 
## Matrix products: default
## BLAS:   /Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/lib/libRblas.0.dylib 
## LAPACK: /Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/lib/libRlapack.dylib;  LAPACK version 3.12.0
## 
## locale:
## [1] C/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
## 
## time zone: Europe/Helsinki
## tzcode source: internal
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] readr_2.1.5     stringr_1.5.1   ggplot2_3.5.1   dplyr_1.1.4    
## [5] tidygate_1.0.14 knitr_1.48     
## 
## loaded via a namespace (and not attached):
##  [1] viridis_0.6.5      plotly_4.10.4      utf8_1.2.4         generics_0.1.3    
##  [5] tidyr_1.3.1        stringi_1.8.4      hms_1.1.3          digest_0.6.37     
##  [9] magrittr_2.0.3     evaluate_0.24.0    grid_4.4.1         RColorBrewer_1.1-3
## [13] fastmap_1.2.0      jsonlite_1.8.8     gridExtra_2.3      promises_1.3.0    
## [17] httr_1.4.7         purrr_1.0.2        fansi_1.0.6        viridisLite_0.4.2 
## [21] scales_1.3.0       codetools_0.2-20   lazyeval_0.2.2     cli_3.6.3         
## [25] shiny_1.9.1        rlang_1.1.4        munsell_0.5.1      withr_3.0.1       
## [29] tools_4.4.1        tzdb_0.4.0         colorspace_2.1-1   httpuv_1.6.15     
## [33] vctrs_0.6.5        R6_2.5.1           mime_0.12          lifecycle_1.0.4   
## [37] htmlwidgets_1.6.4  pkgconfig_2.0.3    pillar_1.9.0       later_1.3.2       
## [41] gtable_0.3.5       data.table_1.16.0  glue_1.7.0         Rcpp_1.0.13       
## [45] xfun_0.47          tibble_3.2.1       tidyselect_1.2.1   rstudioapi_0.16.0 
## [49] xtable_1.8-4       htmltools_0.5.8.1  compiler_4.4.1