why does deleting a ggplot2 object use more memory?

Note that ggplot tracks the last plot created in a cache in the package namespace. It can be retrieved by ggplot2::last_plot() and is there so functions like ggsave() can work without you having to explicitly pass the plot to the function. You can clear that out with

ggplot2::set_last_plot(NULL)

So for example

pryr::mem_used()
p1 <- ggplot2::ggplot(readr::read_rds(path), ggplot2::aes(V1))
rm(p1)
ggplot2::set_last_plot(NULL)
invisible(gc())
pryr::mem_used()

The cache is set anytime you call ggplot() or when a ggplot object is printed. It only stores the most recent value.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top