Layout of a Standard Plot

This script draws the layout of a standard plot. The basis of the code is Introductory Statistics with R, by Peter Dalgaard, pp. 27–28.

##  Use the default device.

get( getOption( "device" ) )()

##  Create data for plotting.

x <- runif( n = 50, min = 0.0, max = 2.0 )
y <- runif( n = 50, min = 0.0, max = 2.0 )

##  Set up the plot coordinates, but don't draw anything.

plot(
    x    = x,
    y    = y,
    xlim = c( 0, 2 ),
    ylim = c( 0, 2 ),
    type = "n",
    xlab = "",
    ylab = "",
    axes = FALSE )

##  Draw the points in a pale gray.

points( x = x, y = y, col = "gray70" )

##  Draw the axes and box.

axis( 1 )
axis( 2 )
box()

##  Draw the title, subtitle, and labels.

title( main = "Main title",
       sub  = "subtitle",
       xlab = "x-label",
       ylab = "y-label" )

##  Draw text in the margins.

for ( side in 1:4 ) {
    mtext( -1:4, side = side, at = 0.7, line = -1:5 )
}

##  Draw text inside the plot region.

mtext( paste( "side", 1:4 ), side = 1:4, line = -1, font = 2 )

The output is:

Layout of a standard plot

Layout of a standard plot