Cannot open plots created with brms::mcmc_plot due to lack of `discrete_range` function



I would like to ask for advice regarding some plots that were created using brms::mcmc_plot(), and cannot be opened in R now.

The plots were created last year using brms 2.17.0, and were saved in RDS objects. The problem I have is that I cannot open the plots in R now because I get an error related to a missing function. I would be very grateful if someone could please advise me if they can think of a possible reason or solution.

I’ve tried to open the plots while having brms 2.17.0 loaded, and then while having the latest version (2.19.0) loaded. In both cases, I’ve got the folllowing error: Error in discrete_range() : could not find function "discrete_range".

My concern is about the reproducibility of the original work using an R Markdown document that draws on these plots. I haven’t tried to create the plots from scratch now because I don’t need to work with these plots specifically. I would just like the R Markdown document to run. Of course, if worst comes to worst, I could recreate the plots, but that would require recreating the brms models, which takes weeks.

If you would be so kind as to take a look, please download one of the files here. A printed version can be downloaded here. The code that was used to create the plot can be found here.

Thank you very much

There’s something about the structure of this plot object that’s incompatible with the version of ggplot2 currently installed on your machine (mcmc_plot actually uses functions from the bayesplot package to generate the plots, and bayesplot uses ggplot2 for the actual plotting). I’m not sure if this is an issue specific to plot objects produced in the past with mcmc_plot/bayesplot, or if there was some more general change in the internal structure of ggplot objects that affects ggplots2’s backward compatibility with ggplot objects produced in the past.

Based on the timing of when the plot object was created, I installed ggplot2 version 3.3.5 and the plot displayed without error. So, if you need to display that particular plot object, you can do the following in a clean R session:

devtools::install_version("ggplot2", "3.3.5")


p = readRDS("lexicaldecision_posteriordistributions_informativepriors_exgaussian.rds")

Hopefully, your rmarkdown document will compile without error with this earlier version of ggplot2 installed. In terms of future reproducibility, if you have a saved model object then you should be able to reproduce any related plots as needed (though it sounds like the model object wasn’t saved). Although it won’t necessarily help for a past project, going forward, you can use the renv package to record and save the specific R version and package versions that were used to produce a given R project’s output.

1 Like

Thank you very much. That was spot-on.

I fully agree with your advice. I have renv and indeed I thought I had reinstalled ggplot2 3.3.5 these days, but apparently not. Now I’ve reinstalled and it’s worked like a charm. I’ll post an issue to ggplot2 in case they can and want to patch this breaking change.

Normally I save models but these took up around 15 GB each.

1 Like

Hi! Did you post an issue? Do you have a link for it? I"m having the same problem, but can’t seem to unload ggplot2 because it is imported by bayesplot :(


Do you mean that you have an old saved ggplot object that you’d like to load and display? If so, does installing an earlier version of ggplot2 allow you to display the plot?

1 Like

Hi! If you have, as I did, only a file containing the ggplot object, you don’t have to load bayesplot. Just backtracking to the appropriate version of ggplot2 should do.

I posted about this breaking change at Breaking change regarding lack of `discrete_range()` function · Issue #5264 · tidyverse/ggplot2 · GitHub.

1 Like

Yes, I ended up updating and rerunning the graphs from scratch, which worked. Thank you!