jpquast / ggplate

Create Layout Plots of Biological Culture Plates and Microplates
https://jpquast.github.io/ggplate/
Other
90 stars 6 forks source link

Allow user control over whether plot is displayed when calling `plate_plot()` #9

Closed gl-eb closed 10 months ago

gl-eb commented 1 year ago

When using the plate_plot() function to plot many layouts at once (e.g. using purrr::map()) and later saving them to files directly, having the function open a graphics device for each plot is unnecessary. I implemented an additional parameter currently called display_plot that allows the user to control whether the plot is displayed when the function is called. It also controls how the scale is set since graphics::par() will open a graphics device. Finally, I added an explicit return(plot) statement.

Any thoughts?

gl-eb commented 11 months ago

I've answered your comments and have made some changes. Let me know what you think.

One use case for plate_plot() returning the ggplot object is that this allows the user to further modify the plot or including it in figures using packages such as patchwork or cowplot.

I'll also clean up the history to remove commits that revert previous changes before you (potentially) merge the PR, so let me know once (and if) you are happy.

jpquast commented 10 months ago

I am still trying to figure out exactly what you want to do. If you provide me with your code maybe that would help. Ideally post a reprex showing the problem and how it should look like when it is fixed.

How I understand it is the following. You want to create multiple plots with e.g. map and save them all in a list, while you don't want to actually print any of the plots. Exactly this is achieved by just running the following code:

library(ggplate)
#> 📊 Welcome to ggplate version 0.0.1! 📈
#>                             
#> 🖍 Have fun plotting your data! 💻
library(purrr)

# Load example data
data("data_continuous_96")

list_data <- list()
for(i in 1:10){

  list_data[[i]] <- data_continuous_96
}

ggplot_objects_list <- map(.x = list_data,
    .f = ~{
      plate_plot(
        data = .x,
        position = well,
        value = Value,
        label = Value,
        plate_size = 96,
        plate_type = "round"
      )
    })

Created on 2023-11-30 with reprex v2.0.2

In my testing return(plot) and plot does not lead to a different output for the function. My reason for not explicitly using return(plot) is that I follow the tidyverse style guide: https://style.tidyverse.org/functions.html?q=return#return.

Maybe your confusion about what the function does came from graphics::par(). It does not open a graphics device but it just checks how large the current plotting window is, so that the scale of points is adjusted accordingly. This is unfortunately the only way this currently works. This could be fixed in the future by creating dedicated geoms for plate plots.

gl-eb commented 10 months ago

You want to create multiple plots with e.g. map and save them all in a list, while you don't want to actually print any of the plots.

This is exactly what I was trying to do.

I did some further testing using your reprex. When I run your code in RStudio or VS Code, all works as expected. However, when I use the the R console or radian directly through Terminal / iTerm (on an M1 machine running macOS Sonoma 14.1.2), a new graphics device is opened. This is indeed caused by graphics::par() and is known behaviour. The help page says:

Each device has its own set of graphical parameters. If the current device is the null device, par will open a new device before querying/setting parameters. (What device is controlled by options("device").)

However, I also determined that the changes I propose in this PR don't actually fix the problem. The only way to prevent this from happening is by setting the scale parameter in plate_plot(). You could mention this somewhere in the documentation of ggplate, but it's also a fairly niche issue.

Sorry for the confusion.