Plot Data

Aim of the Unit

This Unit aims to understand the graphical device and draw scatterplots.

Introduction

This tutorial will show how to draw plots like this:

What is needed to run this tutorial is the following dataset:

x <- seq(from = 0, to = 10, by = 1)
y1 <- x^2 - 3*x
y2 <- x^2 - 12*x
y3 <- x^2 - 24*x
y4 <- x^2 - 36*x
Y<-cbind(y1, y2, y3, y4)

The x-axis is the indipendent variable:

##  [1]  0  1  2  3  4  5  6  7  8  9 10

whereas, the y-axis is composed by four variables:

##       y1  y2   y3   y4
##  [1,]  0   0    0    0
##  [2,] -2 -11  -23  -35
##  [3,] -2 -20  -44  -68
##  [4,]  0 -27  -63  -99
##  [5,]  4 -32  -80 -128
##  [6,] 10 -35  -95 -155
##  [7,] 18 -36 -108 -180
##  [8,] 28 -35 -119 -203
##  [9,] 40 -32 -128 -224
## [10,] 54 -27 -135 -243
## [11,] 70 -20 -140 -260

Overall strategy

To draw a high-quality figure you need to set:

  1. Figure size
  2. Margins
  3. Box plot
  4. Points
  5. Lines
  6. Axis
  7. Lables
  8. Thicks
  9. Titles
  10. Legend
  11. Text expressions
  12. Save

Margins and figure size

R is able to manage complex scientific figures like scatterplot, box-plot or time series. As a first step, a few graphical parameters should be set by the function par().

The function par() should be called at the beginning of your script. With par(), you can define a number of graphical parameters. These are defined globally. Some of the most frequently used graphical parameters are used for two reasons:

  1. Setting dimensions
  2. Setting margins
Setting Function Description
Dimension pty = "s" Set a squared plot
fin = c(3.35, 3.35) Set the dimension of the figure (in inches)
Margin oma = c(3,4,1,1) Set the outer margin (in lines)
mar = c(3,4,1,1) Set the inner margin (in lines)
mgp = c(0,0.3,0) Set the margin for the axis title, axis labels and axis line

Such parameters should be defined with a synthax like this:

par(pty   = "s",
    fin   = c(3.35,3.35),
    oma   = rep(0,4),
    mar   = c(3,4,1,1),      
    mgp   = c(0, 0.3, 0)
    )

A detailed explaination of the meaning of each graphical parameters is offere in the help file of R. Just type ?par() on your R Console.

In the following sections, two of the most important graphical parameters will be described, respectively:

  1. Dimensions
  2. Margins

Set figure size

The first graphical parameter to set is the dimension of the resulting figure. The size of the current graphical region is given by typing in the R Console the following code:

dev.size("in")
## [1] 7 5

To set new dimension of the resulting graph, use the function par(fin = c("width", "height")):

par(fin   = c(3.35,3.35))    # THIS FIXES THE DIMENSION OF THE PLOT

Often, the dimension of the plot should have a width of half column of the paper. Typically, a paper has a width of 170 mm (or 6.7 inches). This means that the dimension of a one-column graph should not exceed 85 mm (or 3.35 inches).

Margins

Among the graphcal parameters defined above, the most important are those that set the margins of the plot. Two graphical functions used for this purpose are the following:

Function Description
oma() Set the size of the outer margins in lines of text.
mar() Set the number of lines of margin on the four sides of the plot.

To better understand the meaning of these two functions, look at the following R graphical model:

To examplify the use of oma() and mar() functions, check out this code:

par(oma   = c(2,2,2,2) + 0.1) # Set 3 lines of space between the graphical device and the figure  
par(mar   = c(3,3,3,3) + 0.1) # Set space between the figure and the plot
plot(x,y1)                    # Simple plot function
box(which = "plot" , lwd = 2) # Draw a box around the plot
box(which = "inner", lwd = 2) # Draw a box around the figure
box(which = "outer", lwd = 2) # Draw a plot around the device

that gives:

Often, there is no need to account for outer margin. Thus, just set:

par(oma   = c(0,0,0,0)) # Set 3 lines of space between the graphical device and the figure  

The margin around the plot region should be enough to account for the axis title and tick labels. Gneral setting values are the following:

par(mar   = c(3,3,0,0) + 0.2) # Set space between the figure and the plot

These settings are invisible. Their effect will become visible later, when you will add to the plot other graphical elements, such as axis, box and lines. Now, red lines have been added in the following plot just to the the effect of oma and mar parameters:

The two red boxes are just useful to observe the effect of the oma() and mar() parameters.

Second step: draw an empty plot

The next step is to initiate the grahical region. This is possible by drawing an empty plot with the function plot(type = "n"):

plot(0:10,0:10, # X AND Y DATA
     type = "n",# SPECIFY AN EMPTY PLOT
     axes = F,  # SPECIFY NO AXIS
     xlab = "", # SPECIFY NO AXIS LABEL
     ylab = ""  # SPECIFY NO AXIS LABEL
     )
# DRAW A CONTOUR LINE TO SEE THE DEVICE REGION
box(which = "outer", col = "red", lty = 3, lwd = 1)

The red box is used to observe the device region.

Third step: draw a box around the plot region

The plot region can be defined by the function box():

box(which = "plot")

The function box() can control several graphical parameters (type ?box() in the R Console). Some of the most useful are the following:

Parameter of box() Description
which = "plot" Set which box to draw between “plot”, “figure”, “inner” or “outer”
col = "black" Set the line color
lwd = 1 Set the line width
lty = 1 Set the line type (i.e. 1=solid (default), 2=dashed, 3=dotted, 4=dotdash, 5=longdash, 6=twodash)

The following code examplifies the use of the graphical parameters of the box() function:

# SET THE DIMENSIONS AND MARGINS
par(oma = c(0,0,0,0)) 
par(mar = c(3,3,0,0) + 0.2)
# DRAW AN EMPTY PLOT
plot(0:10,0:10,     # SET THE RANGE VALUES OF THE X and Y AXIS 
     type = "n",    # HIDE POINTS OR LINES
     axes = F,      # HIDE AXIS
     xlab = "",     # HIDE X-AXIS TITLE
     ylab = "")     # HIDE Y-AXIS TITLE
# DRAW A BOX AROUND THE PLOT
box(which="plot",   # DRAW A BOX
    lwd = 2)        # SET LINE WIDTH
# (OPTIONAL) DRAW A BOX AROUND THE DEVICE REGION
box(which="outer", col="red", lwd=1, lty=3)

Line-width

The line width of the box can be modified easily with the parameter lwd = ...

This parameter can be applied to box or axis:

Line-type

There are six different line types. The line of the box can be modified easily with the parameter lty = ...:

This parameter can be easily applied to other graphical objects, such as box or axis:

Line-color

The color of the line is easily defined with the parameter col= "..."):

This parameter can be applied to lines, axis or box:

Forth step (a): draw points

Points can be added directly with the function plot() or matplot() or, in case a plot() function was already called, with the function points(). The graphical parameters for points symbols are the following:

Parameter Description
pch = 21 Set the symbol to be used (i.e. 21:25)
col = "red" Set the color border of the symbol
bg = "white" Set the color background for the symbol
cex = 1 Set the dimension for the symbol
lwd = 2 Set the line width for the symbol

It follows an example:

par(oma = c(0, 0, 0, 0)) 
par(mar = c(3, 3, 0, 0) + 0.2) 
# DRAW AN EMPTY PLOT
plot(x,y1,
     type = "n",
     axes = F,
     xlab = "",
     ylab = "")
# DRAW BOX
box(which="plot", lwd = 2, lty = 1)
# DRAW POINTS
points(x,y1)
# DRAW A BOX AROUND THE DEVICE REGION
box(which="outer", col="red", lwd=1, lty=3)

Different points symbols can be selected by the parameter pch = ...:

Points can be drawn of any size (cex = ...) and color (col = "..."):

Points from pch = 21:25 can be filled of any background color by the function bg = "..."

and by any border color by the function col = "...":

Border thickness of symbols can be varied of any size by the function lwd = ...:

Forth step (b): draw lines

Once the empty plot is set and the box around the plot drawn, then, lines can be added with the function lines(). Some of the most common graphical parameters for lines are the following:

Parameter Description
lty = 1 Set six different line type (from 1 to 6)
lwd = 2 Set the line width
col = "red" Set the line color

It follows an example with points and lines together:

par(oma = c(0, 0, 0, 0)) 
par(mar = c(3, 3, 0, 0) + 0.2) 
# DRAW AN EMPTY PLOT
plot(x,y1,
     type = "n",  # EMPTY PLOT
     axes = F,    # NO AXIS LINE
     xlab = "",   # NO X-TICK LABELS
     ylab = "")   # NO Y-TICK LABELS
# DRAW BOX
box(which="plot", lwd = 2, lty = 1)
# DRAW POINTS AND LINES
points(x,y1)
lines(x,y1)
# DRAW A BOX AROUND THE DEVICE REGION
box(which="outer", col="red", lwd=1, lty=3)

As described previously, lines can be drawn with different color (col = ...) and thickness (lwd = ...):

Fifth step (a): axis

The function axis() is used to add the axis to the current plot. This ia very rich function, which can specify several parameters, like side, label orientation, tickmarks, colors, etc.

Just as a first example, the following axis parameters:

axis(side = 2,           # SPECIFY THE Y AXIS
     las  = 1,           # ORIENTATION OF AXIS LABELS
     lwd  = 2,           # LINE WIDTH
     col  = "darkred")   # COLOR OF POINTS LINE

lead to this result:

par(oma = c(0, 0, 0, 0)) 
par(mar = c(3, 3, 0, 0) + 0.2) 
par(mgp = c(0, 0.3, 0)) 
# DRAW AN EMPTY PLOT
plot(x,y1,
     type = "n",
     axes = F,
     xlab = "",
     ylab = "")
# DRAW BOX
box(which="plot", lwd = 2, lty = 1)
# DRAW POINTS
points(x,y1)
# DRAW AXIS
axis(side = 1,               # SPECIFY THE Y AXIS
     las      = 1,           # ORIENTATION OF AXIS LABELS
     xaxp     = c(0, 10, 2), # NUMBER OF TICKMARKS
     tck      = 0.03,        # TICK LENGTH
     lwd      = 2)           # LINE WIDTH
axis(side = 2,               # SPECIFY THE Y AXIS
     las      = 1,           # ORIENTATION OF AXIS LABELS
     yaxp     = c(-10, 80, 6),
     tck      = 0.03,         # TICK LENGTH
     lwd      = 2)           # LINE WIDTH
# (OPTIONAL) DRAW A BOX AROUND THE DEVICE REGION
box(which = "outer", col = "red", lwd = 1, lty = 3)

In the above graph, the function axis() has added the following graphical objects:

  1. Line
  2. Thicks
  3. Labels

Axis lines

The function axis() can finely control the outcomes of the axis line by setting the following parameters:

Graphical object Parameter Description
Axis line side = 1 Set at which side (i.e. 1, 2, 3 or 4 side) the axis is drawn
line = 1 Set the margin in lines between the axis and plot margin
lwd = 1 Set the line width
tpy = 1 Set the line type
col = "black" Set the line color
labels = FALSE Suppress tick labels
xaxt = "n" Suppress axis plotting
yaxt = "n" Suppress axis plotting

In addition, there is the following parameter that is defined by the function par():

Parameter of par() Description
mgp = c(3,1,0) Set the margin for the axis title, axis labels and axis line.

With the function axis(), you can define at which side, the axis should be shown on the plot. This is specified with the parameter side = ..., followed by the number 1-bottom, 2-left, 3-top or 4-right.

By default, the axis line is positioned with the box around the plot. However, you can shift the line as you like by the parameter line = .... Positive values shift the axis-line outside the plot. Negative values shift the axis line inside the plot region. See example below:

Within the function axis(), it is possible to show only the axis line (without labels labels = F and ticks ticks.lwd = 0):

or it is possible to show the axis line with labels labels = T but without ticks (ticks.lwd = 0):

or it is possible to show the axis line with ticks (lwd.ticks = 2) but without labels labels = F :

or, finally, it is possible to hide the axis line (lwd = 0) and show only ticks (lwd.ticks = 2) and labels labels = T :

The color and the thickness of the axis line are set, as previously shown, by the parameters col = ... and lwd = ....

Fifth step (b): thickmarks

The function axis() can finely control the outcomes of thickmarks by setting the following parameters:

Graphical object Parameter Description
Tickmarks tck = 0.03 Set the length of the tickmarks. The value is a fraction of the axis length
lwd.ticks = 1 Set the width of the tickmarks
col.ticks = "red" Set the color of the tickmarks
tick = FALSE Suppress tickmarks
at = seq(from=1,to=5,length.out = 5) Set the number of tickmarks
yaxp = c(0,10,3) A vector like c(y1, y2, n) that set the coordinates of the extreme tick marks and the number of intervals between tick-marks
xaxp = c(0,10,3) As yaxp but for the x-axis.

The length of ticks can be set by the functions tck():

The number of ticks can be set by the parameter xaxp = c("min", "max", "n"), where n is the number of intervals. For instance, it is possible to place 10 ticks in the range between 0 to 10 with the code:

axis(side = 1, xaxp = c(0,10,10))

or just three ticks (two inervals) in the same range:

axis(side = 1, xaxp = c(0,10,2))

or five ticks (four intervals) in the same range:

axis(side = 1, xaxp = c(0,10,4))

The function axis() allows also to define the width of the tickmarks by the parameter lwd.ticks = ...:

The color of the ticks can be individually set by the parameter col.ticks = "...":

Fifth step (c): labels

The apearence of the labels placed behind the tickmarks can be finely controlled. The following parameters set the direction, color, dimension and font of the labels:

Graphical object Parameter Description
Tick labels las = 1 tickmark direction (0: parallel to the axis [default] 1: horizontal, 2: perpendicular, 3: vertical to the axis)
col.axis = "red" Set the color of the labels
cex.axis = 1 Set the dimension of the labels
font.axis = 1 Set the font of the labels
par(mgp = c(...)) Set the margin of the axis title, labels and line

The direction of the labels is set by the parameter las = .... The values are:

For scientific figure, the most common setting is las = 1, like in the following example:

Labels can have any colors thanks to the parameter col.axis = "":

Labels can have any size. Typically, its size is slighly lower than the main axis title. The size of the label can be set by the parameter cex.axis = 1:

Labels font can be set by the parameter font.axis = 1:

Finally, it is possible to control the positioning of the labels by the parameter mgp = c("axis title", "tick labels", "axis line"). The values are line units. Since the axis line and the axis title have been hidden, the only parameter of importance is the second one. Thus, a typical overall setting of the labels is the following:

par(mgp = c(0, 0.3, 0))

However, there is also the parameter hadj = ... and padj = ... that shift, respectively, horizontally and perpendicularly the position of the labels. The example below was drawn by setting padj = ...:

Sixth step: axis titles

In place of the parameter xlab = ... or ylab = ... of the function plot(), it is possible to have more control on the output if we use the function mtext(). This function write some text in one of the four margins of the current figure region or one of the outer margins of the device region.

When an empty plot is set plot(type = "n"), remeber to specify also axes = F, like in the following example:

mtext("This is a title", 
      side    = 2,           # X-AXIS  
      las     = 0,           # TEXT ORIENTATION
      line    = 2,           # MARGIN BETWEEN TEXT AND AXIS LINE
      col     = "darkred",   # COLOR OF TEXT
      cex     = 1.2)         # DIMENSION OF TEXT

The use of the function mtext() allows a great control on the output. In the following examples, the parameter line = ... has been varied in order to move the text along the plot:

The marginal text can be positioned in each of the four axis of the plot region with the parameter side = ... and controlled its direction with the parameter las = .... Both parameters have been already shown before.

Ninth step: draw legends

Tenth step: write text or mathematical expression

Eleventh step: save and export the graph

OTHER PLOTS

EXAMPLES

To set the color only of the plot region, it is possible to use the function rect(). In this case, you have to draw first the empy box with plot(type = "n"), followed by the definition of a colored rect(col = "..."). After that, you can plot the data with a line() or point() function. To use the rect() function read the help page of R. To place the rectangle exactly to overlay the plot, use the par("usr")[...] coordinates, like in the following example:

# SET THE DIMENSIONS AND MARGINS
par(fin   = c(3.3,3.3))      # THIS FIXES THE DIMENSION OF THE PLOT
par(oma   = c(0,0,0,0))      # MARGIN OF THE DEVICE
par(mar   = c(5,5,2,0)+0.1)  # MARGIN AROUND THE PLOT
par(pty   = "s")             # SET A SQUARED PLOT REGION
# MAKE AN EMPTY PLOT
plot(x, y1, 
     ann = TRUE,
     axes = TRUE,
     type = "n",
     main = "Overal title",
     col.main = "dark red",
     xlab = "Title for the x-axis",
     ylab = "Title for the y-axis"
     )
# FILL THE EMPTY PLOT WITH A COLORED RECTANGLE
rect(par("usr")[1], 
     par("usr")[3], 
     par("usr")[2], 
     par("usr")[4], 
     col = "light grey")
# PLOT THE POINTS
points(x, y1, 
     type = "b",
     col = "dark red",
     lwd = 2
     )
# DRAW A RED CONTOUR OF THE DEVICE REGION
box(which="outer", col="red", lwd=2)

Multiple axis

Plot with multiple axis is possible by using the function par(new=T) and plotting as many graphs as the number of axis you desire. It follows an example.

########## MARGINS #####################
par(mar=c(3, 10, 0, 0) + 0.2,
    oma=c(0, 0, 0, 0),
    pty = "s"
    )
########## EMPTY PLOT #####################
plot(x, y1, 
     type  = "n",
     axes  = F, 
     main  = "",
     xlab  = "", 
     ylab  = "",
     xlim  = c(min(x), max(x)) ,
     ylim  = c(min(y1), max(y1)))
box(which="plot",   col= "black", lwd  = 2)
########## FIRST AXIS #####################
lines(x, y1, 
     axes = F, 
     xlab = "", 
     ylab = "", 
     type = "l",
     lty  = 2, 
     main = "",
     lwd  = 2)
axis(side  = 2, 
     col   = "black",
     lwd   = 2)
points(x,y1,
       pch = 20,
       col = "black",
       cex     = 1.5)
mtext(2,
      text = "Axis title 1",
      font = 2,
      line = 2)
########## SECOND EMPTY PLOT #####################
par(new=T)
plot(x, y2, 
     type  = "n",
     axes  = F, 
     main  = "",
     xlab  = "", 
     ylab  = "",
     xlim  = c(min(x), max(x)) ,
     ylim  = c(min(y2), max(y2)) ,
     col   = "black")
box(which="plot",   col= "black", lwd  = 2)
########## SECOND AXIS #####################
lines(x, y2, 
     axes = F, 
     xlab = "", 
     ylab = "", 
     type = "l",
     lty  = 2, 
     main = "",
     col   = "darkred",
     lwd  = 2)
axis(side  = 2, 
     col   = "darkred",
     col.axis  = "darkred",
     lwd   = 2,
     line  = 3.5)
points(x,y2,
       pch = 20,
       col = "darkred",
       cex = 1.5)
mtext(2,
      text = "Axis title 2",
      font = 2,
      col  = "darkred",
      line = 5.5)
########## THIRD EMPTY PLOT #####################
par(new=T)
plot(x, y3, 
     type  = "n",
     axes  = F, 
     main  = "",
     xlab  = "", 
     ylab  = "",
     xlim  = c(min(x), max(x)) ,
     ylim  = c(min(y3), max(y3)))
box(which="plot",   col= "black", lwd  = 2)
########## THIRD AXIS #####################
lines(x, y3, 
     axes = F, 
     xlab = "", 
     ylab = "", 
     type = "l",
     lty  = 2, 
     main = "",
     col   = "darkgreen",
     lwd   = 2)
axis(side      = 2, 
     col       = "darkgreen",
     col.axis  = "darkgreen",
     lwd       = 2,
     line      = 7)
points(x,y3,
       pch     = 20,
       col     = "darkgreen",
       cex     = 1.5)
mtext(side     = 2,
      text     = "Axis title 3",
      font     = 2,
      col      = "darkgreen",
      line = 9)
###### X AXIS ##################################
axis(side = 1, 
     xlim = c(min(x), max(x)),
     lwd  = 2,
     col.axis = "blue4",
     line = 0)
mtext("X - axis title",
      side = 1,
      font = 2,
      col  = "blue4",
      line = 2)
box(which = "outer", col = "red", lwd = 2)

Multicolor Tickmark

The function axis() allows to define the color of specific tickmarks and labels. This can be achieved by calling several axis() functions, like in the example below:

######### GRAPHICAL PARAMETERS #########################################
par(oma   = rep(0,4),         # DEVICE REGION AROUND THE FIGURE IN LINES
    mar   = c(3,3,0,0) + 0.2, # FIGURE REGION AROUND THE PLOT IN LINES
    mfrow = c(1,1),
    lwd   = 2,
    mgp   = c(1,0.3,0)        # MARGIN FOR THE AXIS TITLE, LABELS AND LINE
    )     
######### EMPTY PLOT #########################################
matplot(0:10,0:10,
        type = "n",
        axes = F,
        xlab = "",
        ylab = "")
######### BOX  #########################################
box(which="plot",   col= "black", lty  = 1)
box(which="outer",  col= "red",   lty  = 1)
######### AXIS #########################################
axis(side       = 1, 
     lwd        = 2,
     tck        = 0.03,        
     at         = c(1,4,7), 
     col.ticks  = "darkgreen", 
     lwd.ticks  = 2, 
     col.axis   = "darkgreen")
axis(side       = 1, 
     lwd        = 2,
     tck        = 0.03,        
     at         = c(2,5,8), 
     col.ticks  = "darkred", 
     lwd.ticks  = 2, 
     col.axis   = "darkred")
axis(side       = 1, 
     lwd        = 2,
     tck        = 0.03,        
     at         = c(3,6,9), 
     col.ticks  = "blue4", 
     lwd.ticks  = 2, 
     col.axis   = "blue4")

Fill the box (optional) with a background color

The background color of the device region can be defined with the parameter par(bg = "..."):

par(bg = "blue")

To draw a box with a blue background color, as before:

  1. set the margins
  2. draw an empty plot
  3. fill the device region with the blue color

The code is examplied below:

# SET THE DIMENSIONS AND MARGINS
par(oma = c(0,0,0,0)) 
par(mar = c(3,3,0,0) + 0.2)
# SET THE COLOR BACKGROUND
par(bg = "blue4") 
# DRAW AN EMPTY PLOT
plot(0:10,0:10, # X AND Y DATA
     type = "n",# SPECIFY AN EMPTY PLOT
     axes = F,  # SPECIFY NO AXIS
     xlab = "", # SPECIFY NO AXIS LABEL
     ylab = ""  # SPECIFY NO AXIS LABEL
     )
# DRAW A BOX AROUND THE PLOT
box(which = "plot", 
    col = "white", 
    lwd = 2)
# (OPTIONAL) DRAW A BOX AROUND THE DEVICE REGION
box(which = "outer", col = "red", lwd = 2)

COLORED BOXES BY RECT()

It follows a series of examples: