7

I exported a list with some ggplot2 objects into a .RDS file, generated from a R 3.6.1 session. Then, tried importing it into a R 4.0.0 session and got the following error:

Error in identicalUnits(x) : 
  old version of unit class is no longer allowed

I can read and import these plots into an R object, but CAN'T plot. They actually have all the data within (data, layers, scales mapping...) but ggplot2 is not plotting them.

Is there any way around it? Have any of you encountered with this issue? Would ggplot2have plans to update the library so we can import older version plots? Hope you can help me find a solution, or at least a patch. Thanks!

Bernardo
  • 461
  • 7
  • 20
  • 5
    This is not an issue at the ggplot level, but at the grid package level. The grid package is part of the standard R distribution, and have updated their unit system going from R3.6.3 to R4.0.0. Hence, all their old units are invalid now and your ggplot objects no longer work. I check the ggplot github frequently, and have seen no plans to do something with this. – teunbrand May 28 '20 at 15:07
  • That's very useful to know. Do you happen to know any trick to we can do from the users end to plot these objects though? – Bernardo May 28 '20 at 16:53
  • I don't know about such tricks. The only option I could think of is either switch back to R3.6.3 or regenerate the plots in R4.0.0. Or manually inspect every aspect in your ggplot object, replacing any instances of `unit` variables, but that seems very laborious and isn't guaranteed to work. – teunbrand May 28 '20 at 16:57

4 Answers4

3

Saving ggplot2 objects into .Rds files is generally discouraged, because there is absolutely no guarantee that they will still work if either ggplot2 or (in this case) R is upgraded to the next release, even if it is minor release. ggplot2 objects contain large amounts of executable code (closures) as well as internal data structures, and these generally get out of sync whenever we generate a plot with one ggplot2 version and then try to print with another.

Another problem with saving a ggplot2 object to .Rds is that the entire R environment is saved as well, so that your .Rds file may blow up if you happen to have a large amount of data loaded into your session.

The only two safe approaches to retaining plots across versions are to 1. save your raw data and code that generates the plot or 2. save the plot output as .png, .pdf, etc.

Claus Wilke
  • 16,992
  • 7
  • 53
  • 104
3

I also ran into this issue, and was able to solve it by changing the theme of the loaded plot.

g <- readRDS(my_plots_file_path)
g$theme <- ggplot2::theme_minimal()

I think this is because that is where all the instances of unit variables existed in my plot.

  • I know best practice is to not save a plot this way, but for those of us who already have or inherited an Rds with a plot inside, this method works great! – m.evans May 20 '22 at 16:40
  • this should be the accepted answer, it saved my day – simplyPTA Jun 08 '22 at 08:16
1

I had the same issue which happened after I had globally set a custom theme. I noticed that if I re-defined the theme with 'complete = F' and then set the new theme the plotting works normally. Don't have a better solution yet unfortunately. This also happened with ggplot objects that were newly coded, i.e. not read from a .Rds file.

Noa Eren
  • 11
  • 1
-2

I am quite a newbie in R and had the same issue. What worked for me was running the graph's scripts in the new version, then they plot normally.