The R Project SVN R

Rev

Rev 74265 | Rev 81393 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
42333 ripley 1
% File src/library/graphics/man/mosaicplot.Rd
68948 ripley 2
% Part of the R package, https://www.R-project.org
74265 hornik 3
% Copyright 1995-2018 R Core Team
42333 ripley 4
% Distributed under GPL 2 or later
5
 
27442 ripley 6
\name{mosaicplot}
56186 murdoch 7
\alias{mosaicplot}
27442 ripley 8
\alias{mosaicplot.default}
9
\alias{mosaicplot.formula}
50971 ripley 10
\encoding{UTF-8}
27442 ripley 11
\title{Mosaic Plots}
12
\description{Plots a mosaic on the current graphics device.}
13
\usage{
14
mosaicplot(x, \dots)
15
 
61433 ripley 16
\method{mosaicplot}{default}(x, main = deparse(substitute(x)),
30912 ripley 17
           sub = NULL, xlab = NULL, ylab = NULL,
18
           sort = NULL, off = NULL, dir = NULL,
34186 hornik 19
           color = NULL, shade = FALSE, margin = NULL,
55379 ripley 20
           cex.axis = 0.66, las = par("las"), border = NULL,
30912 ripley 21
           type = c("pearson", "deviance", "FT"), \dots)
27442 ripley 22
 
23
\method{mosaicplot}{formula}(formula, data = NULL, \dots,
30912 ripley 24
           main = deparse(substitute(data)), subset,
25
           na.action = stats::na.omit)
27442 ripley 26
}
27
\arguments{
28
  \item{x}{a contingency table in array form, with optional category
29
    labels specified in the \code{dimnames(x)} attribute.  The table is
30
    best created by the \code{table()} command.}
31
  \item{main}{character string for the mosaic title.}
32
  \item{sub}{character string for the mosaic sub-title (at bottom).}
61168 ripley 33
  \item{xlab, ylab}{x- and y-axis labels used for the plot; by default,
27442 ripley 34
    the first and second element of \code{names(dimnames(X))} (i.e., the
35
    name of the first and second variable in \code{X}).}
36
  \item{sort}{vector ordering of the variables, containing a permutation
37
    of the integers \code{1:length(dim(x))} (the default).}
38
  \item{off}{vector of offsets to determine percentage spacing at each
39
    level of the mosaic (appropriate values are between 0 and 20,
34468 hornik 40
    and the default is 20 times the number of splits for 2-dimensional
74363 maechler 41
    tables, and 10 otherwise).  Rescaled to maximally 50, and recycled if
34468 hornik 42
    necessary.}
27442 ripley 43
  \item{dir}{vector of split directions (\code{"v"} for vertical and
44
    \code{"h"} for horizontal) for each level of the mosaic, one
45
    direction for each dimension of the contingency table.  The
46
    default consists of alternating directions, beginning with a
47
    vertical split.}
48
  \item{color}{logical or (recycling) vector of colors for color
34175 hornik 49
    shading, used only when \code{shade} is \code{FALSE}, or \code{NULL}
61153 ripley 50
    (default).  By default, grey boxes are drawn.  \code{color = TRUE}
51
    uses a gamma-corrected grey palette.  \code{color = FALSE} gives empty
34175 hornik 52
    boxes with no shading.}
27442 ripley 53
  \item{shade}{a logical indicating whether to produce extended mosaic
54
    plots, or a numeric vector of at most 5 distinct positive numbers
55
    giving the absolute values of the cut points for the residuals.  By
56
    default, \code{shade} is \code{FALSE}, and simple mosaics are
57
    created.  Using \code{shade = TRUE} cuts absolute values at 2 and
58
    4.}
59
  \item{margin}{a list of vectors with the marginal totals to be fit in
60
    the log-linear model.  By default, an independence model is fitted.
30461 ripley 61
    See \code{\link{loglin}} for further information.}
27442 ripley 62
  \item{cex.axis}{The magnification to be used for axis annotation,
63
    as a multiple of \code{par("cex")}.}
64
  \item{las}{numeric; the style of axis labels, see \code{\link{par}}.}
55379 ripley 65
  \item{border}{colour of borders of cells: see \code{\link{polygon}}.}
27442 ripley 66
  \item{type}{a character string indicating the type of residual to be
67
    represented.  Must be one of \code{"pearson"} (giving components of
68
    Pearson's \eqn{\chi^2}{chi-squared}), \code{"deviance"} (giving
69
    components of the likelihood ratio \eqn{\chi^2}{chi-squared}), or
70
    \code{"FT"} for the Freeman-Tukey residuals.  The value of this
71
    argument can be abbreviated.}
72
  \item{formula}{a formula, such as \code{y ~ x}.}
73
  \item{data}{a data frame (or list), or a contingency table from which
74
    the variables in \code{formula} should be taken.}
75
  \item{\dots}{further arguments to be passed to or from methods.}
76
  \item{subset}{an optional vector specifying a subset of observations
77
    in the data frame to be used for plotting.}
29255 ripley 78
  \item{na.action}{a function which indicates what should happen
79
    when the data contains variables to be cross-tabulated, and these
80
    variables contain \code{NA}s.  The default is to omit cases which
81
    have an \code{NA} in any variable.  Since the tabulation will omit
82
    all cases containing missing values, this will only be useful if the
83
    \code{na.action} function replaces missing values.}
27442 ripley 84
}
85
\details{
86
  This is a generic function.  It currently has a default method
29255 ripley 87
  (\code{mosaicplot.default}) and a formula interface
88
  (\code{mosaicplot.formula}).
27442 ripley 89
 
37294 hornik 90
  Extended mosaic displays visualize standardized residuals of a
55392 ripley 91
  loglinear model for the table by color and outline of the mosaic's
92
  tiles.  (Standardized residuals are often referred to a standard
93
  normal distribution.)  Cells representing negative residuals are drawn
94
  in shaded of red and with broken borders; positive ones are drawn in
95
  blue with solid borders.
27442 ripley 96
 
97
  For the formula method, if \code{data} is an object inheriting from
55392 ripley 98
  class \code{"table"} or class \code{"ftable"} or an array with more
99
  than 2 dimensions, it is taken as a contingency table, and hence all
100
  entries should be non-negative.  In this case the left-hand side of
101
  \code{formula} should be empty and the variables on the right-hand
27442 ripley 102
  side should be taken from the names of the dimnames attribute of the
103
  contingency table.  A marginal table of these variables is computed,
55392 ripley 104
  and a mosaic plot of that table is produced.
27442 ripley 105
 
106
  Otherwise, \code{data} should be a data frame or matrix, list or
107
  environment containing the variables to be cross-tabulated.  In this
108
  case, after possibly selecting a subset of the data as specified by
109
  the \code{subset} argument, a contingency table is computed from the
110
  variables given in \code{formula}, and a mosaic is produced from
111
  this.
112
 
113
  See Emerson (1998) for more information and a case study with
114
  television viewer data from Nielsen Media Research.
29251 ripley 115
 
29255 ripley 116
  Missing values are not supported except via an \code{na.action}
117
  function when \code{data} contains variables to be cross-tabulated.
61433 ripley 118
 
37294 hornik 119
  A more flexible and extensible implementation of mosaic plots written
120
  in the grid graphics system is provided in the function
57939 hornik 121
  \code{\link[vcd]{mosaic}} in the contributed package \CRANpkg{vcd}
74265 hornik 122
  (Meyer, Zeileis and Hornik, 2006).
27442 ripley 123
}
124
\author{
39640 hornik 125
  S-PLUS original by John Emerson \email{john.emerson@yale.edu}.
37294 hornik 126
  Originally modified and enhanced for \R by Kurt Hornik.
27442 ripley 127
}
128
\references{
74265 hornik 129
  Hartigan, J.A., and Kleiner, B. (1984).
130
  A mosaic of television ratings.
131
  \emph{The American Statistician}, \bold{38}, 32--35.
132
  \doi{10.2307/2683556}.
27442 ripley 133
 
74265 hornik 134
  Emerson, J. W. (1998).
37294 hornik 135
  Mosaic displays in S-PLUS: A general implementation and a case study.
27442 ripley 136
  \emph{Statistical Computing and Graphics Newsletter (ASA)},
137
  \bold{9}, 1, 17--23.
138
 
74265 hornik 139
  Friendly, M. (1994).
27442 ripley 140
  Mosaic displays for multi-way contingency tables.
141
  \emph{Journal of the American Statistical Association}, \bold{89},
142
  190--200.
74265 hornik 143
  \doi{10.2307/2291215}.
27442 ripley 144
 
74265 hornik 145
  Meyer, D., Zeileis, A., and Hornik, K. (2006)  
146
  The strucplot Framework: Visualizing Multi-Way Contingency Tables with
147
  \pkg{vcd}.
148
  \emph{Journal of Statistical Software}, \bold{17(3)}, 1--48.
149
  \doi{10.18637/jss.v017.i03}.
27442 ripley 150
}
151
\seealso{
152
  \code{\link{assocplot}},
30461 ripley 153
  \code{\link{loglin}}.
27442 ripley 154
}
155
\examples{
41502 ripley 156
require(stats)
27442 ripley 157
mosaicplot(Titanic, main = "Survival on the Titanic", color = TRUE)
158
## Formula interface for tabulated data:
159
mosaicplot(~ Sex + Age + Survived, data = Titanic, color = TRUE)
160
 
161
mosaicplot(HairEyeColor, shade = TRUE)
162
## Independence model of hair and eye color and sex.  Indicates that
61433 ripley 163
## there are more blue eyed blonde females than expected in the case
37294 hornik 164
## of independence and too few brown eyed blonde females.
165
## The corresponding model is:
166
fm <- loglin(HairEyeColor, list(1, 2, 3))
167
pchisq(fm$pearson, fm$df, lower.tail = FALSE)
27442 ripley 168
 
37294 hornik 169
mosaicplot(HairEyeColor, shade = TRUE, margin = list(1:2, 3))
27442 ripley 170
## Model of joint independence of sex from hair and eye color.  Males
171
## are underrepresented among people with brown hair and eyes, and are
37294 hornik 172
## overrepresented among people with brown hair and blue eyes.
173
## The corresponding model is:
174
fm <- loglin(HairEyeColor, list(1:2, 3))
175
pchisq(fm$pearson, fm$df, lower.tail = FALSE)
27442 ripley 176
 
40615 ripley 177
## Formula interface for raw data: visualize cross-tabulation of numbers
27442 ripley 178
## of gears and carburettors in Motor Trend car data.
179
mosaicplot(~ gear + carb, data = mtcars, color = TRUE, las = 1)
30912 ripley 180
# color recycling
181
mosaicplot(~ gear + carb, data = mtcars, color = 2:3, las = 1)
27442 ripley 182
}
183
\keyword{hplot}