title: RDS file size difference between ggplot2 objects created inside vs. outside function
body: <p>I am trying to build an R project that generates multiple ggplot2 objects using functions. However, I noticed that, when saving these objects as RDS files, the file sizes are much larger than I expected. I realized that saving an RDS object generated with a function, and the same plot in the global environment, give two very different file sizes, despite occupying equivalent memory in the R session. For example:</p>
  
  <pre><code>library(ggplot2)
  data &lt;- data.frame(x = rnorm(1e6))
  
  p1 &lt;- ggplot(data) + 
    geom_histogram(aes(x = x))
  
  plot_fun &lt;- function(y) {
    p &lt;- ggplot(y) +
      geom_histogram(aes(x = x))
    return(p)
  }
  
  p2 &lt;- plot_fun(data)
  
  object.size(p1) # 8 Mb
  object.size(p2) # 8 Mb
  
  saveRDS(p1, "plot1.rds")
  saveRDS(p2, "plot2.rds")
  
  file.info("plot1.rds", "plot2.rds")
  </code></pre>
  
  <p>Does anyone know why this happens? Am I returning the object incorrectly from the function?</p>