107

In ggplot2, one can easily save a graphic into a R object.

p = ggplot(...) + geom_point()      # does not display the graph
p                                   # displays the graph

The standard function plot produces the graphic as a void function and returns NULL.

p = plot(1:10)     # displays the graph
p                  # NULL

Is it possible to save a graphic created by plot in an object?

4
  • plot is a generic, and different plot methods do return such objects as far as I know. plot.default, however, indeed returns NULL. Apr 11, 2015 at 22:41
  • 1
    Is your goal to be able to replot your graph only by typing p after saving it as an object? Or would you like to save it as an object which you would then be able to alter its values for example?
    – LyzandeR
    Apr 11, 2015 at 22:51
  • I might want to overlay other graphics on top of the saved graphic but I don't aim to modify the plot that has been created and saved. Did I answer your question? Thks
    – Remi.b
    Apr 11, 2015 at 22:53
  • @Remi.b It does thanks. I posted a solution that might help.
    – LyzandeR
    Apr 11, 2015 at 22:59

4 Answers 4

116

base graphics draw directly on a device.

You could use

1- recordPlot

2- the recently introduced gridGraphics package, to convert base graphics to their grid equivalent

Here's a minimal example,

plot(1:10) 

p <- recordPlot()
plot.new() ## clean up device
p # redraw

## grab the scene as a grid object
library(gridGraphics)
library(grid)
grid.echo()
a <- grid.grab()

## draw it, changes optional
grid.newpage()
a <- editGrob(a, vp=viewport(width=unit(2,"in")), gp=gpar(fontsize=10))
grid.draw(a)
4
  • 1
    It's unclear to me: are (1) and (2) separate methods to achieve the same goal, or two steps both necessary to answer the OP's question?
    – NLi10Me
    May 25, 2017 at 15:04
  • @NLi10Me 2 different methods.
    – zx8754
    Jan 26, 2018 at 12:30
  • If I try saveRDS(object = p, file = "p.Rds"), then load a new R session, run p <- readRDS(file = "p.Rds") followed by p, I get an error saying Error in replayPlot(x) : loading snapshot from a different session. Am I saving the p object incorrectly? May 4, 2018 at 17:48
  • It seems that the error I was getting was resolved in R 3.3.0, using that version it works. When I used the gridGraphics method shown here, the colors on the redrawn plot kept getting messed up, even with grid.grab(wrap=TRUE) May 4, 2018 at 18:49
51

I solved this by using a function instead of an object. For example, suppose we want to compare two beta distributions with different parameters. We can run:

z1<-rbeta(10000,5,5)
z2<-rbeta(10000,20,20)

plotit<-function(vector,alpha,beta){
    plot(density(vector),xlim=c(0,1))
    abline(v=alpha/(alpha+beta),lty="longdash")
}

And save the plots as functions rather than objects.

z.plot1<-function(){plotit(z1,5,5)}
z.plot2<-function(){plotit(z2,20,20)}

Next, we can call each plot as we want by simply calling the two plots as functions rather than objects.

z.plot1()

plots the first plot and

z.plot2()

plots the second.

5
  • This is very useful ! I think it is a great idea in order to prepare presets plots this is what I was searching for
    – Jojostack
    Apr 7, 2018 at 17:32
  • 1
    This is a genius base R approach! recordPlot is definitely useful (if you have a window already open), but this answer is exactly what folks are looking for when they visit this post. +1! Jun 17, 2018 at 2:43
  • Especially useful for complex multiple plots.
    – Joe
    Aug 24, 2018 at 13:14
  • Let me join the chorus of people who really appreciate this trick. Also on behalf of my students :-) Jul 26, 2019 at 8:27
  • Lexical scoping to the rescue again! :)
    – Jason
    Dec 20, 2019 at 3:18
20

You can use the active binding feature of the pryr package if you don't want to directly change the values of the object created.

library(pryr)
a %<a-% plot(1:10,1:10)

Each time you type a on the console the graph will be reprinted on the screen. The %<a-% operator will rerun the script every time (in case of one graph this is not a problem I think). So essentially every time you use a the code will be rerun resulting in your graph which of course you can manipulate (overlay another plot on top) or save using png for example. No value itself will be stored in a however. The value will still be NULL.

I don't know if the above is what you are looking for but it might be an acceptable solution.

7
  • Thank you. That is a very handy solution. Do you know if this works if the plot is build through several lines (such as plot(1:10);abline(v=4) for example)? +1
    – Remi.b
    Apr 11, 2015 at 23:08
  • @Remi.b Yeah of course it does. You need to type it like this though" a %<a-% {plot(1:10);abline(v=4)}. If you type it inside those curly brackets you can have as many lines as you want! Also, if you want to reassign a value to a you need to remove it first with rm(a) and then reassign it with the %<a-% operator otherwise you get a warning. I don't know why this happens but I guess it is not a big deal.
    – LyzandeR
    Apr 11, 2015 at 23:13
  • This is really cool! I'll have to read a bit about this package because it sounds impossible to me to do what this weird function %<a-% is doing. Thanks
    – Remi.b
    Apr 11, 2015 at 23:17
  • Yeah it is another one of those really cool packages Hadley made. It is a set of functions that lets you understand the R language in depth. Have a look at %<d-% as well, it might come in handy later on. Glad I could be of help :)
    – LyzandeR
    Apr 11, 2015 at 23:23
  • 1
    @LyzandeR is it possible to combine the saved plots into a multiplot? May 12, 2016 at 10:10
-10
library(ggplot2)
# if mygraph is a plot object
ggsave("myplot1.png",mygraph)

# if the plot is in a list (e.g. created by the Bibliometrics package)
ggsave("myplot1.png",mygraphs[[1]])
3
  • Although your answer looks 'correct' (but I am no R programmer), it is normal practice on Stack Overflow to add some explanatory text, rather than just posting a 'terse' code-only block. This makes the answer more valuable in the long-term, and to a broader range of users. (But have an upvote, anyway!) Sep 30, 2019 at 12:34
  • 6
    The OP was asking about how to do this without using ggplot2.
    – Jason
    Dec 19, 2019 at 12:02
  • 2
    Also, this saves the plot as an image to a file. The OP wants to save the plot to an R object (a variable), which can be accessed in the code. The ggplot way to do this is trivial: plot <- ggplot(...), but they're specifically asking to do it in base graphics, which works differently
    – divibisan
    Jan 20 at 22:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.