DillonHammill / CytoExploreR

Interactive Cytometry Data Analysis
61 stars 13 forks source link

cyto_plot_save() not successful #96

Closed rwbaer closed 3 years ago

rwbaer commented 3 years ago

Briefly describe what you hope to achieve: I am trying to save a .png version of a histogram montage stack that is drawn on the RStudio graphics device after popup = TRUE fails to draw a standalone image.

The .png file is created but is 0 KB in size. Adding dev.off() (multiple times even) did not make the save successful although it did increase the size of the file to 7 KB. Opening the 7 kb .png image produces a blank image.

If I use dev.new() to create a graphics window, and the GUI menu of the graphics window to do the saving of a .png I can save an image.

Outline the steps taken to attempt to reach this goal (paste code below):

cyto_plot(gs, parent = "Live Cells",
          channels = "FL1-AREA",
          popup = TRUE,
          density_smooth = 0.4,
          density_layers = 10, # 4 samples per plot
          density_stack = 1, # stacking fully
          density_modal = TRUE,
          density_fill = c(rep("red", 4), rep("blue", 6)),
          density_fill_alpha = c(0.5, 0.5, 0, 0, 0.5, 0.5, 0.5, 0, 0, 0) ,
          density_line_type = rep(1, 10),
          density_line_width = c(rep(2, 4), rep(1, 6)),
          density_line_col = c(rep("red", 4), rep("blue", 6)),
          label_text = fil,
          label_text_x = rep(.075000,10),
          title = "CD206 Staining; M0 Red; M2  Blue; unstained open")

# Call cyto_plot_save
cyto_plot_save("CD206 Expression 2020-10-29.png",
               height = 10,
               width = 7,
               units = "in",
               res = 300)
dev.off()

EDIT: After closing RStudio, reopening it, and reloading the project, I had success at getting cyto_plot() to honor the popup = TRUE. This still did not give me a working cyto_plot_save()

Include any associated screenshots or images here:

DillonHammill commented 3 years ago

popup must be FALSE to use cyto_plot_save().

rwbaer commented 3 years ago

It has been a while since I tried to fight my way through this one, but I tried using cyto_plot_save() again today and failed. To make things easier for checking, I tried to get a repex using the built-in activation data set.

As before, the file saves but can not be opened. The problem has all the hallmarks of what happens when you don't do a dev.off() in base graphics. Here is the example.

library(CytoExploreRData)
library(CytoExploreR)
# Activation FCS Files
cyto_save(Activation,
          save_as = "Activation-Samples")

# Short test
gs <- cyto_setup("Activation-Samples",
                 gatingTemplate = "Activation-gatingTemplate.csv")

cyto_plot(gs[[1]],  parent = "root", channels = c("FSC-A", "SSC-A"))
cyto_plot_save("temp.png")

temp.png appears in the directory but can not be opened on Windows 10, version 10.0.19042 version 19042

DillonHammill commented 3 years ago

cyto_plot_save() must always be called before cyto_plot() it is used to set up the graphics device used for plotting.

# Reset all cyto_plot related settings
cyto_plot_reset()

# Open graphics device
cyto_plot_save("temp.png")

# Add the plot
cyto_plot(gs[[1]],  parent = "root", channels = c("FSC-A", "SSC-A"))

# Save to file (in case)
cyto_plot_complete()

This is explained in detail at the bottom of this page on the website: https://dillonhammill.github.io/CytoExploreR/articles/CytoExploreR-Visualisations.html

rwbaer commented 3 years ago

Very, very helpful.

FWIW, it's not that I didn't look at the website. It's that I didn't understand the role of the cyto_plot_save() function as setting up the graphics device which is not made explicit in that code fragment. The comments in your code fragment above makes my misunderstanding immediately obvious.

It might be helpful to change the comment for cyto_plot_save() on the website to indicate that it is initiating the plotting sequence. Right now it says, "# Call cyto_plot_save". That code fragment is about a custom plotting sequence, but it seems cyto_plot_save also INITIATES simple plotting sequences you wish to save.

Another part of my misunderstanding come from the function name. In ggplot2 graphics, there is a ggsave() command that actually SAVES the most recently created plot (by default) rather than INITIATING a plot sequence. cyto_plot_save() seems more analogous to png(), dev.new() or x11() to me, and I think I should be ok from here.

That said, somehow "?cyto_plot_save" did not save me from my misconceptions either ;-) Your fragment above also tells me that I need to learn about cyto_plot_reset() which isn't included in the web link you pointed me at.

Thanks as always for your patience with me and the clarification.

DillonHammill commented 3 years ago

Happy to help. I will update the website when the new version is released. cyto_plot_save() is much more sophisticated than just a call to png() - it handles a lot of other cyto_plot() settings. The equivalient of dev.new() is cyto_plot_new(). cyto_plot_reset() just closes any graphics device and resets any settings that have been changed by calling cyto_plot_save() - you shouldn't need this if cyto_plot_save() is followed by a call to cyto_plot(). Significant improvements are coming to these family of functions so keep an eye out for the new version.

rwbaer commented 3 years ago

Super. Looking forward to the new version.