Skip to Content

ggplot2: Create an independent copy from an ggplot-Object

I'm not sure how to put this in OO-Speech. But when you are creating a ggplot it will be dependent from the source data.frame. So how can you save a ggplot without that dependency?

dat <- data.frame(x=runif(10),y=runif(10))  
g <- ggplot(dat, aes(x,y)) + geom_point()  
dat <- NULL  

The second $g$ won't produce a plot hence dat is $NULL$. How can I save $g$ so that dat can be changed?

I know it is not good practice but I got some very long code on which I don't want to fiddle about.