Default behavior

The default is — not surprisingly — to create limits so that the data comfortably fit.

Base

Figure 1 shows the default limits. The command to do this is (almost):

plot(one2ten, one2ten)

The actual commands to create this figure and others are below in Appendix R.

Figure 1: Default base graphics plot.

ggplot2

The default here is basically the same, though the resulting picture looks rather different. Figure 2 is created with the commands:

require(ggplot2)
print(qplot(first, second, data=ggdat))

The require command makes sure that the package is loaded into the current R session. In this setting graphs are objects, and they are rendered only when they are printed — hence the call to print.

Figure 2: Default ggplot2 plot.

Typical limit

If the default isn’t what you want, you can change it. Here we want — for some reason — more room on the left of the plot.

Base

The simplified version of the command for Figure 3 is:

plot(one2ten, one2ten, xlim=c(-2,10))

Figure 3: Typical use of the xlim graphics parameter.

The examples here are on the x-axis. To control the y-axis, just substitute “y” for “x” — ylim rather than xlim.

ggplot2

In ggplot2 modifications or additions to a plot object are usually done by adding new terms:

print(qplot(first, second, data=ggdat) + xlim(-2, 10))

Figure 4: Typical ggplot2 specification of x limits.

Reverse direction

The first limit need not be the smallest — if not, then the axis is reversed.

Base

Figure 5: Base graph with the x-axis reversed.

ggplot2

The command for Figure 6 is:

print(qplot(first, second, data=ggdat) + xlim(10, 1))

Figure 6: ggplot2 with the x-axis reversed.

Expansion

The sharp-eyed will have noticed that the actual limits in the plots above are not what is specified. The specifications are strictly inside the plots. This makes it easy to make sure that no data is plotted on the boundary of the plot.

It is possible to change this behavior as well.

Base

We can see what the real range is by looking at the usr graphics parameter:

You can set some graphics parameters, and some are read-only. The usr parameter can be set, but you almost always want to just use it as is. The first two elements of usr are the x-axis limits, the last two are the y-axis limits.

The x-axis was asked to have limits 10 apart, and we can see that there is an extra 0.4 on each side.

You can force the limits to be taken literally by specifying xaxs (or yaxs for the y-axis):