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