Reputation: 6561
How can I centre the labels on the x-axis to match up with the bars? Also, how can I position the x axis label further down so it it is not obscured by the x-axis labels? Thanks!
par(mar= c(15,4,4,2) + 0.1)
barplot(58:1,xaxt="n",xlab="",ylab="Frequency", col=gray(5:0/5))
axis(1, labels=FALSE)
text(1:58, par("usr")[3] - 0.25, srt = 90, adj = 1,
labels = rep("Long Species Name",58), xpd = TRUE)
mtext(1, text = "Species", line=6)
Upvotes: 3
Views: 3463
Reputation: 174813
Check out the return value of barplot()
(by reading ?barplot
). There we find that the mid points of the bars are returned by the function as a vector. Hence it is a simple matter of assigning the returned object (here to object bar
) and then use that in a call to axis()
to locate the tick marks.
In the axis()
call, note that we specify both the labels
argument and the at
argument, with at
being set to the bar mid points as stored in bar
. las = 2
is used to rotate the labels relative to the axis, and cex.axis = 0.6
is used to reduce the label size.
The second part of your question is handled by title()
and the line
argument. First note that when you set the mar
parameter you are setting the margin size in "lines", hence the margin on side 1 (bottom) is 15 lines. The line
argument in title()
specifies which of the margin lines you want to draw the axis label.
Putting this altogether with a modified example we have:
op <- par(mar= c(15,4,4,2) + 0.1)
bar <- barplot(58:1, xaxt="n", xlab="", ylab="Frequency", col=gray(5:0/5))
axis(1, labels = paste("Long Species Name", 1:58), at = bar,
las = 2, cex.axis = 0.6)
title(xlab = "Species", line=11)
par(op)
Which produces:
Upvotes: 10