Positioning axes labels
Ploting axes on the right and top sides of a plot
By default R will plot the x-axis below the plot area and the y-axis to the left of it. You can change this behaviour in this way:
plot(1:100, cumsum(rnorm(100)), type="l", axes=FALSE) # Do not plot any axes
axis(3) # Draw the x-axis above the plot area
axis(4) # Draw the y-axis to the right of the plot area
box()
To also move the labels you set ann=FALSE
or xlab="", ylab=""
and add them afterwards with mtext
, where side=1
is bottom, 2 is left, 3 is top, 4 is right. line
controls the distance from the plot area.
plot(1:100, cumsum(rnorm(100)), type="l", axes=FALSE, ann=FALSE)
axis(3)
box()
mtext("Top axis", side=3, line=3)
Changing distance between labels, ticks and plot area.
Use the mgp
parameter to control these details, either before the call to plot
, like this
par(mgp=c(axis.title.position, axis.label.position, axis.line.position))
or in the plot
command itself, like this
plot(1:100, cumsum(rnorm(100)), type="l", mgp=c(2,1,.5), las=1)
Also note the las
parameter that turns all tick labels horisontal, which makes them easier to read.