wilkelab / cowplot

cowplot: Streamlined Plot Theme and Plot Annotations for ggplot2
https://wilkelab.org/cowplot/
701 stars 84 forks source link

No side effect from plot_grid when used inside a function returning an object #185

Open rivorand opened 2 years ago

rivorand commented 2 years ago

Hello.

I'd like to have a base graphics version alongside a ggplot2 version of a plot inside a function which returns some other object.

The issue is the return statement makes plot_grid produce no output at all (inside RStudio plot pane at least). If one gets rid of this statement, the plots do appear.

require(gridGraphics)
#> Loading required package: gridGraphics
#> Loading required package: grid
require(cowplot)
#> Loading required package: cowplot
require(ggplot2)
#> Loading required package: ggplot2

buggy_fun <- function() {
  ## Graphics
  ### base

  base.g <- ~ {
    plot(0:1, 0:1)
  }

  ### ggplot2
  ggplot.g <- ggplot() +
    geom_rect(aes(xmin= 0, xmax= 1, ymin = 0, ymax= 2), fill="green")

  plot_grid(base.g, ggplot.g, nrow=2)

  return(2) ## No side effect when something is returned from function
}

Created on 2022-05-02 by the reprex package (v2.0.1)