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.

Table of contents:

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

 

r graph figure 1 save data object base

 

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.

 

The YouTube video will be added soon.

 

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

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


4 Comments. Leave new

  • 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?

    Reply
    • 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

      Reply
      • Hi Joachim,

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

        Reply
        • 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)

          cowplot grid

          I hope that helps!

          Joachim

          Reply

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top