[R] Force labelling of x-axis

Marc Schwartz MSchwartz at MedAnalytics.com
Mon Mar 21 14:58:31 CET 2005


On Sun, 2005-03-20 at 23:15 -0500, Bill Kranec wrote:
> Hi,
> 
> I'm trying to do a box-whisker plot of two columns of a data frame, a
> list of category names in one column vs. some numerical values in the
> other.  The plot itself works fine, but only a few points of the x-axis
> ( the category names ) are labelled.  I think that this is because the
> category names are too long.
> 
> Is there any way to force R to label each x-axis value, preferably at a
> 45-degree slant so that each one can be seen?  I feel like this should be
> pretty easy to do, but I can't find anything obvious from the R-manual.

Bill, there have been a couple of other suggestions, but I'll throw in
my $0.02 here:

Without a specific example it is hard to know which way to recommend to
you, but a couple of possibilities if you are using R's base graphics:

1. Reduce the font size of the labels by using 'cex.axis' as an argument
in your call to boxplot(). The default is 1, but you may be able to
reduce it to something that gets your labels printed and still be
readable.

Here is an example:

group <- sample(c("Long Label 1", "Long Label 2", "Long Label 3"), 
                40, replace = TRUE)
N <- rnorm(40)
df <- data.frame(group, N)

boxplot(N ~ Group, data = df)

# Now reduce the size of the labels
boxplot(N ~ group, data = df, cex.axis = 0.75)



2. You can split the labels on two lines by using a "\n" in the labels:

boxplot(N ~ group, data = df, xaxt = "n")
mtext(1, at = 1:3, 
      text = c("Long\nLabel 1", "Long\nLabel 2", "Long\nLabel 3"),
      line = 2)

If you want to reduce the font size in the above use 'cex = ...' in the
call to mtext().



3. If neither of the above (or a combination of the two) helps, there is
a FAQ (7.27) that provides an example of how to rotate axis labels at:

http://cran.r-project.org/doc/FAQ/R-FAQ.html#How-can-I-create-rotated-
axis-labels_003f

Beware of line wrapping in the above URL.

HTH,

Marc Schwartz




More information about the R-help mailing list