Axis labels are text that mark major divisions on a chart. Category axis labels show category names; value axis labels show values.
The independent variable belongs on the x-axis (horizontal line) of the graph and the dependent variable belongs on the y-axis (vertical line).
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.
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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With