Saved Histogram does not open in R

Hello,

I ran a code that generates multiple graphs and saved it at the end.
When opening the graphs file saved in R, all the graphs appears, except the histograms.

Here is an example:

data <- mtcars

residuo_test <- (mtcars$gear - mtcars$carb) / mtcars$carb * 100

hist_ap_19 <- list ()

Histogram

a <- expression (Error ~ class ~ ("%"))
b<- expression (frequency ~ ("%"))

graf_rede_ij_hist1 <- ggplot (mtcars, aes (residuo_test)) + geom_histogram (aes (y = stat (count) / sum (count) * 100), bins = 30) + continuous_scale (limits = c (-50.50,10), name = a) + scale_y_continuous (name = b) +
theme_classic () +
theme (plot.title = element_text (size = 25, face = "bold"),
axis.title = element_text (size = 25, color = "black"),
axis.text = element_text (size = 25, color = "black"))

hist_ap_19[[1]] <- graf_rede_ij_hist1

Save

saveRDS (hist_ap_19, "D: / Desktop / test")

This is how I plot the histogram, but when I open it in a new R session, I get the error:

test <- readRDS (file.choose ())
test [[1]]

FUN error (X [[i]], ...): object 'residuo_test' not found

That's because I no longer have the 'residuo_test' data in the environment.
I've tried to save as a print, but it's a loop, and all graphs are saved as the same histogram. Inserting residual calculation in the geom_histogram does not work either.

How can I plot the histograms and open in a new R session without that error?
Can someone help me, please?

You wont be able to plot the old histograms if you don't have a way to at least recalculate the absent part.
If it was literally as per your example it should be easy as you have mtcars inorder to calculate residiou test again. but I guess you might be in a situation where you had data but didnt keep it ? thats very unfortunate if so :frowning:

in future, I recommend you minimize mixing and matching different data sources for your ggplot's, in this case it would have been 'safer' to simply calculate residiou_test and store as another column of your data (mtcars), and that way the aes calls would have simply referred to contents of a single dataframe that would be part of the ggplot2 data source, rather than lazy evaluated at runtime

1 Like

It is strange because the data set is saved in the graph objects, and geom_histogram is the only one that does not identify the graph elements in the database

The solution was to create 'residuo_test' column, but is also necessary to use 'data = xxxx', or the same error will occur for the database name. With that it will be possible to open the graphs again in a new R session without problems.