Skip to content
Prev 60610 / 398500 Next

barplot() options for intervals on axes

On Mon, 2004-12-06 at 15:30 +0100, Sebastien Moretti wrote:
The general process of customizing the annotation of the axes for a
variety of plots is posted frequently to this e-mail list, so a search
of the archive using "axis" as the keyword yields almost 2,000 hits.
Using "axis labels" narrows that to 650, which are more relevant.

The key is to inhibit the generation of the default y axis by using the
argument 'yaxt = "n"':

Compare:

barplot(1:5)

versus

barplot(1:5, yaxt = "n")


You can then use the axis() function to customize the y axis values:

barplot(1:5, yaxt = "n")
axis(2, at = seq(0, 5, 0.25), las = 1)

help("par") provides additional information on the graphic parameters,
which are the key to these types of customizations. See ?axis for more
information on that function as well.

To your second query, the key is to note that barplot() returns the bar
midpoints, which is referenced in the Value section of ?barplot.

Thus:

mp <- barplot(1:5, yaxt = "n", ylim = c(0, 6))
axis(2, at = seq(0, 5, 0.25), las = 1)
text(mp, 1:5, labels = 1:5, pos = 3)

See ?text for more information.

Also, note that I increased the range of the y axis here to make room
for the bar text labels (primarily the final tallest bar).

Finally, there is an article in the R Help Desk section of the October
2003 R News on basic graphic operations in R, which you might find
helpful. A direct link to it is:

http://cran.r-project.org/doc/Rnews/Rnews_2003-2.pdf

HTH,

Marc Schwartz