Skip to content

barplot(): X-Axis Labels

5 messages · Martin Hoyle, Marc Schwartz, Jess Balint +1 more

#
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,

Martin.

Martin Hoyle,
School of Life and Environmental Sciences,
University of Nottingham,
University Park,
Nottingham,
NG7 2RD,
UK
Webpage: http://myprofile.cos.com/martinhoyle
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
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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 
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
On Mon, 14 Oct 2002 11:51:30 -0500
"Marc Schwartz" <mschwartz at medanalytics.com> wrote:

            
Thank you all very much for your assistance. One more issue I do have is creating a label for the x-axis. If I specificy xlab = "" within the barplot(), it get overwritten by the category labels. I tried to add one below that with text( locator( 1 ), "x-axis label" ), but it seems that it won't insert the text below a certain threshold. Is there a way around this?
Thanks again.

Jess
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
is
barplot(), it get
text(
text below a
Jess,

Normally when drawing text outside the plot region, which is defined by
the box() around the barplot in this case, you need to use mtext() and
not text().

The generation of the 45 degree rotated axis labels in the code I posted
earlier "tricks" the text() command in drawing the labels outside the
plot region.  The x and y coordinate values that text() uses are based
upon the plot region area coordinates.

Add the following line to the end of the prior example code:

mtext("x-axis label", line = 3, side = 1)

It will get overwritten by the axis labels, so you will need to adjust
the "line = " argument to move the x axis label vertically to a position
that makes sense with your real data. 

The "line" argument starts at 0 (zero) near the x axis itself and
increases as you approach the bottom limit of the open graphics window.
If you go too high in "line = ", it will be drawn outside (below) the
visible window.  Thus, you may have to adjust the margin boundaries for
the window to provide additional room below the plot region.  You can do
this by using par("mar") before the barplot is drawn.

HTH.

Marc



-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
>> -----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

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

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

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

    Marc> ....
    Marc> ....   {more nice explanations and examples}
    Marc> ....

Just a remark on the example above (and the ones not cited
here), particularly since I've seen other people do the same:

If it's just one axis annotation that you want to "fiddle" with
and the other drawn regularly, it's slightly ``nicer'' to use
{with the above}

  mp <- barplot(1:16, xaxt = "n", axisnames = FALSE)
  ##                  ~~~~~~~~~~
  axis(1, at = mp, labels = 1:16, cex.axis = 0.5)

instead of `` axes = FALSE '' and the extra ``axis(2)'' command.

Martin Maechler <maechler at stat.math.ethz.ch>	http://stat.ethz.ch/~maechler/
Seminar fuer Statistik, ETH-Zentrum  LEO C16	Leonhardstr. 27
ETH (Federal Inst. Technology)	8092 Zurich	SWITZERLAND
phone: x-41-1-632-3408		fax: ...-1228			<><
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._