jxw
jxw

Reputation: 23

Is there a way to read a saved plot in to R?

Is there a way to read a saved plot in to R, and assign it an object - the equivalent of reading in a csv?

df<-read.csv('test.data.csv')

The end goal is that I have 500 plots that were saved using ggsave() that I'd like to reposition via cowplot(), which only seems to be able to access objects in the active working environment.

Thanks for any insight.

Upvotes: 1

Views: 3189

Answers (2)

Dan Tarr
Dan Tarr

Reputation: 219

You can save individual plot objects to your hard drive in an RDS file (1 object per RDS file):

# sample plot
plot = ggplot(data = cars, aes(x = speed, y = dist)) + geom_point()

# save plot object
saveRDS(plot, file = "C:/data/plot.RDS")

# read plot object back into r 
plot <- readRDS("C:/data/plot.RDS")
plot

Upvotes: 0

Bishops_Guest
Bishops_Guest

Reputation: 1472

No, once the plot is saved as a .jpeg or .pdf or whatever image format you use, the back end data that is stored in the R object is lost.

You can save the R plot object using the save() function and then call that back with the load() function. However this will not be saved in a format that most other programs will recognize as an image. It is not something you could load into powerpoint.

If all you need is loading a straight up image into R, then see the answers to this question: how to read.jpeg in R 2.15

Upvotes: 1

Related Questions