Skip to content

Commit

Permalink
fix: refactor gate statictics
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanppetkov committed Mar 3, 2025
1 parent 24afa97 commit 87dcfb4
Showing 1 changed file with 13 additions and 25 deletions.
38 changes: 13 additions & 25 deletions R/density_scatter_plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -510,48 +510,36 @@ DensityScatterPlot <- function(
# Calculate points inside gates for each facet group
gate_data <- gate_data %>%
group_by(!!!syms(facet_vars)) %>%
group_modify(function(.x, .y) {
# Get the current facet's data by filtering based on group keys
current_data <- if (!is.null(facet_vars)) {
filter_conds <- lapply(facet_vars, function(var) {
facet_var <- sym(var)
quo(!!facet_var == .y[[var]])
})
plot_data %>%
filter(!!!filter_conds)
} else {
plot_data
}

group_modify(function(group_data, group_keys) {
if (gate_type == "rectangle") {
.x %>%
group_data %>%
rowwise() %>%
mutate(
n_inside = sum(
current_data$marker1 >= xmin &
current_data$marker1 <= xmax &
current_data$marker2 >= ymin &
current_data$marker2 <= ymax
plot_data$marker1 >= xmin &
plot_data$marker1 <= xmax &
plot_data$marker2 >= ymin &
plot_data$marker2 <= ymax
),
total = nrow(current_data)
total = nrow(plot_data)
)
} else {
.x %>%
group_data %>%
crossing(
quadrant = c("top_left", "top_right", "bottom_left", "bottom_right")
) %>%
rowwise() %>%
mutate(
total = nrow(current_data),
total = nrow(plot_data),
n_inside = sum(
if (quadrant == "top_left") {
current_data$marker1 < x & current_data$marker2 > y
plot_data$marker1 < x & plot_data$marker2 > y
} else if (quadrant == "top_right") {
current_data$marker1 >= x & current_data$marker2 > y
plot_data$marker1 >= x & plot_data$marker2 > y
} else if (quadrant == "bottom_left") {
current_data$marker1 < x & current_data$marker2 <= y
plot_data$marker1 < x & plot_data$marker2 <= y
} else {
current_data$marker1 >= x & current_data$marker2 <= y
plot_data$marker1 >= x & plot_data$marker2 <= y
}
)
)
Expand Down

0 comments on commit 87dcfb4

Please sign in to comment.