3

I have a series of ggplot objects that I'm trying to save to an .rdata file to load into a Markdown document later. The ggplot object itself is quite small (a few KB). But, when I try to save the object as an .rdata file for later retrieval, the resulting .rdata file is now over 8 gigabytes. I've tried saving the plot directly from the GUI, saving as an .rds... Saving as a .pdf or other image results in a normal image of a few KB.

I'm stumped, has anyone else encountered this problem?

Sample workflow below, I can't provide reproducible code for the problem since I can't upload the dataframe required to make this plot

mcmsy<- (ggplot(data = subset(MonteCarlo, Policy == 'RBFM' & 
Year == BaselineYear), aes(MSY), alpha = 0.8) + geom_density(fill = 'steelblue2'))

object.size(mcmsy)

save(mcmsy, file = 'mcmsy_plot.rdata')
DanO
  • 600
  • 3
  • 11
  • 1
    I'm not sure how I would go about attempting to help you without a reproducible example. – joran Aug 24 '15 at 22:05
  • ggplot makes copies of input data, especially when `stat_`'s are used. we really need to see `MonteCarlo`. also this is not reproducible with `movies` example in `stat_density`. – hrbrmstr Aug 24 '15 at 22:19
  • Thanks, I think I'm narrowing in on the problem. The "mcmsy" object is generated inside a function. The environment inside that function has several dataframes that are several gigabytes in size. Those objects aren't involved in mcmsy in any way shape or form. When I remove those large objects from the environment, generate mcmsy, and then save, the problem goes away. So, it appears that ggplot is saving data from the entire environment that mcmsy was generated in? – DanO Aug 24 '15 at 22:35
  • Take a look at `pryr::object_size` (and its documentation) – Ben Bolker Aug 24 '15 at 22:55

1 Answers1

3

I stumbled upon this problem as well. This is indeed related to the environment. If you want to save your plots as an Rdata file, then you should be creating a new environment inside the function that is generating your plot, so that the complete environment doesn't get saved. Example:

makePlot <- function(plot.data){
  env <- new.env(parent = globalenv())
  env$subset <- plot.data

  my.plot <- with(env, {
    my.plot <- ggplot(subset, ...) 
    return(my.plot)
  })

  return(my.plot)
}
DanO
  • 600
  • 3
  • 11
drgxfs
  • 1,097
  • 1
  • 8
  • 19
  • The above code does not seem to work, please see below: – HoneyBuddha Aug 01 '19 at 18:22
  • The makePlot function should be modified to use the "subset" variable -- not the plot.data variable, which is not available inside the "with" i.e. my.plot <- ggplot(subset, ...) [otherwise, you will get the following error: object 'plot.data' not found when you run the function] – HoneyBuddha Aug 01 '19 at 18:35