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

### 2025-12-01

## 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.5.0 (2025-04-11)
    ## Platform: x86_64-apple-darwin20
    ## Running under: macOS Sequoia 15.7.2
    ##
    ## Matrix products: default
    ## BLAS:   /Library/Frameworks/R.framework/Versions/4.5-x86_64/Resources/lib/libRblas.0.dylib
    ## LAPACK: /Library/Frameworks/R.framework/Versions/4.5-x86_64/Resources/lib/libRlapack.dylib;  LAPACK version 3.12.1
    ##
    ## locale:
    ## [1] C/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
    ##
    ## time zone: Australia/Melbourne
    ## tzcode source: internal
    ##
    ## attached base packages:
    ## [1] stats     graphics  grDevices utils     datasets  methods   base
    ##
    ## other attached packages:
    ## [1] readr_2.1.6     stringr_1.6.0   ggplot2_4.0.1   dplyr_1.1.4
    ## [5] tidygate_1.0.19 knitr_1.50
    ##
    ## loaded via a namespace (and not attached):
    ##  [1] viridis_0.6.5      plotly_4.11.0      generics_0.1.4     tidyr_1.3.1
    ##  [5] stringi_1.8.7      hms_1.1.4          digest_0.6.38      magrittr_2.0.4
    ##  [9] evaluate_1.0.5     grid_4.5.0         RColorBrewer_1.1-3 fastmap_1.2.0
    ## [13] jsonlite_2.0.0     gridExtra_2.3      promises_1.5.0     httr_1.4.7
    ## [17] purrr_1.2.0        viridisLite_0.4.2  scales_1.4.0       codetools_0.2-20
    ## [21] lazyeval_0.2.2     cli_3.6.5          shiny_1.11.1       rlang_1.1.6
    ## [25] withr_3.0.2        otel_0.2.0         tools_4.5.0        tzdb_0.5.0
    ## [29] httpuv_1.6.16      vctrs_0.6.5        R6_2.6.1           mime_0.13
    ## [33] lifecycle_1.0.4    htmlwidgets_1.6.4  pkgconfig_2.0.3    pillar_1.11.1
    ## [37] later_1.4.4        gtable_0.3.6       data.table_1.17.8  glue_1.8.0
    ## [41] Rcpp_1.1.0         xfun_0.54          tibble_3.3.0       tidyselect_1.2.1
    ## [45] farver_2.1.2       xtable_1.8-4       htmltools_0.5.8.1  compiler_4.5.0
    ## [49] S7_0.2.1