Spatial-segmented data with Seurat and RGraphSpace
Sysbiolab Team
2026-12-02
Source:vignettes/articles/spatial-segmentation1.Rmd
spatial-segmentation1.RmdPackage: RGraphSpace 1.5.8.1
Overview
This vignette demonstrates how RGraphSpace renders sf features using spatial-segmented data pre-processed with the Seurat package (Hao et al. 2024).
Before you start
This vignette assumes familiarity with Seurat (Hao et al. 2024), particularly for handling spatial transcriptomics data.
Note: If you are new to Seurat, we recommend reviewing its spatial segmentation tutorials before proceeding.
Computational requirements:
Hardware: Workstation with RAM ≥ 32 GB for large datasets
Software: R (≥4.5); RStudio recommended
Required packages
Before proceeding, ensure that all packages described in the Installation Instructions are installed.
# Check versions
if (packageVersion("RGraphSpace") < "1.5.8"){
message("Need to update 'RGraphSpace' for this vignette")
remotes::install_github("sysbiolab/RGraphSpace")
}
if (packageVersion("Seurat") < "5.5.1"){
message("Need to update 'Seurat' for this vignette")
remotes::install_github("satijalab/Seurat")
}Setting input data
# Load packages
library("RGraphSpace")
library("Seurat")
library("SeuratObject")
library("sf")
library("patchwork")Download the dataset
We will use a dataset provided by 10x Genomics to demonstrate their
Xenium
platform, consisting of spatial transcriptomics data from a fresh
frozen mouse brain. The repository provides a batch download option from
the terminal, using wget or curl; the
wget command is reproduced below.
The Xenium dataset can be downloaded from the 10x Genomics repository:
- Repository URL: https://www.10xgenomics.com/datasets
- Dataset: Fresh Frozen Mouse Brain for Xenium Explorer Demo
- Where to find it: Output and supplemental files
- Download: “Tiny subset”
- File: Xenium_V1_FF_Mouse_Brain_Coronal_Subset_CTX_HP_outs.zip
- MD5: a39fa6d0a751db1f206c915b6419e329
- Size: 3.48 GB
Loading the dataset
We load the Xenium dataset downloaded earlier, including its cell segmentation boundaries.
# Set path to data directory
localdir <- "path/to/data/directory"
# Load the Xenium data
xenium.obj <- LoadXenium(localdir, fov = "fov", segmentations = "cell")Seurat offers several downstream pre-processing steps at this stage,
such as SCTransform normalization and dimensionality
reduction. These are thoroughly documented in Seurat’s own tutorials, so
we do not repeat them here. This vignette focuses instead on using
RGraphSpace to manipulate and visualize the segmented data
directly, for which the raw data is sufficient.
Creating a GraphSpace object
Convert the Seurat object into a
GraphSpace. Xenium reports cell positions in micrometers
(Xenium output documentation), and
Seurat keeps them unchanged, so one coordinate unit is one
micrometer; we record this with gs_coords_unit(), for the
scale bars used later (see physical
scales).
# Coerce 'Seurat' to 'GraphSpace'
gs <- as.GraphSpace(xenium.obj, space = "spatial", layer = "counts")
# Optionally, set a physical unit; Xenium coordinates are in micrometers
gs_coords_unit(gs) <- "um"
gs
#> A GraphSpace-class object for:
#> IGRAPH 8e5d27c UN-- 36602 0 --
#> + attr: x (v/n), y (v/n), name (v/c), nodeLabel (v/c), nodeSize (v/n), orig.ident (v/x),
#> | nCount_Xenium (v/n), nFeature_Xenium (v/n), segmentation_method (v/c),
#> | nCount_BlankCodeword (v/n), nFeature_BlankCodeword (v/n), nCount_ControlCodeword (v/n),
#> | nFeature_ControlCodeword (v/n), nCount_ControlProbe (v/n), nFeature_ControlProbe (v/n),
#> | arrowType (e/c)
#> + geometry: 1 (segmentations)
#> + features: 248 (2010300C02Rik, Acsbg1, Acta2, ...) from logcounts
#> + samples: 36602 (1, 2, ...)
#> + node spatial boundaries: raw graph
#> | x: [234, 5447] (cols)
#> | y: [181, 3534] (rows)
#> + coordinate unit: 1 umThe coercing also extracted the cell segmentation polygons from the
Seurat object as an sf geometry column. Next,
we will normalize both the graph and the geometry together, rotating and
flipping the result to match the orientation used in Seurat’s own
related vignette.
# Normalize graph and geometry coordinates
gs <- normalizeGraphSpace(gs, mar = 0, norm.geometry = TRUE)
# Rotate and flip to follow Seurat's related vignette
gs <- rotateGraphSpace(gs)
gs <- flipGraphSpace(gs)Spatial feature visualization
With the full tissue now laid out, plot gene expression across all cells, marking a region of interest to zoom into next.
# Set color palette and data range for use across plots
cpal <- hcl.colors(100, palette = "Geyser", rev = FALSE)
data_range <- range(log2(gs_fdata(gs) + 1))
# Set a reusable theme
my_theme <- theme_gspace_coords(theme = "th3", is_norm = TRUE,
xlab = "Tissue coordinates 1", ylab = "Tissue coordinates 2")
# Main plot, expression counts of a feature (Slc17a7);
# a box marks a region of interest
p <- ggplot(gs) +
geom_nodespace(mapping = aes(colour = log2(Slc17a7 + 1)),
size = 0.4, pch = 16) +
scale_colour_continuous(palette = cpal, limits = data_range) +
annotate("rect", xmin = 0.55, xmax = 0.75, ymin = 0.45, ymax = 0.65,
fill = NA, colour = "white", lty = "21", lwd = 1) +
annotation_gspace_scalebar() + my_theme
p
Crop to the marked region. Cropping does not automatically re-align coordinates, so both the graph and the geometry need to be normalized again afterward.
# Crop the region of interest
gs_crop <- cropGraphSpace(gs, xmin = 0.55, xmax = 0.75,
ymin = 0.45, ymax = 0.65)
# Re-normalize coordinates for cropping region
gs_crop <- normalizeGraphSpace(gs_crop, mar = 0,
norm.geometry = TRUE)
# Rotate and flip to follow the main plot orientation
gs_crop <- rotateGraphSpace(gs_crop)
gs_crop <- flipGraphSpace(gs_crop)Finally, compare the two representations side by side: nodes alone versus the real cell segmentation shapes, with node centroids overlaid for reference. The scale bar shows the physical size of the cropped region.
# Plot nodes, representing cells
p1 <- ggplot(gs_crop) +
geom_nodespace(mapping = aes(colour = log2(Slc17a7 + 1)),
size = 1.5, pch = 19) +
scale_colour_continuous(palette = cpal, limits = data_range) +
annotation_gspace_scalebar(box = TRUE) +
my_theme
# Plot geometries, representing cells
p2 <- ggplot(gs_crop) +
geom_sf(mapping = aes(geometry = segmentations,
fill = log2(Slc17a7 + 1) )) +
scale_fill_continuous(palette = cpal, limits = data_range) +
geom_nodespace(colour = "black", size = 0.2, pch = 19) +
annotation_gspace_scalebar(box = TRUE) +
my_theme
p1 + p2 +
patchwork::plot_annotation(
title = "RGraphSpace integration with sf geometries",
theme = theme(plot.title = element_text(hjust = 0.5)))
Session information
#> R version 4.6.2 (2026-10-29)
#> Platform: x86_64-pc-linux-gnu
#> Running under: Ubuntu 24.04.5 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=en_US.UTF-8
#> [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: America/Sao_Paulo
#> tzcode source: system (glibc)
#>
#> attached base packages:
#> [1] stats graphics grDevices utils datasets methods base
#>
#> other attached packages:
#> [1] patchwork_1.3.2 sf_1.1-3 Seurat_6.0.1
#> [4] SeuratObject_5.4.0 sp_2.2-4 RGraphSpace_1.5.8.1
#> [7] ggplot2_4.0.4
#>
#> loaded via a namespace (and not attached):
#> [1] DBI_1.3.0 pbapply_1.7-5 gridExtra_2.3.1
#> [4] rlang_1.3.0 magrittr_2.0.5 RcppAnnoy_0.0.23
#> [7] otel_0.3.0 matrixStats_1.5.0 ggridges_0.5.7
#> [10] e1071_1.7-17 compiler_4.6.2 png_0.1-9
#> [13] systemfonts_1.3.4 vctrs_0.7.3 reshape2_1.4.5
#> [16] stringr_1.6.0 pkgconfig_2.0.3 fastmap_1.2.0
#> [19] fontawesome_0.5.3 rmarkdown_2.32 ggbeeswarm_0.7.3
#> [22] ragg_1.5.2 purrr_1.2.2 xfun_0.62
#> [25] cachem_1.1.0 jsonlite_2.0.0 irlba_2.4.1
#> [28] parallel_4.6.2 cluster_2.1.8.3 R6_2.6.1
#> [31] ica_1.0-3 bslib_0.12.0 stringi_1.8.9
#> [34] RColorBrewer_1.1-3 parallelly_1.50.0 jquerylib_0.1.4
#> [37] scattermore_1.2 Rcpp_1.1.2 knitr_1.52
#> [40] future.apply_1.21.0 sctransform_0.4.3 Matrix_1.7-6
#> [43] igraph_2.3.4 tidyselect_1.2.1 rstudioapi_0.19.0
#> [46] dichromat_2.0-1 yaml_2.3.12 codetools_0.2-20
#> [49] listenv_1.1.0 lattice_0.23-1 tibble_3.3.1
#> [52] plyr_1.8.9 withr_3.0.3 S7_1.0.0
#> [55] evaluate_1.0.5 ggrastr_1.0.3 Rtsne_0.17
#> [58] future_1.82.0 fastDummies_1.7.6 desc_1.4.3
#> [61] units_1.0-1 proxy_0.4-29 pillar_1.11.1
#> [64] KernSmooth_2.23-27 generics_0.1.4 RcppHNSW_0.7.0
#> [67] scales_1.4.0 globals_0.20.0 class_7.3-24
#> [70] glue_1.8.1 tools_4.6.2 RSpectra_0.16-2
#> [73] RANN_2.6.3 fs_2.1.0 dotCall64_1.2
#> [76] tidygraph_1.3.1 cowplot_1.2.0 grid_4.6.2
#> [79] tidyr_1.3.2 beeswarm_0.4.0 vipor_0.4.7
#> [82] cli_3.6.6 textshaping_1.1.0 spam_2.11-4
#> [85] dplyr_1.2.1 uwot_0.2.5 gtable_0.3.6
#> [88] pls_2.9-0 sass_0.5.0 digest_0.6.39
#> [91] progressr_1.1.0 classInt_0.4-11 ggrepel_0.9.8
#> [94] htmlwidgets_1.6.4 farver_2.1.2 htmltools_0.5.9
#> [97] pkgdown_2.2.1 lifecycle_1.0.5 httr_1.4.9
#> [100] MASS_7.3-66