# Save Plot in Data Object in Base R (Example)

In this tutorial youâ€™ll learn how to store a graphic in a data object in R programming.

Letâ€™s jump right to the example!

## Example: Save Graphic in Data Object Using recordPlot Function

The following R programming code explains how to record a Base R plot in a data object by applying the recordPlot function.

`plot(1:5, 1:5)                 # Create plot`

Figure 1 shows the output of the previous R code â€“ a simple plot created with the basic graphic options of the R programming language.

If we want to save this plot in a data object, we can use the recordPlot function as follows:

`my_plot <- recordPlot()        # Save plot in data object`

Letâ€™s test if it worked! First, we need to remove the plot that is already drawn in RStudio:

`plot.new()                     # Create empty plot window in RStudio`

Now, we simply need to call the data object, in which our plot is stored, to recreate the plot:

`my_plot                        # Draw saved plot`

Our plot should be shown again in your RStudio environment.

## Video, Further Resources & Summary

Do you need more info on the content of this tutorial? Then you could have a look at the following video of my YouTube channel. Iâ€™m explaining the R syntax of this article in the video.

Furthermore, you might have a look at some of the other articles that I have published on Statistics Globe. You can find a selection of tutorials about the plotting of data here:

In this tutorial, I explained how to save a Base R plot in a data object in the R programming language. Please let me know in the comments, if you have further questions and/or comments.

Subscribe to the Statistics Globe Newsletter

• Hi Joachim, do you know whether there is a way to use plots that were recorded in the way you described for allocation in a grid of multiple plots?
I tried to use recordPlot() for three different plots that I now would like to compose together to one plot, e.g. using the layout() function. This did not work, however. Once I try to draw a saved plot after running layout() or par(mfrow)), it just takes all the space of the plotting device.
Could you help with that?

• Hey Leander,

Thank you for the interesting question, and sorry for the delayed response, I just came back from vacation. Do you still need help with this?

Regards,
Joachim

• Hi Joachim,

I happen to have the same question. Could you explain please? Thank you very much.

• Hey Chen,

I have not found a solution for the layout function. However, you may combine your Base R plots in a grid of plots using the cowplot package.

Please have a look at the example below:

```library("cowplot")

plot(1:5, 1:5)
my_plot1 <- recordPlot()

plot(1:10, 1:10)
my_plot2 <- recordPlot()

plot(1:20, 1:20)
my_plot3 <- recordPlot()

plot_grid(my_plot1,
my_plot2,
my_plot3,
scale = 0.8,
ncol = 2)```

I hope that helps!

Joachim