saezlab / MetaProViz

R-package to perform metabolomics pre-processing, differential metabolite analysis, metabolite clustering and custom visualisations.
https://saezlab.github.io/MetaProViz/
GNU General Public License v3.0
8 stars 0 forks source link

ggplot2 version newer than 3.4.4. gives error from PlotGrob helper functions #80

Open ChristinaSchmidt1 opened 1 month ago

ChristinaSchmidt1 commented 1 month ago

Newer ggplot version is not able to adapt grid. We need to check what was changed in the version

Error in grid.Call(C_convert, x, as.integer(whatfrom), as.integer(whatto), : Viewport has zero dimension(s)

Check which updates where done in the latest ggplot release: https://github.com/tidyverse/ggplot2/releases/tag/v3.5.1

maca8e commented 1 month ago

We narrowed down the error to this code block:

PlotGrob_PCA <- function(InputPlot, SettingsInfo, PlotName){
  #------- Set the total heights and widths
  #we need ggplot_grob to edit the gtable of the ggplot object. Using this we can manipulate the gtable arguments directly.
  plottable<- ggplot2::ggplotGrob(InputPlot) # Convert the plot to a gtable
  if(is.null(SettingsInfo)==TRUE){
    #-----widths
    plottable$widths[5] <- unit(1, "cm")#controls x-axis
    plottable$widths[c(3)] <- unit(2,"cm")#controls margins --> y-axis label is there
    plottable$widths[c(1,2,4)] <- unit(0,"cm")#controls margins --> not needed
    plottable$widths[c(6)] <- unit(8,"cm")#controls margins --> start Figure legend
    plottable$widths[c(10)] <- unit(0,"cm")#controls margins --> Figure legend
    plottable$widths[c(7,8,9,11)] <- unit(0,"cm")#controls margins --> not needed
    plot_widths <- 11

Problem might stem from some of the breaking changes introduced in ggplot2 v3.5.0. This included changes to the guide system as a whole, see 3.5.0 here:

The guide system encompassing axes and legends, as the last remaining chunk of ggplot2, has been rewritten to use the system instead of the S3 system.

The new documentation on functions to create or test if an object is ggproto() based is here: https://ggplot2.tidyverse.org/reference/ggproto.html. Acknowledged it is the least bad system...

In most cases, creating a new OO system to be used by a single package is not a good idea. However, it was the least-bad solution for ggplot2 because it required the fewest changes to an already complex code base.

What we might be able to do is to check which version of ggplot2 the user is running, then 1) if it is the old version, use the previous plot adjustment approach, 2) if it is v3.5.0 <=, we could try convert the ggplotGrob table (i.e. 'plottable') to a ggproto based plot.