# [R] mosaic.by(): vectorizing args passed by apply()?

Michael Friendly friendly at yorku.ca
Fri Nov 30 22:56:56 CET 2001

```I've just started learning R, so I'm still on the steep part of the
learning curve, but my enthusiasm was heightened by learning that
there's a very nice implementation of mosaicplot().

As a learning project, I've already done a basic implementation
of a pairs.table() function which does a mosaic scatterplot matrix,
and now I'm trying to do conditional mosaic plots (discrete analog
of a coplot).

I found that
apply(table, by, mosaicplot,...)
worked quite nicely, but I want to label each mosaic with the
combination of the factor name and level value, e.g., Sex: Male,
and I can't figure out how to get just one element of a list
passed by apply as an argument.

In the function below, titles is a list like:
> paste("Sex: ", dimnames(Titanic)[])
 "Sex:  Male"   "Sex:  Female"
and I want each element passed as the main= value for the corresponding
slice of the table.
Can someone help?

Try the function below with

data(HairEyeColor)
mosaic.by(HairEyeColor, 3)

## Conditional mosaics, one for each level of the by-variable(s)
##   -- how to add factor levels as main= ?
##   -- how to do for more than one by-variable?
mosaic.by <-
function(table, by=NULL, ...)
{
n <- length(dim(table))
if(n == 0)
stop("invalid table `table'")
if(length(by)>1)
stop("Sorry, cannot handle >1 by variable yet.")

np <- dim(table)[by]
name <- names(dimnames(table))[by]
titles <- paste(name, ":", dimnames(table)[[by]])

opar <- par( mfrow=c(np, np), usr=c(1,1000,1,1000), mar=rep(1/2,4))
on.exit(par(opar))

apply(table, by, mosaicplot, main=titles, shade=T, legend=F, ...)
}

thx,
-Michael
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```