Exporting a plot to a file

Jump to the instructions and examples!

There are two main techniques for saving a plot in R/RStudio:

1: Saving a plot programmatically, the preferred method.

  • You have fine control over all options.
  • It is reproducible
    • It does not depend on RStudio
    • It is not point-and-click: you don’t have to remember all the options you selected in the menus.

2: Using the RStudio export function in the Plot pane.

  • I’m not going to cover this because it is self-explanatory and it is not the preferred way to save a figure.

Graphics export functions

There are lots functions to save figures as raster-based graphics files such as png, tiff, jpeg, gif and vector-based files such as pdf, svg, and eps.

Two of the most helpful are the pdf() and png()

The Data

I’ll illustrate the process with one of R’s built-in datasets: mtcars.

First take a look at the data:

data("mtcars")
head(mtcars)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

Next create a simple boxplot with base graphics:

boxplot(
  qsec ~ cyl, 
  data = mtcars,
  xlab = "Number of cylinders",
  ylab = "Time from stopped to 1/4 mile",
  main = "Cylindars and Acceleration!")

Saving a file: examples

Syntax

To save a figure programatically:

  1. Run the function to create the file: pdf(), png(), etc. This tells R to create a file and to stage all of the plot output for saving.
  2. Run the code to build the figure. This can be a simple one-line plot call, or more complex multi-function code. You won’t see the plot since R is directing it to the output file instead of the on-screen plot window.
  3. Run dev.off(). This tells R to write all of the plot data to the file and save it.

Saving to a png file (basic)

png(filename = "mtcars_boxplot.png")
boxplot(
  qsec ~ cyl, 
  data = mtcars,
  xlab = "Number of cylinders",
  ylab = "Time from stopped to 1/4 mile",
  main = "Cylindars and Acceleration!")
dev.off()
## png 
##   2

Saving to a png file (fancy)

The graphics functions have lots of options to customize your output files. You can investigate the various arguments on your own.

require(here)
png(
  filename = here("mtcars_boxplot_hi_res.png"),
  width = 1200, height = 900, 
  res = 120, units = "px")

boxplot(
  qsec ~ cyl, 
  data = mtcars,
  xlab = "Number of cylinders",
  ylab = "Time from stopped to 1/4 mile",
  main = "Cylindars and Acceleration!")
dev.off()
## png 
##   2

Saving to PDF

Vector formats are better for publications. PDF is a popular format.

The syntax for pdf() is very similar to png()

pdf(
  file = here("mtcars_boxplot_hi_res.pdf"),
  width = 7, height = 10, bg = rgb(0.9, 1, 1))
boxplot(
  qsec ~ cyl, 
  data = mtcars,
  xlab = "Number of cylinders",
  ylab = "Time from stopped to 1/4 mile",
  main = "Cylindars and Acceleration!")
dev.off()
## png 
##   2

Finding your Image File

Now there will be a file called mtcars_boxplot.png in your working directory.

  • You should consider using the here package to organize your file input/output. Check it out: https://here.r-lib.org/

Things to Notice

  1. After you call the figure export function, R will not display your plots on screen. All graphics output is directed to the file.
  2. Argument names are not always the same in the various graphics functions. Consult the help entries.
  3. You have to call dev.off() to save the file. If you don’t, R will keep directing all graphics to the file. You won’t be able to display any plots on screen until you use dev.off() to save the file.
  4. You may see a message line png or pdf followed by a number after you call dev.off(). This is good!
  5. If you get stuck in plotting, or file saving limbo, you can run dev.off() several times to reset R’s plotting system. You should run dev.off() as many times as needed until you get a message like this:
dev.off()
dev.off()
## Error in dev.off(): cannot shut down device 1 (the null device)
dev.off()
## Error in dev.off(): cannot shut down device 1 (the null device)
## null device 
##           1