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

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:

  • tidyomics - A software ecosystem for tidy analysis of omic data.
  • tidyHeatmap - Produce heatmaps with tidy principles.

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.2 (2024-10-31)
## Platform: x86_64-pc-linux-gnu
## Running under: Ubuntu 24.04.1 LTS
## 
## Matrix products: default
## BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3 
## LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.26.so;  LAPACK version 3.12.0
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=C              
##  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
##  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
## 
## time zone: Etc/UTC
## tzcode source: system (glibc)
## 
## 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.49     
## 
## loaded via a namespace (and not attached):
##  [1] viridis_0.6.5      plotly_4.10.4      sass_0.4.9         utf8_1.2.4        
##  [5] generics_0.1.3     tidyr_1.3.1        stringi_1.8.4      hms_1.1.3         
##  [9] digest_0.6.37      magrittr_2.0.3     evaluate_1.0.1     grid_4.4.2        
## [13] RColorBrewer_1.1-3 fastmap_1.2.0      jsonlite_1.8.9     gridExtra_2.3     
## [17] promises_1.3.0     httr_1.4.7         purrr_1.0.2        fansi_1.0.6       
## [21] viridisLite_0.4.2  scales_1.3.0       codetools_0.2-20   lazyeval_0.2.2    
## [25] jquerylib_0.1.4    cli_3.6.3          shiny_1.9.1        rlang_1.1.4       
## [29] munsell_0.5.1      withr_3.0.2        cachem_1.1.0       yaml_2.3.10       
## [33] tools_4.4.2        tzdb_0.4.0         colorspace_2.1-1   httpuv_1.6.15     
## [37] buildtools_1.0.0   vctrs_0.6.5        R6_2.5.1           mime_0.12         
## [41] lifecycle_1.0.4    htmlwidgets_1.6.4  pkgconfig_2.0.3    pillar_1.9.0      
## [45] bslib_0.8.0        later_1.3.2        gtable_0.3.6       glue_1.8.0        
## [49] data.table_1.16.2  Rcpp_1.0.13-1      xfun_0.49          tibble_3.2.1      
## [53] tidyselect_1.2.1   sys_3.4.3          xtable_1.8-4       htmltools_0.5.8.1 
## [57] rmarkdown_2.29     maketools_1.3.1    compiler_4.4.2