UPDATE:
this issue was encountered in Gviz_1.14.7
and has been solved in (at least) Gviz_1.16.5.
I am leaving it here in case someone using an older version comes across it.
I am trying to plot several data tracks (coverage), and to make it easier to identify the factors represented in each track I would like to label each on top. For that I used customTracks
So, going from this:
Example 1
library("Gviz") bamFile1 <- system.file("extdata/GSM461176_S2_DRSC_Untreated_1_bmm.bam", package = "Gviz") dTrack2 <- DataTrack(range = bamFile1, genome = "BDGP5", type = "l", chromosome = "chr3L", name = "Untreated") bamFile2 <- system.file("extdata/GSM461179_S2_DRSC_CG8144_RNAi_1_bmm.bam", package = "Gviz") dTrack4 <- DataTrack(range = bamFile2, genome = "BDGP5", type = "l", chromosome = "chr3L", name = "RNAi") plotTracks(list(dTrack4, dTrack2), from = 14764960, to = 14785000)
to this:
Example 2
dTrack2 <- DataTrack(range = bamFile1, genome = "BDGP5", type = "l", chromosome = "chr3L", name = "Reads per million \n(RPM)") dTrack4 <- DataTrack(range = bamFile2, genome = "BDGP5", type = "l", chromosome = "chr3L", name = "Reads per million \n(RPM)") cTrack1 <- CustomTrack(plottingFunction = function(GdObject, prepare) {if(!prepare) grid.text("Untreated"); return(invisible(GdObject))}) cTrack2 <- CustomTrack(plottingFunction = function(GdObject, prepare) {if(!prepare) grid.text("RNAi"); return(invisible(GdObject))}) plotTracks(list(cTrack1, dTrack4, cTrack2, dTrack2), from = 14764960, to = 14785000)
The issue that I have encountered is that both custom tracks plot exactly the same text, which I was not expecting. Is this a bug, or am I going something wrong?
Also is there a better way of achieving my goal, that is plotting text either on top or overlayed with another track? This would actually be very helpful in many situations where is more informative to have a "track title" inside the track, rather than on the Y-axis which then describes the measurement units only (example 2).
Cheers
sessionInfo() R version 3.2.2 (2015-08-14) Platform: x86_64-pc-linux-gnu (64-bit) Running under: Debian GNU/Linux 7 (wheezy) locale: [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C [3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8 [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 attached base packages: [1] grid parallel stats4 stats graphics grDevices utils [8] datasets methods base other attached packages: [1] Gviz_1.14.7 GenomicRanges_1.22.4 GenomeInfoDb_1.6.3 [4] IRanges_2.4.8 S4Vectors_0.8.11 BiocGenerics_0.16.1 loaded via a namespace (and not attached): [1] Rcpp_0.12.7 RColorBrewer_1.1-2 [3] futile.logger_1.4.3 plyr_1.8.4 [5] XVector_0.10.0 GenomicFeatures_1.22.13 [7] bitops_1.0-6 futile.options_1.0.0 [9] tools_3.2.2 zlibbioc_1.16.0 [11] rpart_4.1-10 biomaRt_2.29.2 [13] digest_0.6.10 BSgenome_1.38.0 [15] gtable_0.2.0 RSQLite_1.0.0 [17] lattice_0.20-34 biovizBase_1.18.0 [19] Matrix_1.2-7.1 DBI_0.5-1 [21] gridExtra_2.2.1 cluster_2.0.5 [23] rtracklayer_1.30.4 Biostrings_2.38.4 [25] nnet_7.3-12 data.table_1.9.6 [27] Biobase_2.30.0 AnnotationDbi_1.32.3 [29] XML_3.98-1.4 survival_2.39-5 [31] BiocParallel_1.4.3 foreign_0.8-67 [33] Formula_1.2-1 latticeExtra_0.6-28 [35] ggplot2_2.1.0 lambda.r_1.1.9 [37] Rsamtools_1.22.0 scales_0.4.0 [39] Hmisc_3.17-4 matrixStats_0.51.0 [41] GenomicAlignments_1.6.3 splines_3.2.2 [43] dichromat_2.0-0 SummarizedExperiment_1.0.2 [45] colorspace_1.2-7 acepack_1.4.0 [47] RCurl_1.95-4.8 munsell_0.4.3 [49] VariantAnnotation_1.16.4 chron_2.3-47
Actually, I did find something strange about custom tracks: when constructing them in a loop, it always takes the last element in the loop:
Am I doing something wrong?
The answer when you
rm(i)
beforeplotTracks()
Actually you were almost there ;-) Make use of
CustomTrack
class'variables
slot:Yet another option, closer to your original code but without making use of the for loop:
ctracks <- lapply(1:2, function(i) { CustomTrack(plottingFunction=function(GdObject, prepare, ...) { if(!prepare) grid.text(paste(i)) return(invisible(GdObject)) }) }) plotTracks(ctracks, from=14764960, to=14785000)
cheers