[R] How to wrap or split labels on plot

Heather J. Branton hjb at pdq.com
Fri Dec 3 21:00:47 CET 2004


Dear R gurus,

I want to wrap labels that are too long for a plot. I have looked at 
strsplit(), substr(), nchar(), and strwrap(). I think it's some 
combination but I'm having difficulty trying to figure out the right 
combo. I think I need to create some new matrix containing the labels 
already split, though I'm not sure if maybe there is a quick and dirty 
way to address this without my wandering around the block.

I am using R 1.9.1, Windows XP.  (Note:  we are currently in the midst 
of a big project and probably won't upgrade to R 2.0.1 for another 
couple of weeks -- *unless* that's what I need to do to address these 
issues.)

Here is my script with two label options at the bottom that are not working.

# Settings
win.graph(width=8, height=8, pointsize=10)

# Read in data
test <- matrix(data=c(2.52,9.5,3.07,2.5,1.99,8.95), nrow = 6, byrow=TRUE)

# Read in and attach labels (names) to data
rownames(test) <- c("Mount Pleasant","Jordan","Oil City","Pleasant 
Valley","Village of Lake Isabella","Rosebush")

# Set plot limits:
xmax <- nrow(test)
nvec <- ncol(test)
ymax <- ceiling(max(test))
yinc <- 1

# Generate Pareto order
test <- test[order(test[,1],decreasing=TRUE),]

# Set color palette
MyCols <- rep(c("lightcyan","cornsilk","lavender"), each = xmax)

# Adjust the margins
par(mar = c(7, 5, 6, 3))

# Bar graph
mp <- barplot(test, beside = TRUE,
    col = MyCols,
    axisnames = FALSE,
    names.arg = rep(names(test),nvec),
    las = 2,
    cex.names = 0.75,
    ylab = "IXYV",
    ylim = c(0,ymax),
    yaxt = "n")

# Set up the y axis tick marks and labels
ifelse (ymax<=10,decpt <- 2,decpt <- 0)
ticks <- seq(0, ymax, yinc)
axis(2, at = ticks, las = 1,
     labels = formatC(ticks, format = "f", digits = decpt))

# Draw a box around the whole thing
box()

# Draw the x axis labels
mtext(side = 1, at = rowMeans(mp)-.2, line = .5, las=2, text = 
strsplit(names(test)," "))
mtext(side = 1, at = rowMeans(mp), line = .5, las=2, text = 
strwrap(names(test),7))
mtext(side = 1, line = 5.5, text = "Division")

# Draw titles
title(main="Central", outer=F, font.main=4, line=4)
title(main="IXYV by Division", outer=F, font.main=2, line=2.5)


Thank you kindly for your help.

...heather

 
_______________________________________________________________________
Heather J. Branton                                  Public Data Queries
Data Specialist                                 310 Depot Street, Ste C
734.213.4964 x312                                  Ann Arbor, MI  48104

               U.S. Census Microdata At Your Fingertips
                          http://www.pdq.com




More information about the R-help mailing list