Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save a plot in an object

Tags:

plot

r

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?

like image 810
Remi.b Avatar asked Apr 11 '15 22:04

Remi.b


People also ask

Can you save a plot as an object in R?

Bookmark this question. Show activity on this post. In ggplot2 , one can easily save a graphic into a R object. The standard function plot produces the graphic as a void function and returns NULL.

How do I save a plot in a list?

First, we create file names using names() function on the list of plots. And then use the created file names and plots as input to pwalk() function to save as plots. In this example, we save the plots as png file using ggsave function as one of the arguments to pwalk().

How do I save a plot in R?

Under Windows, right click inside the graph window, and choose either "Save as metafile ..." or "Save as postscript ..." If using Word, make sure to save as a metafile.

How do I save a Ggplot file as a png?

You can either print directly a ggplot into PNG/PDF files or use the convenient function ggsave() for saving a ggplot. The default of ggsave() is to export the last plot that you displayed, using the size of the current graphics device. It also guesses the type of graphics device from the extension.


3 Answers

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)
like image 120
baptiste Avatar answered Oct 06 '22 01:10

baptiste


I am very late to this, but it was the first question which showed up when I searched for the question. So I'd like to add my solution for future viewers who come across the question.

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.

Hope that helps someone who stumbles across this later!

like image 32
cartwheel Avatar answered Oct 06 '22 00:10

cartwheel


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.

like image 39
LyzandeR Avatar answered Oct 06 '22 00:10

LyzandeR