Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

@rivorand

Description

@rivorand

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions