[R] barplot(): X-Axis Labels

Marc Schwartz mschwartz at medanalytics.com
Mon Oct 14 18:51:30 CEST 2002


> -----Original Message-----
> Martin Hoyle wrote:
>
> Try using las=2 in the barplot command;
> 
> barplot(response variable means,names=levels(explanatory
> variable),las=2)
> 
> The text is then at 90 degrees,
>
> >>> Jess Balint <jbalinc at insight.rr.com> 10/13/02 10:47PM >>>
>
> Hello all. I have a simple barplot with sixteen different segments.
> When I plot my data, only five or six of the labels are showing in the
> x-axis. How do go get them all to show? Can I set them at a 45.degree
> angle? Thank you.
> 
> Jess

In addition to Martin's suggestion, you might wish to use meaningful
abbreviations to shorten the labels.

You can also use par("cex.axis") to make the font a bit smaller.  You'll
need to draw the x and y axis separately, lest both fonts be small.
Example:

mp <- barplot(1:16, axes = FALSE, axisnames = FALSE)
axis(2)
axis(1, at = mp, labels = 1:16, cex.axis = 0.5)

You can play around with the value of par("cex.axis") in the 3rd line to
see what size may look good.


If you do want 45 degree x axis labels, one approach is using a
suggestion from Uwe Ligges that I found in a prior post (though, with
slight modification):

labels <- paste("This is bar #", 1:16, sep ="")
mp <- barplot(1:16, axes = FALSE, axisnames = FALSE)
text(mp, par("usr")[3], labels = labels, srt = 45, adj = 1, xpd = TRUE)
axis(2)


You may need to play around with the mp and par("usr")[3] arguments to
text() to get the labels positioned the way you wish, but this should
work.

For example, if you want to draw the actual x axis line and tick marks,
you'll need to shift the rotated x axis labels downward:

labels <- paste("This is bar #", 1:16, sep ="")
mp <- barplot(1:16, axes = FALSE, axisnames = FALSE)
text(mp, par("usr")[3] - 0.5, labels = labels, srt = 45, adj = 1, xpd =
TRUE)
axis(1, at = mp, labels = FALSE)
axis(2)


Finally, you can also combine my other suggestion of reducing the font
size by setting "cex" in the call to text() as follows:

labels <- paste("This is bar #", 1:16, sep ="")
mp <- barplot(1:16, axes = FALSE, axisnames = FALSE)
text(mp, par("usr")[3], labels = labels, srt = 45, adj = 1, cex = 0.5,
xpd = TRUE)
axis(2)

HTH.

Marc Schwartz



-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list