[R] How to wrap or split labels on plot
Marc Schwartz
MSchwartz at MedAnalytics.com
Sat Dec 4 03:21:22 CET 2004
On Fri, 2004-12-03 at 15:00 -0500, Heather J. Branton wrote:
> 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)
Heather,
There is likely to be more than one approach, but the one that I
generally use is to explicitly put a newline character "\n" into the
plot labels where required. So, in this case, you could do something
like:
names(test) <- c("Mount\nPleasant","Jordan","Oil City",
"Pleasant\nValley",
"Village of\nLake Isabella",
"Rosebush")
Also, there are some confusing things in your code, which I suspect may
tie back to your test data versus the actual data you are using. If I am
missing something here, you might want to clarify that, since things
like your colors and other things don't entirely make sense.
Here is something of a simplified approach using the test data as you
have it:
# Test can be a vector
test <- c(2.52, 9.5, 3.07, 2.5, 1.99, 8.95)
names(test) <- c("Mount\nPleasant","Jordan","Oil City",
"Pleasant\nValley",
"Village of\nLake Isabella",
"Rosebush")
# Use sort here
test <- sort(test, decreasing = TRUE)
ymax <- ceiling(max(test))
par(mar = c(7, 5, 6, 3))
# Note that you can use the names here for names.arg
# As a result of the "\n", the titles will print on two lines
mp <- barplot(test, names.arg = names(test),
cex.names = 0.8, ylab = "IXYV",
yaxt = "n", ylim = c(0, ymax))
ticks <- seq(0, ymax, 1)
axis(2, at = ticks, las = 1,
labels = formatC(ticks, format = "f",
digits = ifelse(ymax <= 10, 2, 0)))
box()
mtext(side = 1, line = 3.5, text = "Division")
# you can combine the two title() calls into one mtext() call
mtext(side = 3, text = c("Central", "IXYV by Division"),
font = c(4, 2), line = c(4, 2.5))
If your actual data is a more complex matrix, adjust the above
accordingly.
HTH,
Marc Schwartz
More information about the R-help
mailing list