CytoSPACER provides comprehensive visualization functions built on
ggplot2. This vignette showcases the various plotting
options available for exploring and presenting your results.
First, let’s create a simulated dataset:
set.seed(123)
# Parameters
n_genes <- 100
n_cells <- 500
n_spots <- 50
# Create scRNA-seq data
sc_data <- matrix(rpois(n_genes * n_cells, lambda = 5), nrow = n_genes, ncol = n_cells)
rownames(sc_data) <- paste0("Gene", seq_len(n_genes))
colnames(sc_data) <- paste0("Cell", seq_len(n_cells))
# Define cell types
cell_types <- rep(c("TypeA", "TypeB", "TypeC", "TypeD", "TypeE"), each = 100)
names(cell_types) <- colnames(sc_data)
# Add cell type-specific expression
for (i in 1:5) {
marker_genes <- ((i-1)*20 + 1):(i*20)
type_cells <- which(cell_types == unique(cell_types)[i])
sc_data[marker_genes, type_cells] <- sc_data[marker_genes, type_cells] + 20
}
# Create ST data
st_data <- matrix(rpois(n_genes * n_spots, lambda = 50), nrow = n_genes, ncol = n_spots)
rownames(st_data) <- paste0("Gene", seq_len(n_genes))
colnames(st_data) <- paste0("Spot", seq_len(n_spots))
# Create spatial coordinates (grid pattern)
coordinates <- data.frame(
row = rep(1:10, each = 5),
col = rep(1:5, times = 10),
row.names = colnames(st_data)
)
# Create cell type fractions
unique_types <- unique(cell_types)
n_types <- length(unique_types)
cell_type_fractions <- as.data.frame(matrix(
1/n_types, nrow = n_spots, ncol = n_types,
dimnames = list(colnames(st_data), unique_types)
))
# Run CytoSPACER
results <- run_cytospace(
sc_data = sc_data,
cell_types = cell_types,
st_data = st_data,
coordinates = coordinates,
cell_type_fractions = cell_type_fractions,
mean_cells_per_spot = 5,
seed = 42,
verbose = FALSE
)The primary visualization shows the spatial distribution of assigned cell types:
For dense regions where points overlap, add jitter:
Since all plots are ggplot2 objects, you can easily customize them:
p <- plot_cytospace(results, type = "cell_types", colors = my_colors)
# Add custom theme
p +
theme_minimal() +
theme(
legend.position = "bottom",
legend.title = element_text(face = "bold"),
plot.title = element_text(hjust = 0.5, size = 16, face = "bold")
) +
guides(color = guide_legend(nrow = 1, override.aes = list(size = 4)))# Get the assigned locations data
locs <- results$assigned_locations
locs$Y_plot <- -locs$col
# Create faceted plot
ggplot(locs, aes(x = row, y = Y_plot)) +
geom_point(aes(color = CellType), size = 1.5, alpha = 0.8) +
facet_wrap(~ CellType, ncol = 3) +
scale_color_manual(values = my_colors) +
coord_equal() +
theme_minimal() +
theme(
axis.text = element_blank(),
axis.title = element_blank(),
axis.ticks = element_blank(),
panel.grid = element_blank(),
strip.text = element_text(face = "bold", size = 11),
legend.position = "none"
) +
labs(title = "Cell Type Distribution by Type")# Save individual plot
p <- plot_cytospace(results, type = "cell_types", colors = my_colors)
# Using CytoSPACER function
save_cytospace_plot(
plot = p,
output_dir = "figures/",
prefix = "spatial_",
formats = c("png", "pdf"),
width = 10,
height = 8,
dpi = 300
)
# Using ggplot2 directly
ggsave("figures/cell_types.png", p, width = 10, height = 8, dpi = 300)CytoSPACER includes a built-in color palette optimized for distinguishing cell types:
# Default CytoSPACER colors (first 10)
default_colors <- c(
"#222222", "#F3C300", "#875692", "#F38400", "#A1CAF1",
"#BE0032", "#C2B280", "#848482", "#008856", "#E68FAC"
)
# Display palette
barplot(rep(1, 10), col = default_colors, border = NA, axes = FALSE,
main = "CytoSPACER Default Color Palette", names.arg = 1:10)sessionInfo()
#> R version 4.6.1 (2026-06-24)
#> Platform: x86_64-pc-linux-gnu
#> Running under: Ubuntu 26.04 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.32.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: Etc/UTC
#> tzcode source: system (glibc)
#>
#> attached base packages:
#> [1] stats graphics grDevices utils datasets methods base
#>
#> other attached packages:
#> [1] ggplot2_4.0.3 future_1.70.0 Matrix_1.7-5 CytoSPACER_1.0.0
#> [5] rmarkdown_2.31
#>
#> loaded via a namespace (and not attached):
#> [1] gtable_0.3.6 future.apply_1.20.2 jsonlite_2.0.0
#> [4] dplyr_1.2.1 compiler_4.6.1 Rcpp_1.1.1-1.1
#> [7] tidyselect_1.2.1 parallel_4.6.1 jquerylib_0.1.4
#> [10] globals_0.19.1 scales_1.4.0 yaml_2.3.12
#> [13] fastmap_1.2.0 lattice_0.22-9 R6_2.6.1
#> [16] labeling_0.4.3 generics_0.1.4 knitr_1.51
#> [19] tibble_3.3.1 maketools_1.3.2 pillar_1.11.1
#> [22] bslib_0.11.0 RColorBrewer_1.1-3 rlang_1.2.0
#> [25] cachem_1.1.0 xfun_0.59 sass_0.4.10
#> [28] sys_3.4.3 S7_0.2.2 otel_0.2.0
#> [31] cli_3.6.6 withr_3.0.3 progressr_1.0.0
#> [34] magrittr_2.0.5 digest_0.6.39 grid_4.6.1
#> [37] lifecycle_1.0.5 vctrs_0.7.3 evaluate_1.0.5
#> [40] glue_1.8.1 data.table_1.18.4 farver_2.1.2
#> [43] listenv_1.0.0 codetools_0.2-20 buildtools_1.0.0
#> [46] parallelly_1.48.0 pkgconfig_2.0.3 tools_4.6.1
#> [49] htmltools_0.5.9