Log-log Plots

Creating log-log plots in R is easy; simply add log = "xy" as a named parameter to the call to plot().

The core of this example, creating the grid lines, was posted to the R-help list by Petr Pikal.

##  Create a data frame with artificial data.

d.f <- data.frame( x = 1:100, y = 1:100 )

##  Open a new default device.

get( getOption( "device" ) )()

##  Plot the data, hiding the points for now to prevent the calls to
##  abline() from drawing over the points.

plot( y ~ x, data = d.f, type = "n", log = "xy", main = "Log-log Plot" )

##  Put grid lines on the plot, using a light blue color ("lightsteelblue2").

abline( h = seq( 0, 100, 10 ), lty = 3, col = colors()[ 440 ] )
abline( v = seq( 0, 100, 10 ), lty = 3, col = colors()[ 440 ] )

##  Draw the points over the grid lines.

points( y ~ x, data = d.f )

##  Redraw the plot box over the grid lines.

box()

The output is:

Log-log plot

Log-log plot

Here’s another version, with more grid lines:

##  Create a data frame with artificial data.

x <- 1:31
y <- x^2
d.f <- data.frame( x = x, y = y )

##  Open a new default device.

get( getOption( "device" ) )()

##  Plot the data, hiding the points for now to prevent the calls to
##  abline() from drawing over the points.

plot(
    y ~ x,
    data = d.f,
    type = "n",
    log  = "xy",
    main = "Log-log Plot",
    xlim = c( 1, 1000 ),
    ylim = c( 1, 1000 ) )

##  Put grid lines on the plot, using a light blue color ("lightsteelblue2").

abline(
    h   = c( seq( 1, 9, 1 ), seq( 10, 90, 10 ), seq( 100, 1000, 100 ) ),
    lty = 3,
    col = colors()[ 440 ] )

abline(
    v   = c( seq( 1, 9, 1 ), seq( 10, 90, 10 ), seq( 100, 1000, 100 ) ),
    lty = 3,
    col = colors()[ 440 ] )

##  Draw the points over the grid lines.

points( y ~ x, data = d.f )

##  Redraw the plot box over the grid lines.

box()

The output is:

Log-log plot

Log-log plot