LieberInstitute / spatialLIBD

Code for the spatialLIBD R/Bioconductor package and shiny app
http://LieberInstitute.github.io/spatialLIBD/
78 stars 16 forks source link

[BUG] `sort_clusters` creates arbitary numeric seqences as legend labels in `vis_grid_clus` plots. #77

Closed boyiguo1 closed 1 month ago

boyiguo1 commented 4 months ago

Thanks for the nice package!

I've been using vis_grid_clus to plot multiple samples together. I noticed the legends representing levels of clustervar are always turning to numbers instead of showing their original labels even when I manually set up with colors. With some debugging, I realize the function sort_clusters might be the culprit for it.

library(spatialLIBD)
spe <- fetch_data("spatialDLPFC_Visium_example_subset")
spe$spd <- sprintf("SpD_%02d", spe$BayesSpace_harmony_10)
table(spe$spd)
# SpD_01 SpD_02 SpD_03 SpD_04 SpD_05 SpD_06 SpD_07 SpD_08 SpD_09 SpD_10 
#    139    755   1932   2137   1882    688   1805   1511    591    667 
sort_clusters(spe$spd) |> table()
#    1    2    3    4    5    6    7    8    9   10 
# 2137 1932 1882 1805 1511  755  688  667  591  139 

As an end results for vis_grid_clus plots, I'm getting plots with no color filled spots and warning messages 1: No shared levels found betweennames(values)of the manual scale and the data's fill values. when running the following line.

k <- unique(spe$spd) |> length()
vis_grid_clus(
  spe,
  "spd",
  return_plots = TRUE,
  colors = set_names(
    Polychrome::palette36.colors(k),
    unique(spe$spd) |> sort()
  )
)

This is because sort_clusters is employed with the default option of sort_clust = TRUE. https://github.com/LieberInstitute/spatialLIBD/blob/77a5303f91edb7b9ffb1ce00b4193dae5d16a8a1/R/vis_grid_clus.R#L69-L72

The problem is metigated by making sort_clust = FALSE as well as providing colors option.

vis_grid_clus(
  spe,
  "spd",
  return_plots = TRUE,
  sort_clust = FALSE,
  colors = set_names(
    Polychrome::palette36.colors(k),
    unique(spe$spd) |> sort()
  )
)

R Session Information

```R ─ Session info ──────────────────────────────────────────────────────────────────────────────── setting value version R version 4.3.2 (2023-10-31) os macOS Sonoma 14.2.1 system aarch64, darwin20 ui X11 language (EN) collate en_US.UTF-8 ctype en_US.UTF-8 tz America/New_York date 2024-04-23 pandoc 3.1.12.1 @ /opt/homebrew/bin/pandoc ─ Packages ──────────────────────────────────────────────────────────────────────────────────── package * version date (UTC) lib source abind 1.4-5 2016-07-21 [1] CRAN (R 4.3.0) AnnotationDbi 1.64.1 2023-11-02 [1] Bioconductor AnnotationHub 3.10.0 2023-10-26 [1] Bioconductor attempt 0.3.1 2020-05-03 [1] CRAN (R 4.3.0) beachmat 2.18.1 2024-02-17 [1] Bioconductor 3.18 (R 4.3.2) beeswarm 0.4.0 2021-06-01 [1] CRAN (R 4.3.0) benchmarkme 1.0.8 2022-06-12 [1] CRAN (R 4.3.0) benchmarkmeData 1.0.4 2020-04-23 [1] CRAN (R 4.3.0) Biobase * 2.62.0 2023-10-26 [1] Bioconductor BiocFileCache 2.10.1 2023-10-26 [1] Bioconductor BiocGenerics * 0.48.1 2023-11-02 [1] Bioconductor BiocIO 1.12.0 2023-10-26 [1] Bioconductor BiocManager 1.30.22 2023-08-08 [1] CRAN (R 4.3.0) BiocNeighbors 1.20.2 2024-01-13 [1] Bioconductor 3.18 (R 4.3.2) BiocParallel 1.36.0 2023-10-26 [1] Bioconductor BiocSingular 1.18.0 2023-10-24 [1] Bioconductor BiocVersion 3.18.1 2023-11-18 [1] Bioconductor 3.18 (R 4.3.2) Biostrings 2.70.1 2023-10-26 [1] Bioconductor bit 4.0.5 2022-11-15 [1] CRAN (R 4.3.0) bit64 4.0.5 2020-08-30 [1] CRAN (R 4.3.0) bitops 1.0-7 2021-04-24 [1] CRAN (R 4.3.0) blob 1.2.4 2023-03-17 [1] CRAN (R 4.3.0) bslib 0.7.0 2024-03-29 [1] CRAN (R 4.3.1) cachem 1.0.8 2023-05-01 [1] CRAN (R 4.3.0) cli 3.6.2 2023-12-11 [1] CRAN (R 4.3.1) codetools 0.2-19 2023-02-01 [1] CRAN (R 4.3.2) colorspace 2.1-0 2023-01-23 [1] CRAN (R 4.3.0) config 0.3.2 2023-08-30 [1] CRAN (R 4.3.0) cowplot 1.1.2 2023-12-15 [1] CRAN (R 4.3.1) crayon 1.5.2 2022-09-29 [1] CRAN (R 4.3.0) curl 5.2.0 2023-12-08 [1] CRAN (R 4.3.1) data.table 1.14.10 2023-12-08 [1] CRAN (R 4.3.1) DBI 1.2.0 2023-12-21 [1] CRAN (R 4.3.1) dbplyr 2.4.0 2023-10-26 [1] CRAN (R 4.3.1) DelayedArray 0.28.0 2023-10-24 [1] Bioconductor DelayedMatrixStats 1.24.0 2023-10-24 [1] Bioconductor digest 0.6.35 2024-03-11 [1] CRAN (R 4.3.1) doParallel 1.0.17 2022-02-07 [1] CRAN (R 4.3.0) dotCall64 1.1-1 2023-11-28 [1] CRAN (R 4.3.1) dplyr * 1.1.4 2023-11-17 [1] CRAN (R 4.3.1) DT 0.31 2023-12-09 [1] CRAN (R 4.3.1) edgeR 4.0.16 2024-02-20 [1] Bioconductor 3.18 (R 4.3.2) ellipsis 0.3.2 2021-04-29 [1] CRAN (R 4.3.0) ExperimentHub 2.10.0 2023-10-26 [1] Bioconductor fansi 1.0.6 2023-12-08 [1] CRAN (R 4.3.1) fastmap 1.1.1 2023-02-24 [1] CRAN (R 4.3.0) fields 15.2 2023-08-17 [1] CRAN (R 4.3.0) filelock 1.0.3 2023-12-11 [1] CRAN (R 4.3.1) forcats * 1.0.0 2023-01-29 [1] CRAN (R 4.3.0) foreach 1.5.2 2022-02-02 [1] CRAN (R 4.3.0) generics 0.1.3 2022-07-05 [1] CRAN (R 4.3.0) GenomeInfoDb * 1.38.8 2024-03-16 [1] Bioconductor 3.18 (R 4.3.3) GenomeInfoDbData 1.2.11 2023-11-08 [1] Bioconductor GenomicAlignments 1.38.0 2023-10-26 [1] Bioconductor GenomicRanges * 1.54.1 2023-10-30 [1] Bioconductor ggbeeswarm 0.7.2 2023-04-29 [1] CRAN (R 4.3.0) ggplot2 * 3.5.0 2024-02-23 [1] CRAN (R 4.3.1) ggrepel 0.9.5 2024-01-10 [1] CRAN (R 4.3.1) glue 1.7.0 2024-01-09 [1] CRAN (R 4.3.1) golem 0.4.1 2023-06-05 [1] CRAN (R 4.3.0) gridExtra 2.3 2017-09-09 [1] CRAN (R 4.3.0) gtable 0.3.4 2023-08-21 [1] CRAN (R 4.3.0) hms 1.1.3 2023-03-21 [1] CRAN (R 4.3.0) htmltools 0.5.8.1 2024-04-04 [1] CRAN (R 4.3.1) htmlwidgets 1.6.4 2023-12-06 [1] CRAN (R 4.3.1) httpuv 1.6.13 2023-12-06 [1] CRAN (R 4.3.1) httr 1.4.7 2023-08-15 [1] CRAN (R 4.3.0) interactiveDisplayBase 1.40.0 2023-10-26 [1] Bioconductor IRanges * 2.36.0 2023-10-26 [1] Bioconductor irlba 2.3.5.1 2022-10-03 [1] CRAN (R 4.3.2) iterators 1.0.14 2022-02-05 [1] CRAN (R 4.3.0) jquerylib 0.1.4 2021-04-26 [1] CRAN (R 4.3.0) jsonlite 1.8.8 2023-12-04 [1] CRAN (R 4.3.1) KEGGREST 1.42.0 2023-10-26 [1] Bioconductor later 1.3.2 2023-12-06 [1] CRAN (R 4.3.1) lattice 0.22-5 2023-10-24 [1] CRAN (R 4.3.1) lazyeval 0.2.2 2019-03-15 [1] CRAN (R 4.3.0) lifecycle 1.0.4 2023-11-07 [1] CRAN (R 4.3.0) limma 3.58.1 2023-11-02 [1] Bioconductor locfit 1.5-9.9 2024-03-01 [1] CRAN (R 4.3.1) lubridate * 1.9.3 2023-09-27 [1] CRAN (R 4.3.1) magick 2.8.2 2023-12-20 [1] CRAN (R 4.3.1) magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.3.0) maps 3.4.2 2023-12-15 [1] CRAN (R 4.3.1) Matrix 1.6-5 2024-01-11 [1] CRAN (R 4.3.2) MatrixGenerics * 1.14.0 2023-10-26 [1] Bioconductor matrixStats * 1.2.0 2023-12-11 [1] CRAN (R 4.3.1) memoise 2.0.1 2021-11-26 [1] CRAN (R 4.3.0) mime 0.12 2021-09-28 [1] CRAN (R 4.3.0) munsell 0.5.1 2024-04-01 [1] CRAN (R 4.3.1) paletteer 1.5.0 2022-10-19 [1] CRAN (R 4.3.0) pillar 1.9.0 2023-03-22 [1] CRAN (R 4.3.0) pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.3.0) plotly 4.10.3 2023-10-21 [1] CRAN (R 4.3.1) png 0.1-8 2022-11-29 [1] CRAN (R 4.3.0) Polychrome * 1.5.1 2022-05-03 [1] CRAN (R 4.3.0) promises 1.2.1 2023-08-10 [1] CRAN (R 4.3.0) purrr * 1.0.2 2023-08-10 [1] CRAN (R 4.3.0) R6 2.5.1 2021-08-19 [1] CRAN (R 4.3.0) rappdirs 0.3.3 2021-01-31 [1] CRAN (R 4.3.0) RColorBrewer 1.1-3 2022-04-03 [1] CRAN (R 4.3.0) Rcpp 1.0.12 2024-01-09 [1] CRAN (R 4.3.1) RCurl 1.98-1.14 2024-01-09 [1] CRAN (R 4.3.1) readr * 2.1.4 2023-02-10 [1] CRAN (R 4.3.0) rematch2 2.1.2 2020-05-01 [1] CRAN (R 4.3.0) restfulr 0.0.15 2022-06-16 [1] CRAN (R 4.3.0) rjson 0.2.21 2022-01-09 [1] CRAN (R 4.3.0) rlang 1.1.3 2024-01-10 [1] CRAN (R 4.3.1) Rsamtools 2.18.0 2023-10-26 [1] Bioconductor RSQLite 2.3.4 2023-12-08 [1] CRAN (R 4.3.1) rstudioapi 0.16.0 2024-03-24 [1] CRAN (R 4.3.1) rsvd 1.0.5 2021-04-16 [1] CRAN (R 4.3.0) rtracklayer 1.62.0 2023-10-26 [1] Bioconductor S4Arrays 1.2.1 2024-03-05 [1] Bioconductor 3.18 (R 4.3.2) S4Vectors * 0.40.2 2023-11-25 [1] Bioconductor 3.18 (R 4.3.2) sass 0.4.9 2024-03-15 [1] CRAN (R 4.3.1) ScaledMatrix 1.10.0 2023-10-24 [1] Bioconductor scales 1.3.0 2023-11-28 [1] CRAN (R 4.3.1) scater 1.30.1 2023-11-16 [1] Bioconductor scatterplot3d 0.3-44 2023-05-05 [1] CRAN (R 4.3.0) scuttle 1.12.0 2023-10-24 [1] Bioconductor sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.3.0) shiny 1.8.0 2023-11-17 [1] CRAN (R 4.3.1) shinyWidgets 0.8.0 2023-08-30 [1] CRAN (R 4.3.0) SingleCellExperiment * 1.24.0 2023-10-24 [1] Bioconductor spam 2.10-0 2023-10-23 [1] CRAN (R 4.3.1) SparseArray 1.2.4 2024-02-10 [1] Bioconductor 3.18 (R 4.3.2) sparseMatrixStats 1.14.0 2023-10-26 [1] Bioconductor SpatialExperiment * 1.12.0 2023-10-26 [1] Bioconductor spatialLIBD * 1.14.1 2023-11-30 [1] Bioconductor 3.18 (R 4.3.0) statmod 1.5.0 2023-01-06 [1] CRAN (R 4.3.0) stringi 1.8.3 2023-12-11 [1] CRAN (R 4.3.1) stringr * 1.5.1 2023-11-14 [1] CRAN (R 4.3.1) SummarizedExperiment * 1.32.0 2023-10-24 [1] Bioconductor tibble * 3.2.1 2023-03-20 [1] CRAN (R 4.3.0) tidyr * 1.3.0 2023-01-24 [1] CRAN (R 4.3.0) tidyselect 1.2.0 2022-10-10 [1] CRAN (R 4.3.0) tidyverse * 2.0.0 2023-02-22 [1] CRAN (R 4.3.0) timechange 0.2.0 2023-01-11 [1] CRAN (R 4.3.0) tzdb 0.4.0 2023-05-12 [1] CRAN (R 4.3.0) utf8 1.2.4 2023-10-22 [1] CRAN (R 4.3.1) vctrs 0.6.5 2023-12-01 [1] CRAN (R 4.3.1) vipor 0.4.7 2023-12-18 [1] CRAN (R 4.3.1) viridis 0.6.5 2024-01-29 [1] CRAN (R 4.3.1) viridisLite 0.4.2 2023-05-02 [1] CRAN (R 4.3.0) withr 3.0.0 2024-01-16 [1] CRAN (R 4.3.1) XML 3.99-0.16 2023-11-29 [1] CRAN (R 4.3.1) xtable 1.8-4 2019-04-21 [1] CRAN (R 4.3.0) XVector 0.42.0 2023-10-26 [1] Bioconductor yaml 2.3.8 2023-12-11 [1] CRAN (R 4.3.1) zlibbioc 1.48.2 2024-03-19 [1] Bioconductor 3.18 (R 4.3.3) [1] /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library ─────────────────────────────────────────────────────────────────────────────────────────────── ```
lcolladotor commented 1 month ago

Hi @boyiguo1,

This has been now resolved in https://github.com/LieberInstitute/spatialLIBD/commit/08c2b2d80a29b1decee578d01f7230b595a9bf5a which also resolved #80.

Best, Leo


Here's the output of your example with version 1.17.6:

suppressPackageStartupMessages(library("spatialLIBD"))
spe <- fetch_data("spatialDLPFC_Visium_example_subset")
#> 2024-07-12 13:23:57.969 loading file /Users/leocollado/Library/Caches/org.R-project.R/R/BiocFileCache/6f563a40ecf7_spatialDLPFC_spe_subset_example.rds%3Fdl%3D1
spe$spd <- sprintf("SpD_%02d", spe$BayesSpace_harmony_10)
vis_grid_clus(
  spe,
  "spd",
  return_plots = TRUE,
  sort_clust = TRUE
)
#> $Br6432_ant

#> 
#> $Br6522_ant

#> 
#> $Br8667_mid


k <- unique(spe$spd) |> length()
vis_grid_clus(
  spe,
  "spd",
  return_plots = TRUE,
  sort_clust = FALSE,
  colors = setNames(
    Polychrome::palette36.colors(k),
    unique(spe$spd) |> sort()
  )
)
#> $Br6432_ant

#> 
#> $Br6522_ant

#> 
#> $Br8667_mid

packageVersion("spatialLIBD")
#> [1] '1.17.6'

Created on 2024-07-12 with reprex v2.1.0