Lifecycle:maturing

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

knitr::opts_chunk$set(cache = TRUE, warning = FALSE, message = FALSE, cache.lazy = FALSE)

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

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

load("../data/demo_gate_data.rda")

# Load pre-recorded brush path from data for example
tidygate_env <<- rlang::env()
tidygate_env$gates <- demo_gate_data

mtcars_gated <- 
  mtcars |>
  mutate(gated = gate(x = mpg, y = wt, programmatic_gates = tidygate_env$gates))

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

# Select points within gate 2
mtcars_gated |>
  filter(str_detect(gated, "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()
# 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))
mtcars |>
  mutate(gated = gate(x = mpg, y = wt, programmatic_gates = tidygate_env$gates)) |>
  filter(!is.na(gated))
sessionInfo()


stemangiola/tidygate documentation built on Sept. 17, 2024, 10:43 p.m.