x <- seq(-2,2,length=1000)
y <- x^3-2*x
plot(x,y)
Steve Simon
January 1, 2014
I want to start with a simple artificial example, a cubic function. With this simple function, you will see different ways of displaying information on the x and y axes.
Figure 1. Plot of a simple cubic function.
This is a classic cubic function with a local minimum at sqrt(2/3), local maximum at -sqrt(2/3) and zeros at -sqrt(2), 0, and +sqrt(2). Let’s draw the tick marks at these locations instead.
xticks <- c(-sqrt(2),-sqrt(2/3),0,sqrt(2/3),sqrt(2))
plot(x,y,axes=FALSE)
axis(side=2)
axis(side=1,at=xticks)
Figure 2. Plot with different tick locations.
Let’s make the labels fit better.
Figure 3. Plot with rounded values at tick marks.
If you like, you can use math expressions on your axes
plot(x,y,axes=FALSE)
xlabs <- c(expression(-sqrt(2)),expression(-sqrt(2/3)),expression(0),expression(sqrt(2/3)),expression(+sqrt(2)))
axis(side=2)
axis(side=1,at=xticks,labels=xlabs)
Figure 4. Plot with math symbols
You can have the axes cross at the middle. When you do, it no longer makes sense to have a tick mark at zero.
xticks <- c(-sqrt(2),-sqrt(2/3),sqrt(2/3),sqrt(2))
xlabs <- c(expression(-sqrt(2)),expression(-sqrt(2/3)),expression(sqrt(2/3)),expression(+sqrt(2)))
plot(x,y,axes=FALSE)
axis(side=2,pos=0,at=c(-4,-2,2,4))
axis(side=1,at=xticks,labels=xlabs,pos=0)
Figure 5. Plot with crossed axes.
One important axis option is set in the plot function itself. You are able to put different labels on the x and y axes with the xlab and ylab arguments.
Figure 6. Plot with axis labels
Finally, there are several options to control the display of axes that are found in the par function. Here are a couple of the most important ones.
The las argument in par changes the orientation of the tick mark labels.
# las=0 is the default
par(mfrow=c(2,2))
par(las=0)
plot(x,y)
title("las=0: Both parallel")
par(las=1)
plot(x,y)
title("las=1: Both horizontal")
par(las=2)
plot(x,y)
title("las=2: Both perpendicular")
par(las=3)
plot(x,y)
title("las=3: Both vertical")
Figure 7. Plots with different tick label orientations
The pty argumet in par can also force the plot to be square.
Figure 8. Plot with square dimensions
When you combine this with equal axis limits, it creates a graph where the line of unity (y=x) has a 45 degree slope. This is helpful when comparing two measurements on the same units.
Figure 9. Plot with square dimensions and equal scales