Slingshot
(Street et al. 2018) is a popular and powerful trajectory inference tool, available from bioconductor. While it only provides default plotting functions in base R, the package also offer conversion functions that can be used to generate plots with ggplot2
(Wickham 2016).
We will assume that you are both familiar with slingshot
and ggplot2
. Otherwise, please refere to the respective packages’ vignettes.
We run slingshot
on the provided example
# Running slinghsot
suppressPackageStartupMessages(library(slingshot))
data("slingshotExample")
rd <- slingshotExample$rd
colnames(rd) <- c("Dim1", "Dim2")
cl <- slingshotExample$cl
df <- data.frame(rd, "cl" = as.character(cl))
sds <- slingshot(rd, cl)
We can then visualize the samples in reduced dimension.
suppressPackageStartupMessages(library(ggplot2))
suppressPackageStartupMessages(library(dplyr))
# Plotting the results
p <- ggplot(df, aes(x = Dim1, y = Dim2)) +
geom_point(aes(fill = cl), col = "grey70", shape = 21) +
theme_classic()
p
Then, we can add the curves. We can extract the needed information using the accessory functions. This can be turned into tidy data.frames that interact nicely with ggplot2
by setting the as.df
argument to TRUE
.
curves <- slingCurves(sds, as.df = TRUE)
p + geom_path(data = curves %>% arrange(Order),
aes(group = Lineage))
We can do all the usual ggplot2 operations
p + geom_path(data = curves %>% arrange(Order),
aes(group = Lineage), size = 1.5)
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
p + geom_path(data = curves %>% arrange(Order),
aes(group = Lineage, col = as.character(Lineage)), size = 1.5)
slingshot
relies on a minimum spanning tree to build the lineages. We can extract the information via:
mst <- slingMST(sds, as.df = TRUE)
We can then plot the nodes of the graph
p + geom_point(data = mst, size = 4)
And then add the edges
p + geom_point(data = mst, size = 4) +
geom_path(data = mst %>% arrange(Order), aes(group = Lineage), size = 2)
ggplot2
objects are very versatile. It is therefore quite easy to add the slingshot information to the output of a function that returns such an object. Here we demonstrate with the plotReducedDim
function from the scater
package(McCarthy et al. 2017).
suppressPackageStartupMessages(library(scater))
suppressPackageStartupMessages(library(SingleCellExperiment))
sce <- SingleCellExperiment(assays = list(counts = t(rd)),
colData = df)
reducedDim(sce, "Dim") <- rd
p <- plotReducedDim(sce, dimred = "Dim", colour_by = "cl")
p
Then, it is quite straightforward to add the curves. We just need to rename the components as X and Y
curves <- slingCurves(sds, as.df = TRUE) %>%
dplyr::rename("X" = "Dim1", "Y" = "Dim2")
p + geom_path(data = curves %>% arrange(Order),
aes(group = Lineage))
Similarly for the MST:
mst <- slingMST(sds, as.df = TRUE) %>%
dplyr::rename("X" = "Dim1", "Y" = "Dim2")
p + geom_point(data = mst, size = 4) +
geom_path(data = mst %>% arrange(Order), aes(group = Lineage), size = 2)
If you have trouble figuring out how to adapt this to other functions, feel free to open an issue on the traviz repo.
sessionInfo()
## R version 4.4.0 beta (2024-04-15 r86425)
## Platform: x86_64-pc-linux-gnu
## Running under: Ubuntu 22.04.4 LTS
##
## Matrix products: default
## BLAS: /home/biocbuild/bbs-3.19-bioc/R/lib/libRblas.so
## LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0
##
## locale:
## [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
## [3] LC_TIME=en_GB LC_COLLATE=C
## [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/New_York
## tzcode source: system (glibc)
##
## attached base packages:
## [1] stats4 stats graphics grDevices utils datasets methods
## [8] base
##
## other attached packages:
## [1] scater_1.32.0 scuttle_1.14.0
## [3] dplyr_1.1.4 ggplot2_3.5.1
## [5] slingshot_2.12.0 TrajectoryUtils_1.12.0
## [7] SingleCellExperiment_1.26.0 SummarizedExperiment_1.34.0
## [9] Biobase_2.64.0 GenomicRanges_1.56.0
## [11] GenomeInfoDb_1.40.0 IRanges_2.38.0
## [13] S4Vectors_0.42.0 BiocGenerics_0.50.0
## [15] MatrixGenerics_1.16.0 matrixStats_1.3.0
## [17] princurve_2.1.6 knitr_1.46
##
## loaded via a namespace (and not attached):
## [1] tidyselect_1.2.1 viridisLite_0.4.2
## [3] vipor_0.4.7 farver_2.1.1
## [5] viridis_0.6.5 fastmap_1.1.1
## [7] digest_0.6.35 rsvd_1.0.5
## [9] lifecycle_1.0.4 magrittr_2.0.3
## [11] compiler_4.4.0 rlang_1.1.3
## [13] sass_0.4.9 tools_4.4.0
## [15] igraph_2.0.3 utf8_1.2.4
## [17] yaml_2.3.8 S4Arrays_1.4.0
## [19] labeling_0.4.3 DelayedArray_0.30.0
## [21] abind_1.4-5 BiocParallel_1.38.0
## [23] withr_3.0.0 grid_4.4.0
## [25] fansi_1.0.6 beachmat_2.20.0
## [27] colorspace_2.1-0 scales_1.3.0
## [29] cli_3.6.2 rmarkdown_2.26
## [31] crayon_1.5.2 generics_0.1.3
## [33] httr_1.4.7 DelayedMatrixStats_1.26.0
## [35] ggbeeswarm_0.7.2 cachem_1.0.8
## [37] zlibbioc_1.50.0 parallel_4.4.0
## [39] XVector_0.44.0 vctrs_0.6.5
## [41] Matrix_1.7-0 jsonlite_1.8.8
## [43] BiocSingular_1.20.0 BiocNeighbors_1.22.0
## [45] ggrepel_0.9.5 irlba_2.3.5.1
## [47] beeswarm_0.4.0 jquerylib_0.1.4
## [49] glue_1.7.0 codetools_0.2-20
## [51] cowplot_1.1.3 gtable_0.3.5
## [53] UCSC.utils_1.0.0 ScaledMatrix_1.12.0
## [55] munsell_0.5.1 tibble_3.2.1
## [57] pillar_1.9.0 htmltools_0.5.8.1
## [59] GenomeInfoDbData_1.2.12 R6_2.5.1
## [61] sparseMatrixStats_1.16.0 evaluate_0.23
## [63] lattice_0.22-6 highr_0.10
## [65] bslib_0.7.0 Rcpp_1.0.12
## [67] gridExtra_2.3 SparseArray_1.4.0
## [69] xfun_0.43 pkgconfig_2.0.3
McCarthy, Davis J., Kieran R. Campbell, Aaron T. L. Lun, and Quin F. Willis. 2017. “Scater: Pre-Processing, Quality Control, Normalisation and Visualisation of Single-Cell RNA-Seq Data in R.” Bioinformatics 33 (8): 1179–86. https://doi.org/10.1093/bioinformatics/btw777.
Street, Kelly, Davide Risso, Russell B. Fletcher, Diya Das, John Ngai, Nir Yosef, Elizabeth Purdom, and Sandrine Dudoit. 2018. “Slingshot: cell lineage and pseudotime inference for single-cell transcriptomics.” BMC Genomics 19 (1): 477. https://doi.org/10.1186/s12864-018-4772-0.
Wickham, Hadley. 2016. Ggplot2: Elegant Graphics for Data Analysis. Springer-Verlag New York. https://ggplot2.tidyverse.org.