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)
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.2.1 (2022-06-23)
## Platform: aarch64-apple-darwin20 (64-bit)
## Running under: macOS Ventura 13.0
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRlapack.dylib
##
## locale:
## [1] C/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
##
## attached base packages:
## [1] stats4 stats graphics grDevices utils datasets methods
## [8] base
##
## other attached packages:
## [1] scater_1.26.0 scuttle_1.8.0
## [3] dplyr_1.0.9 ggplot2_3.3.6
## [5] slingshot_2.6.0 TrajectoryUtils_1.6.0
## [7] SingleCellExperiment_1.20.0 SummarizedExperiment_1.28.0
## [9] Biobase_2.58.0 GenomicRanges_1.50.1
## [11] GenomeInfoDb_1.34.2 IRanges_2.32.0
## [13] S4Vectors_0.36.0 BiocGenerics_0.44.0
## [15] MatrixGenerics_1.10.0 matrixStats_0.62.0
## [17] princurve_2.1.6 knitr_1.39
##
## loaded via a namespace (and not attached):
## [1] viridis_0.6.2 sass_0.4.1
## [3] BiocSingular_1.14.0 viridisLite_0.4.0
## [5] jsonlite_1.8.0 DelayedMatrixStats_1.19.0
## [7] bslib_0.3.1 assertthat_0.2.1
## [9] highr_0.9 vipor_0.4.5
## [11] GenomeInfoDbData_1.2.8 ggrepel_0.9.1
## [13] yaml_2.3.5 pillar_1.7.0
## [15] lattice_0.20-45 glue_1.6.2
## [17] beachmat_2.14.0 digest_0.6.29
## [19] XVector_0.38.0 colorspace_2.0-3
## [21] cowplot_1.1.1 htmltools_0.5.2
## [23] Matrix_1.4-1 pkgconfig_2.0.3
## [25] zlibbioc_1.44.0 purrr_0.3.4
## [27] scales_1.2.0 ScaledMatrix_1.6.0
## [29] BiocParallel_1.32.1 tibble_3.1.7
## [31] generics_0.1.3 farver_2.1.1
## [33] ellipsis_0.3.2 withr_2.5.0
## [35] cli_3.3.0 magrittr_2.0.3
## [37] crayon_1.5.1 evaluate_0.15
## [39] fansi_1.0.3 beeswarm_0.4.0
## [41] tools_4.2.1 lifecycle_1.0.1
## [43] stringr_1.4.0 munsell_0.5.0
## [45] DelayedArray_0.24.0 irlba_2.3.5
## [47] compiler_4.2.1 jquerylib_0.1.4
## [49] rsvd_1.0.5 rlang_1.0.4
## [51] grid_4.2.1 RCurl_1.98-1.7
## [53] BiocNeighbors_1.16.0 igraph_1.3.5
## [55] bitops_1.0-7 labeling_0.4.2
## [57] rmarkdown_2.14 gtable_0.3.0
## [59] codetools_0.2-18 DBI_1.1.3
## [61] R6_2.5.1 gridExtra_2.3
## [63] fastmap_1.1.0 utf8_1.2.2
## [65] stringi_1.7.8 ggbeeswarm_0.6.0
## [67] parallel_4.2.1 Rcpp_1.0.9
## [69] vctrs_0.4.1 tidyselect_1.1.2
## [71] xfun_0.31 sparseMatrixStats_1.10.0