RGLab / ggcyto

Visualize Cytometry data with ggplot2
MIT License
56 stars 13 forks source link

Feature request: patchwork support #86

Open mstone-modulus opened 2 years ago

mstone-modulus commented 2 years ago

Hi,

Would it be possible to support composing figures produced via autoplot() with patchwork?

Currently, attempting to combine figures results in the following error:

> p1 + p2
Error in if (empty(data)) { : missing value where TRUE/FALSE needed
In addition: Warning message:
In class(x) <- c("patchwork", class(x)) :
  Setting class(x) to multiple strings ("patchwork", "ggcyto_flowSet", ...); result will no longer be an S4 object

I've found that I can use ggcyto_arrange() and gridExtra as a workaround when applying autoplot() to GatingHierarchy objects. For example, when wishing to view multiple gates across all samples in a GatingSet.

nodes <- c("nonDebris", "lymph")

gts <- lapply(gs, function(gh) {
  res <- autoplot(gh, nodes, bins=32, strip.text="gate")
  gt <- ggcyto_arrange(res, nrow=1)
})

p <- do.call(gridExtra::gtable_rbind, gts)
plot(p)

However, I can't use this approach when applying autoplot() to flowFrame objects (e.g. when replicating the 1D and 2D gating plots in the auto gating demo).

fr <- gh_pop_get_data(gs[[1]], "lymph")
chnl <- "Live"
g <- openCyto:::.tailgate(fr, channels = chnl, tol = 0.05)
p1 <- autoplot(fr, chnl) + geom_gate(g)
p2 <- autoplot(fr, chnl, "SSC-A") + geom_gate(g)

From what I can tell, this is because ggcyto_arrange() requires a ggcyto_GatingLayout, which autoplot() returns when applied to a GatingSet but not when applied to a flowFrame.

## res <- autoplot(gh, ...)
> is(res)
[1] "ggcyto_GatingLayout" "list"               
[3] "vector"              "AssayData"          
[5] "list_OR_List"        "vector_OR_Vector"   
[7] "vector_OR_factor" 

## p1 <- autoplot(fr, ...)  
> is(p1)
[1] "ggcyto_flowSet" "ggcyto"         "gg"            
[4] "ggplot"         "oldClass"     

Would it be possible to make the return type of autoplot() consistent across inputs? And is there a straightforward way to cast a list of ggcyto_flowSet objects to ggcyto_GatingLayout? I thought supporting patchwork might be the easiest way to support this kind of figure composition in the long run, but I'm happy to continue using the gridExtra workaround until it can be implemented.

Thanks!

Matt

mstone-modulus commented 2 years ago

edit: Casting to ggplot appears to solve this. Is there any reason why this might present an issue in practice?

as.ggplot(p1) + as.ggplot(p2)
malcook commented 1 month ago

edit: Casting to ggplot appears to solve this. Is there any reason why this might present an issue in practice?

67 ?