One reason for the popularity of the R environment is its plotting capacities. However, this comes at the price of specifying a lot of parameters and arguments. In this section we learn about plotting with base R. As this topic is huge, we will only scratch the surface and review those functions which we believe are useful for a day-to-day workflow. If you are interesting in learning more on plotting with R we highly recommend the Paul Ross Murrell’s book R Graphics (2011). Further, we found the well structured Quick-R online tutorial by Robert I. Kabacoff very helpful and follow his structural approach.


Graphical Parameters

In R we customize graphical features of the graphs (fonts, colors, axes, titles) through graphic options. One way is to specify these options through the par() function. Setting parameter values via the par() function effects all plots for the rest of the session or until we change them again. The format is

par(optionname = value, optionname = value, ...)

Some use cases are given below:

par()                # view current settings
opar <- par()        # make a copy of current settings
par(col.lab = "red") # red x and y labels
par(opar)            # restore original settings

The par() function is very powerful and exhaustive. Type help(par) into your console for further information. In addition we recommend to bookmark the compilation of important arguments for the par() function provided by Paul Ross Murrell.

Another way to specify graphical parameters is by providing the optionname = value pairs directly to the plotting function. In this case, the options are only in effect for that specific graph. Always remember to check out the help page for a specific plotting function to determine which particular graphical parameters apply.

Text and Symbol Size

The following options can be used to control text and symbol size in graphs.

option description
cex number indicating the amount by which plotting text and symbols should be scaled relative to the default (=1)
cex.axis magnification of axis annotation relative to cex
cex.lab magnification of x and y labels relative to cex
cex.main magnification of titles relative to cex
cex.sub magnification of subtitles relative to cex

Plotting Symbols and Lines

Plotting symbols are specified using the pch argument, border color and fill color are specified with the argument col and bg, respectively. Lines can be changed using the lty and lwd argument.

option description
pch option to specify symbols to use when plotting points
lty line type (number 1-6)
lwd line width relative to the default (default=1). 2 is twice as wide

Colors

In R colors are specified either by index, name, hexadecimal, or RGB. Type colors() into the R console to return all available color names.

length(colors())
## [1] 657
## print the first 25 colornames
head(colors(), 25)
##  [1] "white"          "aliceblue"      "antiquewhite"   "antiquewhite1" 
##  [5] "antiquewhite2"  "antiquewhite3"  "antiquewhite4"  "aquamarine"    
##  [9] "aquamarine1"    "aquamarine2"    "aquamarine3"    "aquamarine4"   
## [13] "azure"          "azure1"         "azure2"         "azure3"        
## [17] "azure4"         "beige"          "bisque"         "bisque1"       
## [21] "bisque2"        "bisque3"        "bisque4"        "black"         
## [25] "blanchedalmond"

Further we can create a vector of n contiguous colors using the functions

Another highly recommended package for color schemes, particular for maps, is the RColorBrewer package. Go to the project webpage for further information.

Options that specify colors include the following.

option description
col default plotting color
col.axis color for axis annotation
col.lab color for x and y labels
col.main color for titles
col.sub color for subtitles
fg plot foreground color (axes, boxes)
bg plot background color

Fonts

We can specify font size and style with the following arguments.

option description
font Integer specifying font to use for text. 1=plain, 2=bold, 3=italic, 4=bold italic, 5=symbol
font.axis font for axis annotation
font.lab font for x and y labels
font.main font for titles
font.sub font for subtitles

Margins and Graph Size

Margin size is controlled using the following parameters.

option description
mar numerical vector indicating margin size in lines
mai numerical vector indicating margin size in inches
pin plot dimensions (width, height) in inches

To showcase the different parameters above we enhance the visualization of a simple sine plot with different amplitudes.

This is our baseline plot, without any modifications:

x  <- seq(-2*pi, 2*pi, length.out = 100)
y1 <- sin(x)
y2 <- sin(x)+0.5
y3 <- sin(x)-0.5
y4 <- 2*sin(x)
y5 <- 0.5*sin(x)

plot(x,y1)
points(x,y2)
points(x,y2)
points(x,y3)
points(x,y4)
points(x,y5)

Quite a mess!

Let us improve the plot.

# start with the first plot
plot(x, y1, 
     type = 'o',        # over plotted points and lines
     pch = 20,          # specify symbol
     col = 1,           # line color
     cex.axis = 0.75,   # magnification of axis annotation relative to 1 
     col.axis = "blue", # color for axis annotation 
     col.lab = "blue",  # color for x and y labels
     fg = "blue",       # axis color 
     font.axis = 3      # italic font style
     )

# for the sake of writing less code we construct a list object
# with the different y values as members
y <- list(y2,y3,y4,y5)

## loop through the list y
for (i in 1:4){
  lines(x, y[[i]],   # pick one y after the other
        lty = i+1,   # set line type to 2,3,4,5
        lwd = i+1,   # set line width to 2,3,4,5
        col = i+1    # set line color to 2,3,4,5
        )
}