Summarize cell counts and proportions inside spatial regions
Source:R/StatsCellsInside.R
StatsCellsInside.RdComputes the number and proportion of cells from each cluster inside boundaries or ring regions. This function is useful for downstream visualizations such as bar plots or pie charts showing the spatial composition of cell types per region.
Arguments
- cells_inside
An
sfobject of cells returned bygetCellsInside(). Must containclusterandregion_idcolumns.
Value
A data frame with one row per cluster per region, containing the following columns:
region_id: Identifier for each spatial region.cluster: Cluster label of the cells.count: Number of cells from the given cluster in the region.proportion: Proportion of cells from the given cluster relative to the total number of cells in the region.
Examples
# Load coordinates data
coords <- readRDS(system.file("extdata", "MouseBrainCoords.rds",
package = "SpNeigh"
))
# Get boundary and cells inside
boundary_points <- getBoundary(
data = coords, one_cluster = 2,
eps = 120, minPts = 10
)
# Select regions of interests if needed (Optional)
boundary_points <- subset(boundary_points, region_id == 2)
cells_inside <- getCellsInside(data = coords, boundary = boundary_points)
# Summarize cluster statistics per region
stats_cells <- statsCellsInside(cells_inside)
head(stats_cells)
#> # A tibble: 4 × 4
#> region_id cluster count proportion
#> <chr> <fct> <int> <dbl>
#> 1 2 0 313 0.536
#> 2 2 2 159 0.272
#> 3 2 3 3 0.00514
#> 4 2 5 109 0.187