Cannot use color/ fill arguments outside of aes() in ggbio karyogram layout
2
1
Entering edit mode
Mario Saare ▴ 20
@mario-saare-6739
Last seen 15 months ago
Estonia

Dear ggbio developers,

I would like to use the karyogram layout in the ggbio R package to visualize some genome-wide patterns, but it seems that it is not possible to control the colors, if I use "fill" or "color" arguments outside of aes() function, although the vignette shows that this option is available. I have tried it with the data from the vignette and with my own (there are no error messages). How can I overcome this problem?

Here is a demo with the data in your vignette:

library(ggbio)
#Test the karyogram plot.
data(ideoCyto, package = "biovizBase")
ideoCyto
#Arguments 'fill' and 'color' work within aes() function.
autoplot(ideoCyto$hg19, layout='karyogram', aes(fill=gieStain))
#All chromosomes should be green, but arguments 'fill' and 'color' do not work.
autoplot(ideoCyto$hg19, layout='karyogram', fill="green")

Best regards,

Mario

> sessionInfo()
R version 3.2.3 (2015-12-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=Estonian_Estonia.1257  LC_CTYPE=Estonian_Estonia.1257    LC_MONETARY=Estonian_Estonia.1257
[4] LC_NUMERIC=C                      LC_TIME=Estonian_Estonia.1257    

attached base packages:
[1] parallel  stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggbio_1.18.5        ggplot2_2.1.0       BiocGenerics_0.16.1

loaded via a namespace (and not attached):
 [1] SummarizedExperiment_1.0.2 VariantAnnotation_1.16.4   reshape2_1.4.1             splines_3.2.3             
 [5] lattice_0.20-33            colorspace_1.2-6           stats4_3.2.3               rtracklayer_1.30.2        
 [9] GenomicFeatures_1.22.13    RBGL_1.46.0                survival_2.38-3            XML_3.98-1.4              
[13] foreign_0.8-66             DBI_0.3.1                  BiocParallel_1.4.3         RColorBrewer_1.1-2        
[17] lambda.r_1.1.7             plyr_1.8.3                 stringr_1.0.0              zlibbioc_1.16.0           
[21] Biostrings_2.38.4          munsell_0.4.3              gtable_0.2.0               futile.logger_1.4.1       
[25] OrganismDbi_1.12.1         labeling_0.3               latticeExtra_0.6-28        Biobase_2.30.0            
[29] GGally_1.0.1               IRanges_2.4.8              biomaRt_2.26.1             BiocInstaller_1.20.1      
[33] GenomeInfoDb_1.6.3         AnnotationDbi_1.32.3       Rcpp_0.12.3                acepack_1.3-3.3           
[37] scales_0.4.0               BSgenome_1.38.0            S4Vectors_0.8.11           graph_1.48.0              
[41] Hmisc_3.17-2               XVector_0.10.0             Rsamtools_1.22.0           gridExtra_2.2.1           
[45] digest_0.6.9               stringi_1.0-1              biovizBase_1.18.0          GenomicRanges_1.22.4      
[49] grid_3.2.3                 tools_3.2.3                bitops_1.0-6               magrittr_1.5              
[53] RCurl_1.95-4.8             RSQLite_1.0.0              dichromat_2.0-0            Formula_1.2-1             
[57] cluster_2.0.3              futile.options_1.0.0       reshape_0.8.5              rpart_4.1-10              
[61] GenomicAlignments_1.6.3    nnet_7.3-12               

 

ggbio • 1.7k views
ADD COMMENT
1
Entering edit mode
@james-w-macdonald-5106
Last seen 15 hours ago
United States

It is possible to control the colors, but you have to follow the ggplot2 API. Part of that API requires that all 'aesthetic mappings' have to be defined in the aes or aes_string functions. In addition, the colors you specify in general have to be part of the input data (unless you specify a different set of data for the colors, in which case there has to be a logical mapping of the colors in the new data to the old data). For example, the code that worked for you does so because there is a gieStain column in the ideoCyto$hg19 data.frame, and doing aes(colour=gieStain) is equivalent to saying 'hey, color my karyotype using the factors in this column of the input data'.

Both ggbio and ggplot2 are double-edged swords with really sharp edges. If what you want to do is what the developers already had in mind, you can make really sweet plots with minimal effort. However, if you want to do something a little different it may take way more work than you anticipated, requiring much rending of hair and grinding of teeth.

ADD COMMENT
1
Entering edit mode
Mario Saare ▴ 20
@mario-saare-6739
Last seen 15 months ago
Estonia

Dear James,

I will try to make it more clear that it is a technical problem and not a matter of my ignorance. Below, I have taken a few lines of code directly from the ggbio vignette. The last autoplot command should create a karyogram with four colors representing the different annotations for the genomic intervals. The 'color' argument is outside of the aes() command and defines a custom color for each annotation, which is exactly what I want to do with my data. This example worked perfectly in R 3.1.2 (with ggplot2 1.0.1 and ggbio 1.14.0), but not anymore in the current R 3.2.3 (with ggplot2 2.1.0 and ggbio 1.18.5). ggplot2 had a major upgrade in December 2015, and I believe it broke some of the functionality in ggbio karyogram layout. Whatever the changes where, they don't give an error message when plotting the karyogram, and I guess the authors have not noticed that the figure in the latest vignette (page 47) does not show the colors anymore (it's the same with the figure on page 48). Yes, I can use the aes() function and then change the colors with scale_fill_manual/scale_color_manual, but I think it would be good to restore the lost functionality. Or, the authors of ggbio could say that it is not a major problem and the users should stick to using aes() to map colors.

library(ggbio)

data(ideoCyto, package = "biovizBase")
data(darned_hg19_subset500, package = "biovizBase")
dn <- darned_hg19_subset500
library(GenomicRanges)
seqlengths(dn)
seqlengths(dn) <- seqlengths(ideoCyto$hg19)[names(seqlengths(dn))]
## then we change order
dn <- keepSeqlevels(dn, paste0("chr", c(1:22, "X")))
seqlengths(dn)

autoplot(dn, layout = "karyogram")

dn.nona <- dn[!is.na(dn$exReg)]
## compute levels based on categories
dn.nona$levels <- as.numeric(factor(dn.nona$exReg))
## do a trcik show them at different height
p.ylim <- autoplot(dn.nona, layout = "karyogram", aes(color = exReg, fill = exReg,
                                                      ymin = (levels - 1) * 10/3,
                                                      ymax = levels * 10 /3))

dn3 <- dn.nona[dn.nona$exReg == '3']
dn5 <- dn.nona[dn.nona$exReg == '5']
dnC <- dn.nona[dn.nona$exReg == 'C']
dn.na <- dn[is.na(dn$exReg)]

autoplot(seqinfo(dn3), layout = "karyogram") +
  layout_karyogram(data = dn3, geom = "rect", ylim = c(0, 10/3), color = "#7fc97f") +
  layout_karyogram(data = dn5, geom = "rect", ylim = c(10/3, 10/3*2), color = "#beaed4") +
  layout_karyogram(data = dnC, geom = "rect", ylim = c(10/3*2, 10), color = "#fdc086") +
  layout_karyogram(data = dn.na, geom = "rect", ylim = c(10, 10/3*4), color = "brown")

Mario

 

ADD COMMENT

Login before adding your answer.

Traffic: 704 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6