bnprks / BPCells

Scaling Single Cell Analysis to Millions of Cells
https://bnprks.github.io/BPCells
Other
148 stars 14 forks source link

ggplot2 have deprecated theme element legend parameters #107

Closed acmullen-med closed 1 month ago

acmullen-med commented 1 month ago

Hello BPCells maintainers,

On line 26 of trackplots.R, the parameter legend.title.position has been deprecated in ggplot2 3.5.0: https://github.com/tidyverse/ggplot2/blob/fc62903c76d736510dbed24a36c42ef826762262/NEWS.md?plain=1#L54

When trying to use these plotting functions I receive this error:

Error in `mapply()`:
! The `legend.title.position` theme element is not defined in the element hierarchy.
Run `rlang::last_trace()` to see where the error occurred.

---
Backtrace:
    ▆
 1. ├─base (local) `<fn>`(x)
 2. └─ggplot2:::print.ggplot(x)
 3.   ├─ggplot2::ggplot_gtable(data)
 4.   └─ggplot2:::ggplot_gtable.ggplot_built(data)
 5.     └─ggplot2:::plot_theme(plot)
 6.       └─base::mapply(validate_element, theme, names(theme), MoreArgs = list(element_tree = get_element_tree()))
 7.         └─ggplot2 (local) `<fn>`(dots[[1L]][[98L]], dots[[2L]][[98L]], element_tree = `<named list>`)
 8.           └─cli::cli_abort(...)
 9.             └─rlang::abort(...)

sessionInfo:

R version 4.3.2 (2023-10-31)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 22.04.4 LTS

Matrix products: default
BLAS:   /net/gs/vol3/software/modules-sw/R/4.3.2/Linux/Ubuntu22.04/x86_64/lib/R/lib/libRblas.so 
LAPACK: /net/gs/vol3/software/modules-sw/R/4.3.2/Linux/Ubuntu22.04/x86_64/lib/R/lib/libRlapack.so;  LAPACK version 3.11.0

locale:
 [1] LC_CTYPE=C.UTF-8       LC_NUMERIC=C           LC_TIME=C.UTF-8       
 [4] LC_COLLATE=C.UTF-8     LC_MONETARY=C.UTF-8    LC_MESSAGES=C.UTF-8   
 [7] LC_PAPER=C.UTF-8       LC_NAME=C              LC_ADDRESS=C          
[10] LC_TELEPHONE=C         LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C   

time zone: America/Los_Angeles
tzcode source: system (glibc)

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

other attached packages:
[1] BPCells_0.2.0 ggplot2_3.5.1

loaded via a namespace (and not attached):
 [1] utf8_1.2.4                  generics_0.1.3             
 [3] SparseArray_1.2.4           bitops_1.0-7               
 [5] stringi_1.8.4               lattice_0.22-6             
 [7] magrittr_2.0.3              grid_4.3.2                 
 [9] Matrix_1.6-5                ggrepel_0.9.5              
[11] restfulr_0.0.15             GenomeInfoDb_1.38.8        
[13] fansi_1.0.6                 scales_1.3.0               
[15] XML_3.99-0.16.1             Biostrings_2.70.3          
[17] codetools_0.2-20            abind_1.4-5                
[19] cli_3.6.2                   rlang_1.1.3                
[21] crayon_1.5.2                XVector_0.42.0             
[23] Biobase_2.62.0              munsell_0.5.1              
[25] DelayedArray_0.28.0         withr_3.0.0                
[27] yaml_2.3.8                  S4Arrays_1.2.1             
[29] tools_4.3.2                 parallel_4.3.2             
[31] BiocParallel_1.36.0         dplyr_1.1.4                
[33] colorspace_2.1-0            GenomeInfoDbData_1.2.11    
[35] Rsamtools_2.18.0            SummarizedExperiment_1.32.0
[37] BiocGenerics_0.48.1         vctrs_0.6.5                
[39] R6_2.5.1                    matrixStats_1.3.0          
[41] BiocIO_1.12.0               stats4_4.3.2               
[43] lifecycle_1.0.4             stringr_1.5.1              
[45] rtracklayer_1.62.0          zlibbioc_1.48.2            
[47] S4Vectors_0.40.2            IRanges_2.36.0             
[49] pkgconfig_2.0.3             pillar_1.9.0               
[51] gtable_0.3.5                glue_1.7.0                 
[53] Rcpp_1.0.12                 tibble_3.2.1               
[55] GenomicRanges_1.54.1        GenomicAlignments_1.38.2   
[57] tidyselect_1.2.1            MatrixGenerics_1.14.0      
[59] rjson_0.2.21                labeling_0.4.3             
[61] compiler_4.3.2              RCurl_1.98-1.14  

I believe removing the legend.title.position will resolve the issue but I am not sure. When I tried to deprecate my version of ggplot2 to 3.4.0, the issue did not resolve.

Thank you for maintaining this package

bnprks commented 1 month ago

Hi @acmullen-med, thanks for reporting this!

Could you share what you're running to hit this issue? I have ggplot2 3.5.1 installed and most of the trackplot stuff I run has worked ok. As a very small example, this runs fine for me and draws a legend with the expected horizontal orientation with title on top:

loops <- list(start=c(1,3), end=c(5,4), chr=c("chr1", "chr1"), score=c(1,2))
BPCells::trackplot_loop(loops, "chr1:1-6", color_by="score")

I think actually legend.title.position was just introduced in ggplot2 3.5.0 (link), and the line you linked to appears to be talking about legend.title.align instead.

That said, I think there is at least one change needed in BPCells which will be to bump the minimum ggplot2 version required to >= 3.5.0 in the DESCRIPTION file.

If you could share the code you're running to hit this issue that would be helpful for me to investigate further.

-Ben

acmullen-med commented 1 month ago

I think you can close this issue ticket. After rebuilding main it seems to be working fine. I must have had something wrong on my end. Both your toy example and my below are working fine now.

That being said, both of the plotting functions trackplot_gene() and trackplot_coverage() were giving me the above error; however, trackplot_scalebar() did not which does not call trackplot_theme(). I forked the library, removed line 26 from trackplots.R, and built the fork. The plots would render and they looked fine to me. I don't know if it was from removing the line or rebuilding.

It seems like you've taken a closer look at the ggplot2 docs than me. I agree with your assessment.

The below is what I was using as my test example when debugging.

gtfFilePath <- "/net/bbi/vol1/data/genomes_stage/zebrafish/zebrafish_rna/Danio_rerio.GRCz11.99.gtf.gz"
gene_anno <- rtracklayer::readGFF(gtfFilePath)
gene_anno$chromosome <- gene_anno$seqid
gene_anno$gene <- gene_anno$gene_id
gene_anno$transcript <- gene_anno$transcript_id
gene_anno$symbol <- gene_anno$gene_name
geneSubset <- gene_anno[gene_anno$gene_biotype == "protein_coding",]
geneSubset$feature <- geneSubset$type
geneSubset$chr <- geneSubset$seqid
geneSubset$chr <- paste0("chr",geneSubset$chr)

region <- gene_region(geneSubset, "tbx16", extend_bp = 5e4)
gene_plotTbx16 <- trackplot_gene(geneSubset, region)
gene_plotTbx16