# trackViewer Vignette: change the track styles

#### Jianhong Ou, Lihua Julie Zhu

#### 16 April 2025

Abstract

Visualize mapped reads along with annotation as track layers for NGS dataset such as ChIP-seq, RNA-seq, miRNA-seq, DNA-seq, SNPs and methylation data.

- Plot methylation/mutation/variant data by lollipop plot

- Plot high dense methylation/mutation/variant data by dandelion plot

- Plot genomic interaction data (HiC, cooler, etc) by viewTracks

## Prepare toy data

First, we import the sample data.

    library(trackViewer)
    extdata <- system.file("extdata", package="trackViewer",
                           mustWork=TRUE)
    repA <- importScore(file.path(extdata, "cpsf160.repA_-.wig"),
                        file.path(extdata, "cpsf160.repA_+.wig"),
                        format="WIG")
    ## Because the wig file does not contain any strand info,
    ## we need to set it manually.
    strand(repA$dat) <- "-"
    strand(repA$dat2) <- "+"

    fox2 <- importScore(file.path(extdata, "fox2.bed"), format="BED",
                        ranges=GRanges("chr11", IRanges(122830799, 123116707)))
    dat <- coverageGR(fox2$dat)
    ## We can split the data by strand into two different track channels
    ## Here, we set the dat2 slot to save the negative strand info.

    fox2$dat <- dat[strand(dat)=="+"]
    fox2$dat2 <- dat[strand(dat)=="-"]

    library(TxDb.Hsapiens.UCSC.hg19.knownGene)
    library(org.Hs.eg.db)
    gr <- GRanges("chr11", IRanges(122929275, 122930122), strand="-")
    trs <- geneModelFromTxdb(TxDb.Hsapiens.UCSC.hg19.knownGene,
                             org.Hs.eg.db,
                             gr=gr)

    optSty <- optimizeStyle(trackList(repA, fox2, trs))
    trackList <- optSty$tracks
    viewerStyle <- optSty$style

## Using the `browseTracks` function as a helper

Since version 1.33.6, the interactive plot generated by `browseTracks` function will print the psuedocode about how to change the track styles at the bottom, which can be used as a quick reference.

    browseTracks(trackList, gr=gr)

## Adjust the x-axis or the X scale

In most cases, researchers are interested in the relative position of the peaks in the gene. Sometimes, margin needs to be adjusted to be able to show the entire gene model. The Figure below shows how to add an X scale (x-scale) and remove the x-axis using the setTrackXscaleParam and setTrackViewerStyleParam functions.

    setTrackViewerStyleParam(viewerStyle, "xaxis", FALSE)
    setTrackViewerStyleParam(viewerStyle, "margin", c(.01, .05, .01, .01))
    setTrackXscaleParam(trackList[[1]], "draw", TRUE)
    setTrackXscaleParam(trackList[[1]], "gp", list(cex=0.8))
    viewTracks(trackList, gr=gr, viewerStyle=viewerStyle)

plot data with x-scale

    setTrackXscaleParam(trackList[[1]], attr="position",
                        value=list(x=122929700, y=3, label=200))
    viewTracks(trackList, gr=gr, viewerStyle=viewerStyle)

plot data with x-scale

## Adjust the y-axis

The y-axis can be put to the right side of the track by setting the main slot to FALSE in the y-axis slot of each track. In addition, the limit of y-axis (ylim) can be set by setTrackStyleParam.

    setTrackViewerStyleParam(viewerStyle, "margin", c(.01, .05, .01, .05))
    for(i in 1:2){
        setTrackYaxisParam(trackList[[i]], "main", FALSE)
    }
    ## Adjust the limit of y-axis
    setTrackStyleParam(trackList[[1]], "ylim", c(0, 25))
    setTrackStyleParam(trackList[[2]], "ylim", c(-25, 0))
    ## The y-axis tick labels can be turn off/on by setting the 'label' to FALSE/TRUE
    #setTrackYaxisParam(trackList[[1]], 'label', TRUE)
    viewTracks(trackList, gr=gr, viewerStyle=viewerStyle)

plot data with y-axis in right side

The function `setTrackYaxisParam` can be sued to adjust the track color/size/etc of y-axis and the y-axis labels.

    ## change the
    setTrackYaxisParam(trackList[[1]], "gp", list(cex=.8, col="green"))
    setTrackYaxisParam(trackList[[2]], "gp", list(cex=.8, col="blue"))
    viewTracks(trackList, gr=gr, viewerStyle=viewerStyle)

plot data with adjusted track color

## Adjust the label of y-axis

The style of y-axis can be changed by setting the ylabgp slot in the style of each track.

    setTrackStyleParam(trackList[[1]], "ylabgp", list(cex=.8, col="green"))
    ## set cex to avoid automatic adjust
    setTrackStyleParam(trackList[[2]], "ylabgp", list(cex=.8, col="blue"))
    setTrackStyleParam(trackList[[2]], "marginBottom", .2)
    viewTracks(trackList, gr=gr, viewerStyle=viewerStyle)

plot data with adjusted color and size of y-axis label

The y-axis label can be put at the top or the bottom of each track.

    setTrackStyleParam(trackList[[1]], "ylabpos", "bottomleft")
    setTrackStyleParam(trackList[[2]], "ylabpos", "topright")
    setTrackStyleParam(trackList[[2]], "marginTop", .2)
    viewTracks(trackList, gr=gr, viewerStyle=viewerStyle)

plot data with adjusted y-axis label position

For each transcript, the transcript name can be put on the upstream or downstream of the transcript. The Y-lab can also be removed by set ‘ylabpos’ to ‘none’ or set the name as ’’.

    trackListN <- trackList
    setTrackStyleParam(trackListN[[3]], "ylabpos", "upstream")
    setTrackStyleParam(trackListN[[4]], "ylabpos", "downstream")
    ## set cex to avoid automatic adjust
    setTrackStyleParam(trackListN[[3]], "ylabgp", list(cex=.6))
    setTrackStyleParam(trackListN[[4]], "ylabgp", list(cex=.6))
    ## remove ylab
    setTrackStyleParam(trackListN[[5]], "ylabpos", "none")
    names(trackListN)[6] <- ""
    gr1 <- range(unname(unlist(GRangesList(sapply(trs, function(.ele) .ele$dat)))))
    start(gr1) <- start(gr1) - 2000
    end(gr1) <- end(gr1) + 2000
    viewTracks(trackListN, gr=gr1, viewerStyle=viewerStyle)

plot data with adjusted transcripts name position

## Adjust the track color

The track color can be changed by setting the color slot in the style of each track. The first color is for the dat slot of track and the second color is for the dat2 slot.

    setTrackStyleParam(trackList[[1]], "color", c("green", "black"))
    setTrackStyleParam(trackList[[2]], "color", c("black", "blue"))
    for(i in 3:length(trackList))
        setTrackStyleParam(trackList[[i]], "color", "black")
    viewTracks(trackList, gr=gr, viewerStyle=viewerStyle)

plot data with adjusted track color

## Adjust the track height

The track height can be changed by setting the height slot in the style of each track. However, the total height for all the tracks should be 1.

    trackListH <- trackList
    setTrackStyleParam(trackListH[[1]], "height", .1)
    setTrackStyleParam(trackListH[[2]], "height", .44)
    for(i in 3:length(trackListH)){
        setTrackStyleParam(trackListH[[i]], "height",
                           (1-(0.1+0.44))/(length(trackListH)-2))
    }
    viewTracks(trackListH, gr=gr, viewerStyle=viewerStyle)

plot data with adjusted track height

## Change the track names

The track names such as gene model names can be edited easily by changing the names of trackList.

    names(trackList) <- c("cpsf160", "fox2", rep("HSPA8", 5))
    viewTracks(trackList, gr=gr, viewerStyle=viewerStyle)

change the track names

## Show paired data in the same track

trackViewer can be used to show to-be-compared data in the same track side by side.

    cpsf160 <- importScore(file.path(extdata, "cpsf160.repA_-.wig"),
                           file.path(extdata, "cpsf160.repB_-.wig"),
                           format="WIG")
    strand(cpsf160$dat) <- strand(cpsf160$dat2) <- "-"
    setTrackStyleParam(cpsf160, "color", c("black", "red"))
    viewTracks(trackList(trs, cpsf160), gr=gr, viewerStyle=viewerStyle)

show two data in the same track

## Show signals and the called peaks

The called peaks can be plotted as annotation.

    cpsf160_cp <- cpsf160
    cpsf160$dat2 <- reduce(cpsf160$dat)
    cpsf160$dat2$score <- sample.int(length(cpsf160$dat2))
    setTrackStyleParam(cpsf160, "tracktype", c("peak", "annotation"))
    cpsf160_cp$dat <- reduce(cpsf160_cp$dat2)
    cpsf160_cp$dat$score <- sample.int(length(cpsf160_cp$dat))
    setTrackStyleParam(cpsf160_cp, "tracktype", c("annotation", "peak"))
    setTrackStyleParam(cpsf160_cp, "marginTop", 0.05)
    viewTracks(trackList(trs, cpsf160, cpsf160_cp), gr=gr, viewerStyle=viewerStyle)

show called peaks in track

## Flip the x-axis

The x-axis can be horizotally flipped for the genes in the negative strand.

    viewerStyleF <- viewerStyle
    setTrackViewerStyleParam(viewerStyleF, "flip", TRUE)
    setTrackViewerStyleParam(viewerStyleF, "xaxis", TRUE)
    setTrackViewerStyleParam(viewerStyleF, "margin", c(.1, .05, .01, .01))
    vp <- viewTracks(trackList, gr=gr, viewerStyle=viewerStyleF)
    addGuideLine(c(122929767, 122929969), vp=vp)
    addArrowMark(list(x=122929650,
                      y=2),
                 label="label",
                 col="blue",
                 vp=vp)

show data in the flipped track

## Optimize the theme

Currently, we support two themes: bw (black and white) and col (colored).

    optSty <- optimizeStyle(trackList(repA, fox2, trs), theme="bw")
    trackList <- optSty$tracks
    viewerStyle <- optSty$style
    vp <- viewTracks(trackList, gr=gr, viewerStyle=viewerStyle)

balck & white theme

    optSty <- optimizeStyle(trackList(repA, fox2, trs), theme="col")
    trackList <- optSty$tracks
    viewerStyle <- optSty$style
    vp <- viewTracks(trackList, gr=gr, viewerStyle=viewerStyle)

colorful theme

    optSty <- optimizeStyle(trackList(repA, fox2, trs), theme="safe")
    trackList <- optSty$tracks
    viewerStyle <- optSty$style
    vp <- viewTracks(trackList, gr=gr, viewerStyle=viewerStyle)

safe theme

## Plot with breaks

We could plot the tracks with breaks by setting multiple genomic ranges.

    gr.breaks <- GRanges("chr11",
                         IRanges(c(122929275, 122929575, 122929775),
                                 c(122929555, 122929725, 122930122)),
                         strand="-", percentage=c(.4, .2, .4))
    vp <- viewTracks(trackList, gr=gr.breaks, viewerStyle=viewerStyle)

axis with breaks

## Session Info

    sessionInfo()

R version 4.5.0 RC (2025-04-04 r88126 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] grid stats4 stats graphics grDevices utils datasets [8] methods base

other attached packages: [1] httr_1.4.7
[2] VariantAnnotation_1.54.0
[3] Rsamtools_2.24.0
[4] Biostrings_2.76.0
[5] XVector_0.48.0
[6] SummarizedExperiment_1.38.0
[7] MatrixGenerics_1.20.0
[8] matrixStats_1.5.0
[9] org.Hs.eg.db_3.21.0
[10] TxDb.Hsapiens.UCSC.hg19.knownGene_3.2.2 [11] GenomicFeatures_1.60.0
[12] AnnotationDbi_1.70.0
[13] Biobase_2.68.0
[14] Gviz_1.52.0
[15] rtracklayer_1.68.0
[16] trackViewer_1.44.0
[17] GenomicRanges_1.60.0
[18] GenomeInfoDb_1.44.0
[19] IRanges_2.42.0
[20] S4Vectors_0.46.0
[21] BiocGenerics_0.54.0
[22] generics_0.1.3

loaded via a namespace (and not attached): [1] RColorBrewer_1.1-3 strawr_0.0.92 rstudioapi_0.17.1
[4] jsonlite_2.0.0 magrittr_2.0.3 rmarkdown_2.29
[7] BiocIO_1.18.0 vctrs_0.6.5 memoise_2.0.1
[10] RCurl_1.98-1.17 base64enc_0.1-3 htmltools_0.5.8.1
[13] S4Arrays_1.8.0 progress_1.2.3 curl_6.2.2
[16] Rhdf5lib_1.30.0 SparseArray_1.8.0 Formula_1.2-5
[19] rhdf5_2.52.0 sass_0.4.10 bslib_0.9.0
[22] htmlwidgets_1.6.4 httr2_1.1.2 cachem_1.1.0
[25] GenomicAlignments_1.44.0 lifecycle_1.0.4 pkgconfig_2.0.3
[28] Matrix_1.7-3 R6_2.6.1 fastmap_1.2.0
[31] GenomeInfoDbData_1.2.14 digest_0.6.37 colorspace_2.1-1
[34] Hmisc_5.2-3 RSQLite_2.3.9 filelock_1.0.3
[37] abind_1.4-8 compiler_4.5.0 bit64_4.6.0-1
[40] htmlTable_2.4.3 backports_1.5.0 BiocParallel_1.42.0
[43] DBI_1.2.3 biomaRt_2.64.0 rappdirs_0.3.3
[46] DelayedArray_0.34.0 rjson_0.2.23 tools_4.5.0
[49] foreign_0.8-90 nnet_7.3-20 glue_1.8.0
[52] restfulr_0.0.15 InteractionSet_1.36.0 rhdf5filters_1.20.0
[55] checkmate_2.3.2 cluster_2.1.8.1 gtable_0.3.6
[58] BSgenome_1.76.0 ensembldb_2.32.0 data.table_1.17.0
[61] hms_1.1.3 xml2_1.3.8 pillar_1.10.2
[64] stringr_1.5.1 dplyr_1.1.4 BiocFileCache_2.16.0
[67] lattice_0.22-7 bit_4.6.0 deldir_2.0-4
[70] biovizBase_1.56.0 tidyselect_1.2.1 knitr_1.50
[73] gridExtra_2.3 ProtGenerics_1.40.0 xfun_0.52
[76] stringi_1.8.7 UCSC.utils_1.4.0 lazyeval_0.2.2
[79] yaml_2.3.10 evaluate_1.0.3 codetools_0.2-20
[82] interp_1.1-6 tibble_3.2.1 BiocManager_1.30.25
[85] cli_3.6.4 rpart_4.1.24 munsell_0.5.1
[88] jquerylib_0.1.4 dichromat_2.0-0.1 Rcpp_1.0.14
[91] grImport_0.9-7 dbplyr_2.5.0 png_0.1-8
[94] XML_3.99-0.18 parallel_4.5.0 ggplot2_3.5.2
[97] blob_1.2.4 prettyunits_1.2.0 latticeExtra_0.6-30
[100] jpeg_0.1-11 AnnotationFilter_1.32.0 bitops_1.0-9
[103] txdbmaker_1.4.0 scales_1.3.0 crayon_1.5.3
[106] BiocStyle_2.36.0 rlang_1.1.6 KEGGREST_1.48.0