Skip to contents

In this tutorial, we’ll look at basic usage of the MapLabels() function for plotting features that represent discrete groups of spots. This could for example be manually selected spots or clusters. Such features as stored in the meta.data slot of our Seurat object and can be represented as either ‘character’ vectors of ‘factors’.

Load data

First we need to load some 10x Visium data. Here we’ll use a mouse brain tissue dataset and a mouse colon dataset that are shipped with semla.

# Load data
se_mbrain <- readRDS(file = system.file("extdata", 
                                        "mousebrain/se_mbrain", 
                                        package = "semla"))
se_mbrain$sample_id <- "mousebrain"
se_mcolon <- readRDS(file = system.file("extdata", 
                                        "mousecolon/se_mcolon", 
                                        package = "semla"))
se_mcolon$sample_id <- "mousecolon"
se <- MergeSTData(se_mbrain, se_mcolon)

Map categorical features

For categorical data, we use MapLabels() instead of MapFeatures(). This function allows us to color our spots based on some column of our Seurat object containing categorical data.

# Here we use the & operator from the patchwork R package to add a theme
# You can find more details in the 'advanced' tutorial
MapLabels(se, column_name = "sample_id", ncol = 1) &
  theme(legend.position = "right")

Let’s run unsupervised clustering on our data to get slightly more interesting results to work with.

NB: It doesn’t make much sense to run data-driven clustering on two completely different tissue types, but here we are only interested in demonstrating how you can use MapLabels.

se <- se |>
  NormalizeData() |>
  ScaleData() |>
  FindVariableFeatures() |>
  RunPCA() |>
  FindNeighbors(reduction = "pca", dims = 1:10) |>
  FindClusters(resolution = 0.2)
## Modularity Optimizer version 1.3.0 by Ludo Waltman and Nees Jan van Eck
## 
## Number of nodes: 5164
## Number of edges: 170850
## 
## Running Louvain algorithm...
## Maximum modularity in 10 random starts: 0.9210
## Number of communities: 5
## Elapsed time: 0 seconds

Map clusters

MapLabels(se, column_name = "seurat_clusters", ncol = 1) &
  theme(legend.position = "right")

We can also use the function MapLabelsSummary() if want to add a stacked bar plot next to the spatial plot, summarizing the percentage of spots for each cluster in the section. If you would rather view the actual spot count you can pass bar_display = "count" instead.

MapLabelsSummary(se, 
                 column_name = "seurat_clusters", 
                 ncol = 1, 
                 section_number = 1) &
  theme(legend.position = "none")

Overlay maps on images

And just as with MapFeatures, we can add our H&E images to the plots. Before we do this, we just need to load the H&E images into our Seurat object first with LoadImages.

se <- LoadImages(se, verbose = FALSE)

MapLabels(se, 
          column_name = "seurat_clusters", 
          image_use = "raw", 
          override_plot_dims = TRUE) +
  plot_layout(guides = "collect") &
  guides(fill = guide_legend(override.aes = list(size = 3), 
                             ncol = 2)) &
  theme(legend.position = "right")

Crop image

We can crop the images manually by defining a crop_area. The crop_area should be a vector of length four defining the corners of a rectangle, where the x- and y-axes are defined from 0-1.

In order to decide how this rectangle should be defined, you can get some help by adding a grid to the plot:

p <- MapLabels(se, 
               column_name = "seurat_clusters", 
               image_use = "raw", 
               pt_alpha = 0.5) &
  theme(panel.grid.major = element_line(linetype = "dashed"), axis.text = element_text())
p

Now if we want to crop out the GALT tissue in the mouse colon sample we can cut the image at left=0.45, bottom=0.55, right=0.65, top=0.7:

p <- MapLabels(se, 
               column_name = "selection", 
               image_use = "raw", 
               pt_size = 5, 
               section_number = 2, 
               crop_area = c(0.45, 0.55, 0.65, 0.7))
p

And we can patch together a nice figure showing the whole tissue and the zoom in of the GALT:

# override_plot_dims=TRUE can be used to crop the image to only
# include the region that contain spots (see 'advanced' tutorial)
p_global <- MapLabels(se, 
                      column_name = "selection", 
                      image_use = "raw",
                      pt_size = 1, 
                      section_number = 2, 
                      override_plot_dims = TRUE) &
  guides(fill = guide_legend(override.aes = list(size = 3)))

p_GALT <- MapLabels(se, 
                    column_name = "selection", 
                    image_use = "raw",  
                    pt_size = 5, 
                    section_number = 2, 
                    crop_area = c(0.45, 0.55, 0.65, 0.7)) &
  theme(plot.title = element_blank(), 
        plot.subtitle = element_blank(), 
        legend.position = "none")

(p_global / p_GALT)



Package version
  • semla: 1.1.6
Session info
## R version 4.2.3 (2023-03-15)
## Platform: x86_64-apple-darwin17.0 (64-bit)
## Running under: macOS Big Sur ... 10.16
## 
## Matrix products: default
## BLAS:   /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRlapack.dylib
## 
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
## 
## attached base packages:
## [1] stats     graphics  grDevices datasets  utils     methods   base     
## 
## other attached packages:
## [1] patchwork_1.1.3    tibble_3.2.1       semla_1.1.6        ggplot2_3.4.3     
## [5] dplyr_1.1.3        SeuratObject_4.1.3 Seurat_4.3.0.1    
## 
## loaded via a namespace (and not attached):
##   [1] Rtsne_0.16             colorspace_2.1-0       deldir_1.0-9          
##   [4] ellipsis_0.3.2         ggridges_0.5.4         rprojroot_2.0.3       
##   [7] fs_1.6.3               spatstat.data_3.0-1    farver_2.1.1          
##  [10] leiden_0.4.3           listenv_0.9.0          ggrepel_0.9.3         
##  [13] fansi_1.0.4            codetools_0.2-19       splines_4.2.3         
##  [16] cachem_1.0.8           knitr_1.43             zeallot_0.1.0         
##  [19] polyclip_1.10-4        jsonlite_1.8.7         ica_1.0-3             
##  [22] cluster_2.1.4          png_0.1-8              uwot_0.1.16           
##  [25] spatstat.sparse_3.0-2  shiny_1.7.5            sctransform_0.3.5     
##  [28] BiocManager_1.30.22    compiler_4.2.3         httr_1.4.7            
##  [31] Matrix_1.5-1           fastmap_1.1.1          lazyeval_0.2.2        
##  [34] cli_3.6.1              later_1.3.1            htmltools_0.5.6       
##  [37] tools_4.2.3            igraph_1.5.1           gtable_0.3.4          
##  [40] glue_1.6.2             RANN_2.6.1             reshape2_1.4.4        
##  [43] Rcpp_1.0.11            scattermore_1.2        jquerylib_0.1.4       
##  [46] pkgdown_2.0.7          vctrs_0.6.3            nlme_3.1-163          
##  [49] spatstat.explore_3.2-1 progressr_0.14.0       lmtest_0.9-40         
##  [52] spatstat.random_3.1-5  xfun_0.40              stringr_1.5.0         
##  [55] globals_0.16.2         mime_0.12              miniUI_0.1.1.1        
##  [58] lifecycle_1.0.3        irlba_2.3.5.1          renv_1.0.2            
##  [61] goftest_1.2-3          future_1.33.0          MASS_7.3-60           
##  [64] zoo_1.8-12             scales_1.2.1           spatstat.utils_3.0-3  
##  [67] ragg_1.2.5             promises_1.2.1         parallel_4.2.3        
##  [70] RColorBrewer_1.1-3     yaml_2.3.7             memoise_2.0.1         
##  [73] reticulate_1.31        pbapply_1.7-2          gridExtra_2.3         
##  [76] sass_0.4.7             stringi_1.7.12         highr_0.10            
##  [79] desc_1.4.2             rlang_1.1.1            pkgconfig_2.0.3       
##  [82] systemfonts_1.0.4      matrixStats_1.0.0      evaluate_0.21         
##  [85] lattice_0.21-8         tensor_1.5             ROCR_1.0-11           
##  [88] purrr_1.0.2            labeling_0.4.3         htmlwidgets_1.6.2     
##  [91] cowplot_1.1.1          tidyselect_1.2.0       parallelly_1.36.0     
##  [94] RcppAnnoy_0.0.21       plyr_1.8.8             magrittr_2.0.3        
##  [97] R6_2.5.1               magick_2.7.4           generics_0.1.3        
## [100] withr_2.5.0            pillar_1.9.0           fitdistrplus_1.1-11   
## [103] abind_1.4-5            survival_3.5-7         sp_2.0-0              
## [106] future.apply_1.11.0    KernSmooth_2.23-22     utf8_1.2.3            
## [109] spatstat.geom_3.2-4    plotly_4.10.2          rmarkdown_2.24        
## [112] grid_4.2.3             data.table_1.14.8      forcats_1.0.0         
## [115] digest_0.6.33          xtable_1.8-4           dbscan_1.1-11         
## [118] tidyr_1.3.0            httpuv_1.6.11          textshaping_0.3.6     
## [121] munsell_0.5.0          viridisLite_0.4.2      bslib_0.5.1           
## [124] shinyjs_2.1.0