Skip to content
Prev 67774 / 398506 Next

abbreviate or wrap dimname labels

For a variety of displays (mosaicplots, barplots, ...)
one often wants to either abbreviate or wrap long labels,
particularly when these are made up of several words.
In general, it would be nice to have a function,

abbreviate.or.wrap <-
    function(x, maxlength=10, maxlines=2, split=" ") {
}

that would take a character vector or a list of vectors, x,
and try to abbreviate or wrap them to fit approximately
the maxlength and maxlines constraints, using the split
argument to specify allowable characters to wrap to multiple
lines.

For example, this two-way table has dimnames too long to
be displayed nicely in a mosaicplot:

 > library(catspec)
 > library(vcd)
 >
 > data(FHtab)
 > FHtab<-as.data.frame(FHtab)
 >
 > xtable <- xtabs(Freq ~ .,FHtab)
 > lab <- dimnames(xtable)
 > lab
$OccFather
[1] "Upper nonmanual" "Lower nonmanual" "Upper manual"    "Lower manual"
[5] "Farm"

$OccSon
[1] "Upper nonmanual" "Lower nonmanual" "Upper manual"    "Lower manual"
[5] "Farm"

abbreviate works here, but gives results that aren't very readable:

 > lapply(lab, abbreviate, 8)
$OccFather
Upper nonmanual Lower nonmanual    Upper manual    Lower manual       Farm
      "Upprnnmn"      "Lwrnnmnl"      "Uppermnl"      "Lowermnl" 
   "Farm"

$OccSon
Upper nonmanual Lower nonmanual    Upper manual    Lower manual 
    Farm
      "Upprnnmn"      "Lwrnnmnl"      "Uppermnl"      "Lowermnl" 
   "Farm"

In a related thread, Marc Schwartz proposed a solution for wrapping
labels, based on

 >short.labels <- sapply(labels, function(x) paste(strwrap(x,
                          10), collapse = "\n"), USE.NAMES = FALSE)

But, my attempt to use strwrap in my context gives a single string
for each set of dimension names:

 > stack.lab <-function(x) { paste(strwrap(x,10), collapse = "\n") }
 > lapply(lab, stack.lab)
$OccFather
[1] "Upper\nnonmanual\nLower\nnonmanual\nUpper\nmanual\nLower\nmanual\nFarm"

$OccSon
[1] "Upper\nnonmanual\nLower\nnonmanual\nUpper\nmanual\nLower\nmanual\nFarm"

For my particular example, I can do what I want with gsub, but it is
hardly general:

 > lab[[1]] <- gsub(" ","\n", lab[[1]])
 > lab[[2]] <- lab[[1]]   # cheating: I know it's a square table
 > lab
$OccFather
[1] "Upper\nnonmanual" "Lower\nnonmanual" "Upper\nmanual" 
"Lower\nmanual"
[5] "Farm"

$OccSon
[1] "Upper\nnonmanual" "Lower\nnonmanual" "Upper\nmanual" 
"Lower\nmanual"
[5] "Farm"

 > dimnames(xtable) <- lab

Then,
mosaicplot(xtable, shade=TRUE)
gives a nice display!

Can anyone help with a more general solution for wrapping labels
or abbreviate.or.wrap()?

thanks,
-Michael