[R] Fitting long names in boxplot

Gavin Simpson gavin.simpson at ucl.ac.uk
Sat Mar 1 11:06:47 CET 2008


On Fri, 2008-02-29 at 14:40 -0800, Alex Reynolds wrote:
> I have the following code:
> 
> subsetTimeDataPlot <- boxplot(subsetTimeData$time ~ 
> subsetTimeData$build, horizontal=True, col="lightblue", ...)
> 
> The 'names' component consists of strings that can be up to 20-30 
> characters long.
> 
> How would I go about drawing the names vertically (not the boxplot) or 
> shrinking the names text (I tried cex.names=0.7 but this didn't help).
> 
> Thanks for any advice, I apologize if these are stupid questions,

Look at ?par and argument/parameter 'las', which controls the
orientation of axis tick labels.

You'll also need to increase the margin size on the side of the plot
holding the labels if they are very long. For this, see ?par again and
parameters 'mar' or 'mai' depending on how you wish to define the margin
size.

## Example
dat <- data.frame(values = rnorm(100), group = gl(2, 50))
levels(dat$group) <- c("reallyreallylonglabel", 
                       "anevenlooooooooooooongerlabel")
## las = 1 gets y-axis labels horizontal, but note note enough room
boxplot(values ~ group, data = dat, horizontal = TRUE, las = 1)

## so change the margin on the left (no. 2)
## default is c(5, 4, 4, 2) + 0.1
## and shrink text size so we don't need a huge margin
op <- par(mar = c(5, 10, 4, 2) + 0.1)
boxplot(values ~ group, data = dat, horizontal = TRUE, las = 1, 
        cex.axis = 0.7)
## reset the plotting parameters
par(op)

HTH

G
> 
> -Alex
> 
-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 Dr. Gavin Simpson             [t] +44 (0)20 7679 0522
 ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
 Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
 Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
 UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%



More information about the R-help mailing list