[R] mosaic.by(): vectorizing args passed by apply()?
John Fox
jfox at mcmaster.ca
Sat Dec 1 19:28:37 CET 2001
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list