The default axis settings are often not adequate to deal with all kinds of data. For example, we might wish to change the number of tick marks along an axis or change the orientation of the annotations if they are too long in order to fit them horizontally. In this recipe, we will cover some settings that can be used to customize axes as per our requirements.
All you need to try out in this recipe is to run R and type the recipe in the command prompt. You can also choose to save the recipe as a script so that you can use it again later on.
We can set the xaxp
and yaxp
arguments with the par()
command in order to specify coordinates of the extreme tick marks and the number of intervals between tick marks in the c(min,max,n)
form:
plot(rnorm(100),xaxp=c(0,100,10))
When xaxp
or yaxp
is not specified, R automatically calculates the number of tick marks and their values. By default, R extends the axis limits by adding 4 percent at each end and then draws an axis that fits within the extended range. This means that even if we set the axis limits using xlim
or ylim
, the graph corners don't exactly correspond with these values. To make sure they do, we need to change the axis style using the xaxs
argument, which takes one of the two possible values: r
(the regular or default) and i
(internal). We need to set xaxs
to i
.
A vector of the c(x1,
x2,
n)
form gives the coordinates of the extreme tick marks and the number of intervals between tick marks.
To change the orientation of axis value annotations, we need to set the las
argument of the par()
command. This takes one of the four possible numeric values:
0
: This is always parallel to the axis (the default)1
: This is always horizontal2
: This is always perpendicular to the axis3
: This is always verticalWe can also use the axis()
command to create a custom axis by specifying a number of arguments. The basic arguments are as follows:
side
: This takes numeric values (1
=below, 2
=left, 3
=above, and 4
=right)at
: This takes a vector of coordinates where tick marks are to be drawnlabels
: This takes a vector of tick mark annotationsWe can set the line width for the axis lines and the tick marks separately by passing the lwd
and lwd.ticks
arguments, respectively. Similarly, colors can be set using the col
and col.ticks
arguments.