Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

autoplot of GatingHierarchy creates spurious empty plot #101

Open
malcook opened this issue Sep 12, 2024 · 5 comments
Open

autoplot of GatingHierarchy creates spurious empty plot #101

malcook opened this issue Sep 12, 2024 · 5 comments

Comments

@malcook
Copy link

malcook commented Sep 12, 2024

Executing the following produces two plots in the pwd, Rplot001.png and Rplot002.png

The first plot is blank.

I expect only the second plot should be created.

png()
dataDir <- system.file("extdata",package="flowWorkspaceData")
gs <- load_gs(list.files(dataDir, pattern = "gs_manual",full = TRUE))
gh <- gs[[1]]
autoplot(gh) # by default the strip.text shows the parent population
dev.off()
null device 

using

> sessionInfo()
R version 4.4.0 (2024-04-24)
Platform: x86_64-pc-linux-gnu
Running under: Rocky Linux 8.10 (Green Obsidian)

Matrix products: default
BLAS/LAPACK: /usr/lib64/libopenblasp-r0.3.15.so;  LAPACK version 3.9.0

locale:
[1] C

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

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

other attached packages:
 [1] flowStats_4.16.0     flowViz_1.68.0       lattice_0.22-6       flowClust_3.42.0     openCyto_2.16.1      ggcyto_1.32.0        flowWorkspace_4.16.0
 [8] ncdfFlow_2.50.0      BH_1.84.0-0          flowCore_2.17.1      cowplot_1.1.3        scales_1.3.0         data.table_1.15.4    ggplot2_3.5.1       
[15] magrittr_2.0.3       withr_3.0.0          functional_0.6      

loaded via a namespace (and not attached):
 [1] gtable_0.3.5        ks_1.14.2           latticeExtra_0.6-30 Biobase_2.64.0      bitops_1.0-7        vctrs_0.6.5         generics_0.1.3      stats4_4.4.0       
 [9] tibble_3.2.1        fansi_1.0.6         DEoptimR_1.1-3      cluster_2.1.6       fds_1.8             pkgconfig_2.0.3     Matrix_1.7-0        KernSmooth_2.23-22 
[17] RColorBrewer_1.1-3  S4Vectors_0.42.0    graph_1.82.0        lifecycle_1.0.4     farver_2.1.2        compiler_4.4.0      deldir_2.0-4        munsell_0.5.1      
[25] mnormt_2.1.1        clue_0.3-65         rainbow_3.8         RCurl_1.98-1.14     pracma_2.4.4        crayon_1.5.2        hexbin_1.28.3       pillar_1.9.0       
[33] rrcov_1.7-6         MASS_7.3-60.2       mclust_6.1.1        robustbase_0.99-4   RProtoBufLib_2.16.0 tidyselect_1.2.1    mvtnorm_1.2-6       dplyr_1.1.4        
[41] labeling_0.4.3      splines_4.4.0       pcaPP_2.0-5         grid_4.4.0          colorspace_2.1-0    cli_3.6.2           RBGL_1.80.0         XML_3.99-0.16.1    
[49] utf8_1.2.4          corpcor_1.6.10      matrixStats_1.3.0   jpeg_0.1-10         interp_1.1-6        deSolve_1.40        gridExtra_2.3       cytolib_2.15.3     
[57] png_0.1-8           rlang_1.1.4         Rcpp_1.0.12         hdrcde_3.4          fda_6.1.8           glue_1.7.0          Rgraphviz_2.48.0    BiocGenerics_0.50.0
[65] R6_2.5.1            plyr_1.8.9          IDPmisc_1.1.21      zlibbioc_1.50.0    
> 
@mikejiang
Copy link
Member

This will give you single image

png()
grid::grid.newpage(); 
object =autoplot(gh) # by default then strip.text shows the parent population
grid::grid.draw(ggcyto_arrange(object))
dev.off()

@malcook
Copy link
Author

malcook commented Sep 13, 2024

Thanks for the workaround. Is it possible that autoplot can be modified to not create a blank page for gatingHierarchy? I am mapping autoplot across a list of flowSets and gatingSets and returning the collected combined results to calling function which is the printing/rendering them to a png. At that point it is too late the perform the workaround adjustments you propose.

@SamGG
Copy link

SamGG commented Sep 14, 2024

At that point it is too late the perform the workaround adjustments you propose.

I don't see your point. Either you can modify your code and redo the autoplots, either you already got png and process them.

Depending on your code, I would try to write an autoplot function that may have precedence over the ggcyto autoplot.

library(ggcyto)

autoplot <- function(gh) {
grid::grid.newpage(); 
object = ggcyto::autoplot(gh)
grid::grid.draw(ggcyto_arrange(object))
}

your initial code

@malcook
Copy link
Author

malcook commented Sep 14, 2024

Thanks. I'm taking your suggestion.

I'd already been shadowing autoplot.

I've modified my efforts as you suggest, resulting in:

autoplot <- function(object,strip.text = "gate",bins = 128,...) {
    ## override ggcyto::autoplot to change behavior to:
    ##  - default strip.text to "gate" (instead of 'parent')
    ##  - default bins to 128 (instead of 64)
    ##  - use data's range as plot limits
    ##  - apply [workaround](https://github.com/RGLab/ggcyto/issues/101#issuecomment-2350918986)
    gh<-isa(object,'GatingHierarchy')
    if (gh) grid::grid.newpage(); 
    ap = ggcyto::autoplot(object
                         ,...
                         ,strip.text = strip.text
                         ,bins = bins
                          ) + 
        ggcyto_par_set(limits = "data") 
    if (gh) grid::grid.draw(ggcyto_arrange(ap)) else ap
}

@malcook malcook closed this as completed Sep 14, 2024
@malcook
Copy link
Author

malcook commented Sep 15, 2024

I think I perhaps closed this prematurely.

I am reminded now that ggcyto::autoplot returns a value that isa 'ggplot' except when the passed object isa 'GatingHierarchy' (as @mikejiang noted #67 (comment)).

@SamGG - again thanks for your suggestion, but, the point I was trying to make earlier still applies. Let me try and make it better.

Your proposed solution, which I have tried to take, actually renders (with grid::grid.draw) the TableGrob (created with ggcyto_arrange) within the call to autoplot. This is precisely what i am trying to avoid. I am trying to render the plot once only, in a different context than the call to autoplot, using print.

I think I can demonstrate the issue using the package's own examples quite clearly:

rm(autoplot) # loose my overridden version (in case re-running this demonstration)

# run all the package's examples and collect plots as set of pngs:
png('autoplot.flowSet.example.%d.png')
example('autoplot.flowSet','ggcyto',ask=FALSE)
dev.off()

# now, demonstrate the issue by trying to separate the creation of the plot (with autoplot) from its rendering (with print).
png('autoplot.flowSet.example.demo_issue.%d.png')
p<-autoplot(gh)
print(p)
dev.off()

# observe that two pngs (one blank) were created:
Sys.glob('autoplot.flowSet.example.demo_issue.*.png')

# now redefine autoplot as proposed
autoplot <- function(...) {
    grid::grid.newpage(); 
    result = ggcyto::autoplot(...)
    grid::grid.draw(ggcyto_arrange(result))
}

# and demonstrate the issue persists in that the plot is rendered without calling `print` at all
png('autoplot.flowSet.example.proposed_solution.%d.png')
p<-autoplot(gh)
# print(p)
dev.off()
Sys.glob('autoplot.flowSet.example.proposed_solution.*.png') 
# that file has the gates rendered even though print was never called!!!

I'm not sure whether I should expect this issue to be addressed since it is so longstanding and perhaps is it inadvisable to depend upon autoplot magic to do the right thing. If so, I'd suggest it getting re-closed with "won't fix".

?? 🙃

@malcook malcook reopened this Sep 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants