patchwork icon indicating copy to clipboard operation
patchwork copied to clipboard

Unserialization of wrap_plots loses plots

Open Enchufa2 opened this issue 4 years ago • 1 comments

Steps to reproduce:

  1. Create a patchwork object and serialize it:
library(ggplot2)
library(patchwork)

p1 <- ggplot(iris) + aes(Sepal.Length) + geom_histogram()
p2 <- ggplot(iris) + aes(Sepal.Length, Sepal.Width) + geom_point()
wrap_plots(p1, p2)
# shows a composition of p1 and p2

saveRDS(last_plot(), "test.rds")
  1. In a clean R session:
readRDS("test.rds")
# shows only p2

Is this expected behaviour?

Enchufa2 avatar Apr 12 '21 13:04 Enchufa2

Actually, the following shows both plots:

library(patchwork)
readRDS("test.rds")

which shouldn't be necessary.

Enchufa2 avatar Apr 12 '21 13:04 Enchufa2

why shouldn't it be necessary to have patchwork attached when displaying a patchwork plot?

thomasp85 avatar Aug 04 '23 19:08 thomasp85

Attaching ggplot2 is not needed, so I would expect the same for patchwork.

Enchufa2 avatar Aug 04 '23 20:08 Enchufa2

The reason why it works in ggplot2 is a weird side effect of ggplot objects including a reference to the ggplot2 namespace deep inside the scales list. It was quite surprising as method dispatch in R in general doesn't work without the package loaded

I can probably try to emulate it in patchwork to cheat the classic R mechanics

thomasp85 avatar Aug 08 '23 10:08 thomasp85