# Using traviz

#### Koen Van den Berge, Hector Roux de Bézieux, Kelly Street

#### 03/06/2021

# Introduction

The `traviz` package was created to collect visualization functions related to trajectory inference. Asides from general purpose functions useful to any user, it also contains visualizations that can be used in a `slingshot`, `tradeSeq` or `condiments` workflow.

# Visualization of a trajectory

First, we will demonstrate functions to visualize a trajectory. Here, we’ll work with a trajectory as estimated by Slingshot (Street et al. 2018). An example trajectory is provided along with the `traviz` package.

Below, we show how one can use the `lines` and `plot` functions for a `SlingshotDataSet` object. We also show how the `plotGeneCount` function can be used for a quick visualization of the trajectory.

    library(slingshot)
    library(traviz)

    ##
    ## Attaching package: 'traviz'

    ## The following object is masked from 'package:scater':
    ##
    ##     plotExpression

    data(crv, package = "traviz")
    class(crv)

    ## [1] "SlingshotDataSet"
    ## attr(,"package")
    ## [1] "slingshot"

    rd <- slingReducedDim(crv)
    cl <- apply(slingClusterLabels(crv),1, function(x) which(x==1))

    ## Only visualize the trajectory
    plot(crv)

    ## Visualize the trajectory on top of cells in reduced space
    plot(rd, pch=16, col=cl+1, cex=2/3)
    lines(crv, col="black", lwd=3)

    ## Visualizing trajectory and clusters using plotGeneCount
    plotGeneCount(crv, clusters=cl)

### 3D visualization

The same functions (i.e., `plot` and `lines`) can also be used to visualize trajectories in 3D space using the `rgl` package. This can be done using the `plot3d` and `lines3d` functions, in similar vein as the 2D visualizations above.

# Visualization of gene expression along a trajectory

## Visualizing gene expression along with the trajectory in reduced space

The `plotGeneCount` also allows you to visualize the trajectory in reduced space, where each cell is colored according to its expression of the gene as defined by the `gene` argument.

    data(counts, package="traviz")
    plotGeneCount(crv, counts, gene = "Mpo")

## Visualizing gene expression along pseudotime

The `plotExpression` function may be used to visualize gene expression along pseudotime along with a quick smooth estimate of average gene expression. It uses a loess smoother to smooth the raw gene expression values as a function of pseudotime. Note that no between-cell normalization occurs to do the smoothing.

    plotExpression(counts=counts,
                   sds=crv,
                   gene="Car1")

If smoothers have been fitted using `tradeSeq`, then one can use the `plotSmoothers` function to plot the estimated smoothers. Note that here normalization does happen within `tradeSeq`, and the smoothers are estimated using a negative binomial count model.

    data(sce, package="traviz")

    plotSmoothers(models = sce, counts = counts, gene = "Elane")

# Feedback and suggestions

Any comment, question, or feedback is welcome and can be posted as an issue on the traviz repo.

# Session Info

    sessionInfo()

    ## R version 4.5.0 (2025-04-11 ucrt)
    ## Platform: x86_64-w64-mingw32/x64
    ## Running under: Windows Server 2022 x64 (build 20348)
    ##
    ## Matrix products: default
    ##   LAPACK version 3.12.1
    ##
    ## locale:
    ## [1] LC_COLLATE=C
    ## [2] LC_CTYPE=English_United States.utf8
    ## [3] LC_MONETARY=English_United States.utf8
    ## [4] LC_NUMERIC=C
    ## [5] LC_TIME=English_United States.utf8
    ##
    ## time zone: America/New_York
    ## tzcode source: internal
    ##
    ## attached base packages:
    ## [1] stats4    stats     graphics  grDevices utils     datasets  methods
    ## [8] base
    ##
    ## other attached packages:
    ##  [1] traviz_1.15.0               scater_1.37.0
    ##  [3] scuttle_1.19.0              dplyr_1.1.4
    ##  [5] ggplot2_3.5.2               slingshot_2.17.0
    ##  [7] TrajectoryUtils_1.17.0      SingleCellExperiment_1.31.0
    ##  [9] SummarizedExperiment_1.39.0 Biobase_2.69.0
    ## [11] GenomicRanges_1.61.0        GenomeInfoDb_1.45.4
    ## [13] IRanges_2.43.0              S4Vectors_0.47.0
    ## [15] BiocGenerics_0.55.0         generics_0.1.4
    ## [17] MatrixGenerics_1.21.0       matrixStats_1.5.0
    ## [19] princurve_2.1.6             knitr_1.50
    ##
    ## loaded via a namespace (and not attached):
    ##  [1] tidyselect_1.2.1          viridisLite_0.4.2
    ##  [3] vipor_0.4.7               farver_2.1.2
    ##  [5] viridis_0.6.5             fastmap_1.2.0
    ##  [7] digest_0.6.37             rsvd_1.0.5
    ##  [9] lifecycle_1.0.4           magrittr_2.0.3
    ## [11] compiler_4.5.0            rlang_1.1.6
    ## [13] sass_0.4.10               tools_4.5.0
    ## [15] igraph_2.1.4              yaml_2.3.10
    ## [17] S4Arrays_1.9.1            labeling_0.4.3
    ## [19] htmlwidgets_1.6.4         DelayedArray_0.35.1
    ## [21] RColorBrewer_1.1-3        abind_1.4-8
    ## [23] BiocParallel_1.43.3       withr_3.0.2
    ## [25] grid_4.5.0                rgl_1.3.18
    ## [27] beachmat_2.25.1           extrafontdb_1.0
    ## [29] scales_1.4.0              dichromat_2.0-0.1
    ## [31] cli_3.6.5                 rmarkdown_2.29
    ## [33] crayon_1.5.3              httr_1.4.7
    ## [35] DelayedMatrixStats_1.31.0 ggbeeswarm_0.7.2
    ## [37] cachem_1.1.0              splines_4.5.0
    ## [39] parallel_4.5.0            XVector_0.49.0
    ## [41] base64enc_0.1-3           vctrs_0.6.5
    ## [43] Matrix_1.7-3              jsonlite_2.0.0
    ## [45] BiocSingular_1.25.0       BiocNeighbors_2.3.1
    ## [47] ggrepel_0.9.6             irlba_2.3.5.1
    ## [49] beeswarm_0.4.0            jquerylib_0.1.4
    ## [51] glue_1.8.0                codetools_0.2-20
    ## [53] cowplot_1.1.3             gtable_0.3.6
    ## [55] UCSC.utils_1.5.0          extrafont_0.19
    ## [57] ScaledMatrix_1.17.0       tibble_3.2.1
    ## [59] pillar_1.10.2             htmltools_0.5.8.1
    ## [61] R6_2.6.1                  sparseMatrixStats_1.21.0
    ## [63] evaluate_1.0.3            lattice_0.22-7
    ## [65] bslib_0.9.0               Rcpp_1.0.14
    ## [67] Rttf2pt1_1.3.12           gridExtra_2.3
    ## [69] SparseArray_1.9.0         nlme_3.1-168
    ## [71] mgcv_1.9-3                xfun_0.52
    ## [73] pkgconfig_2.0.3

# References

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.