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

Prof Brian Ripley ripley at stats.ox.ac.uk
Sun Dec 2 08:51:24 CET 2001


In R, using for as John Fox has done is almost certainly as good as
anything.  That's what apply does internally.

Think of the *apply of splitting data by some grouping, apolying a
function to each subgroup and combining the results.  That means which
subgroup it was was not known.  So you do need to have all the
information split: you could that by taking the titles onto the original
object or splitting the indices of a vector or array (rather than the
object itself).

Using lapply() rather than a for() loop used to be very worthwhile in
S-PLUS 3.4.  It was worse in 5.0, and is somewhat better in 6.0.  R has
never had the (extreme) memory problems with for() loops that some
versions of S have had, so it's only worth avoiding solutions with
explicit loops if they prove to be too slow.


On Sat, 1 Dec 2001, John Fox wrote:

> Dear Mike,
>
> There might be a  more elegant solution, but one way is slice the table
> into a list of tables corresponding to the panels of the display, and to
> use a for loop to index panels and titles:
>
>      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,4))
> # why np x np?
>          on.exit(par(opar))
>          panels <- apply(table, by, list)
>          for (i in 1:np) mosaicplot(panels[[i]][[1]], main=titles[i],
> shade=T, ...)
>      }
>
> Does that do what you want?
>
> Regards,
>   John
>
> At 04:56 PM 11/30/2001 -0500, Michael Friendly wrote:
> >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)[[2]])
> >[1] "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
> >_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>
> -----------------------------------------------------
> John Fox
> Department of Sociology
> McMaster University
> Hamilton, Ontario, Canada L8S 4M4
> email: jfox at mcmaster.ca
> phone: 905-525-9140x23604
> web: www.socsci.mcmaster.ca/jfox
> -----------------------------------------------------
>
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> 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
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list