Graphs and plots in R

There are many ways to produce figures in R, and this document offers a guide through the maze for those who want to produce graphs and plots to include in other documents such as dissertations.

What format?

If you are writing a LaTeX document, you have the choice of including graphics in several different formats. For graphs and plots we recommend using PDF format. This is a vector graphics format in which the image is made up of lines and geometric shapes and can be resized without losing any detail. An alternative format is PNG, a bitmap format, commonly used in photographs or scanned images. Resizing a bitmap image generally results in a loss of detail and so should not be used for graphs and plots.

What is the best way to save the plot?

Use the R function pdf() rather than File -> Save as -> PDF... when saving plots.  The pdf() function allows you to specify exactly how an R graph appears and is the recommended method for creating PDF files to be included in LaTeX documents.  The function must be used before you begin the plot. If no options are given the file will be saved on your desktop with the name Rplots.pdf. If you want to specify a different name for the file use pdf(file='boxplot1.pdf"). No plot will appear, but it will be saved in the specified file. Once the plot is complete use to close the device. You should now be able to view the PDF file.

For more information about LaTeX and graphs see 'How to include R Graphs in LaTeX' in the LaTeX FAQ.

A worked example

Here are two plots, based on an example on the help page for boxplot. The first plot used the example code unchanged and was saved from the screen.

Plot using default values 

The graph below was created using the following R commands:

pdf(width=3.5, height=3, pointsize=9)
boxplot(len ~ dose, data = ToothGrowth,
        boxwex = 0.25, at = 1:3 - 0.2,
        subset = supp == "VC", col = "yellow",
        main = "Guinea Pigs' Tooth Growth",
        xlab = "Vitamin C dose mg", cex.main=1,
        ylab = "tooth length", medlwd=1.5,
        xlim = c(0.5, 3.5), ylim = c(0, 35), yaxs = "i")
boxplot(len ~ dose, data = ToothGrowth, add = TRUE,
        boxwex = 0.25, at = 1:3 + 0.2, medlwd=1.5,
        subset = supp == "OJ", col = "orange")
        legend(2, 11, c("Ascorbic acid", "Orange juice"),
fill = c("yellow", "orange"), cex=.9)

The additional code is bold and red; the original code from the boxplot example is red. The plot looks like this:

Plot using suggested values

The additional code has improved the layout of the plot in two ways:

  • The fonts are bigger and so the labels and captions are close the font size in a LaTeX document.
  • The is graph is now rectangular not square, so it will fit better with text on A4 paper.

The dimensions of the plot (width=3.5, height=3) should match the dimensions you use in the LaTeX file. Adjust the point size (pointsize=9) to get the text approximately the same size as that in the rest of the document.