You have produced several plots in R, and now you want to go
back and take a look at one of those old plots. On a Mac or
RStudio, you can do that with the navigation keys. R for Windows
has the same functionality but it is not enabled by default (for
good reasons, see Warning below). Here's how to enable it.|
Short term fix
To start recording plots, go to the History menu in the
graphics window and check Recording. The current plot and plots
you do subsequently will be added to the record. You can scroll
through recorded plots with the PgUp and PgDn arrow keys.
Instead of turning Recording on, you can add individual plots
to the record with the History > ADD menu option or with the
The plots are recorded in an object called
in your workspace, so they do not disappear when you close the
graphics window: you can open a new graphics window and still
review the old plots. However, plots in the new graphics window
will not be recorded unless you turn History > Recording on
You can open a graphics window with recording already on
using the command
windows(record=TRUE) before the
All of those options require forethought: you need to know
that you will want to return to that plot before you
obliterate it with the next one. That rarely works for me! I
like to change the default behaviour so that recording is on
when I do the first plot.
Changing the default setting
You can change the default settings of the graphics window
windows.options function: do
windows.options(record=TRUE) and any graphics windows
opened will have recording turned on. That might be useful in a
specific script, but I don't want to have to do that every time
I open R. Instead, I put the command into my .Rprofile
file. The .Rprofile file (note the dot at the start of the
file name) is an optional file of R code which is run every time
that R starts up. Here's how to put it into your .Rprofile
- Check the location with
Sys.getenv("R_USER"); it is usually your Documents
- Check to see if you already have an .Rprofile file there.
Open Documents in File Manager, go to the View tab and make sure
"File name extensions" is checked (otherwise file names
beginning with a dot will not be shown).
- If it exists, open with Notepad or other text editor and
add the code below at the end of the file.
- If it does not exist, open Notepad with a new file, and save
it in Documents with the name ".Rprofile" including the
quotation marks, as shown in the screen shot below. This
prevents Notepad adding ".txt" to the end of the name.
- Copy-paste the following code into your .Rprofile
- Save and close.
When you open R in future, it will read your .Rprofile
file and graphics windows will open with recording enabled.
The plots are recorded in your workspace in an object called
This can become very big, and can take up a large proportion of
the limited memory available to R. This can happen if:
- you have very large plots
with thousands of points; or
- you have lots of
plots, even if each one is small.
If you routinely save your workspace when you exit R, then
restore it when you start R again, the
object will continue to grow with every session.
You can check the size with
and remove it either with the History > Clear history
option in the graphics window or with
Just turning off History > Recording will not remove the past
Don't rely on the plot history as a permanent record of your
plots. If you need that, save them as JPG or PDF files with the
File > Save as... menu options.