Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Positioning axes labels

Tags:

plot

r

axes

People also ask

What does labeling the axes mean?

Axis labels are text that mark major divisions on a chart. Category axis labels show category names; value axis labels show values.

What label goes on which axis?

The independent variable belongs on the x-axis (horizontal line) of the graph and the dependent variable belongs on the y-axis (vertical line).

What is the proper way to label axes?

The proper form for a graph title is "y-axis variable vs. x-axis variable." For example, if you were comparing the the amount of fertilizer to how much a plant grew, the amount of fertilizer would be the independent, or x-axis variable and the growth would be the dependent, or y-axis variable.


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()

enter image description here

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)

enter image description here

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)

enter image description here

Also note the las parameter that turns all tick labels horisontal, which makes them easier to read.