The R Project SVN R

Rev

Rev 88593 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
42333 ripley 1
% File src/library/graphics/man/spineplot.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
 
35517 hornik 6
\name{spineplot}
56186 murdoch 7
\alias{spineplot}
35517 hornik 8
\alias{spineplot.default}
9
\alias{spineplot.formula}
10
\title{Spine Plots and Spinograms}
11
\description{
12
  Spine plots are a special cases of mosaic plots, and can be seen as
13
  a generalization of stacked (or highlighted) bar plots. Analogously,
14
  spinograms are an extension of histograms.
15
}
16
\usage{
17
spineplot(x, \dots)
36102 ripley 18
 
35517 hornik 19
\method{spineplot}{default}(x, y = NULL,
44243 ripley 20
          breaks = NULL, tol.ylab = 0.05, off = NULL,
21
          ylevels = NULL, col = NULL,
22
          main = "", xlab = NULL, ylab = NULL,
36102 ripley 23
          xaxlabels = NULL, yaxlabels = NULL,
84085 hornik 24
          xlim = NULL, ylim = c(0, 1), axes = TRUE, weights = NULL, \dots)
36102 ripley 25
 
55392 ripley 26
\method{spineplot}{formula}(formula, data = NULL,
44243 ripley 27
          breaks = NULL, tol.ylab = 0.05, off = NULL,
28
          ylevels = NULL, col = NULL,
29
          main = "", xlab = NULL, ylab = NULL,
36102 ripley 30
          xaxlabels = NULL, yaxlabels = NULL,
48612 ripley 31
          xlim = NULL, ylim = c(0, 1), axes = TRUE, \dots,
84085 hornik 32
          subset = NULL, weights = NULL, drop.unused.levels = FALSE)
35517 hornik 33
}
34
\arguments{
35
  \item{x}{an object, the default method expects either a single variable
36102 ripley 36
    (interpreted to be the explanatory variable) or a 2-way table.  See
37
    details.}
35517 hornik 38
  \item{y}{a \code{"factor"} interpreted to be the dependent variable}
36102 ripley 39
  \item{formula}{a \code{"formula"} of type \code{y ~ x} with a single
61433 ripley 40
    dependent \code{"factor"} and a single explanatory variable.}
35517 hornik 41
  \item{data}{an optional data frame.}
40281 ripley 42
  \item{breaks}{if the explanatory variable is numeric, this controls how
35517 hornik 43
    it is discretized. \code{breaks} is passed to \code{\link{hist}} and can
44
    be a list of arguments.}
45
  \item{tol.ylab}{convenience tolerance parameter for y-axis annotation.
46
    If the distance between two labels drops under this threshold, they are
47
    plotted equidistantly.}
48
  \item{off}{vertical offset between the bars (in per cent). It is fixed to
49
    \code{0} for spinograms and defaults to \code{2} for spine plots.}
41355 hornik 50
  \item{ylevels}{a character or numeric vector specifying in which order
51
    the levels of the dependent variable should be plotted.}
35517 hornik 52
  \item{col}{a vector of fill colors of the same length as \code{levels(y)}.
61433 ripley 53
    The default is to call \code{\link{gray.colors}}.}
35517 hornik 54
  \item{main, xlab, ylab}{character strings for annotation}
35533 hornik 55
  \item{xaxlabels, yaxlabels}{character vectors for annotation of x and y axis.
56
    Default to \code{levels(y)} and \code{levels(x)}, respectively for the
35599 hornik 57
    spine plot. For \code{xaxlabels} in the spinogram, the breaks are
58
    used.}
61433 ripley 59
  \item{xlim, ylim}{the range of x and y values with sensible defaults.}
48612 ripley 60
  \item{axes}{logical.  If \code{FALSE} all axes (including those giving
61
    level names) are suppressed.}
84085 hornik 62
  \item{weights}{numeric. A vector of frequency weights for each
63
    observation in the data. If \code{NULL} all weights are implicitly
64
    assumed to be 1. If \code{x} is already a 2-way table, the weights
65
    are ignored.}
35517 hornik 66
  \item{\dots}{additional arguments passed to \code{\link{rect}}.}
35600 hornik 67
  \item{subset}{an optional vector specifying a subset of observations
71669 hornik 68
    to be used for plotting.}
69
  \item{drop.unused.levels}{should factors have unused levels dropped?
70
    Defaults to \code{FALSE}.}
35517 hornik 71
}
72
\details{
36816 ripley 73
  \code{spineplot} creates either a spinogram or a spine plot.  It can
74
  be called via \code{spineplot(x, y)} or \code{spineplot(y ~ x)} where
75
  \code{y} is interpreted to be the dependent variable (and has to be
76
  categorical) and \code{x} the explanatory variable.  \code{x} can be
77
  either categorical (then a spine plot is created) or numerical (then a
78
  spinogram is plotted).  Additionally, \code{spineplot} can also be
79
  called with only a single argument which then has to be a 2-way table,
80
  interpreted to correspond to \code{table(x, y)}.
61433 ripley 81
 
36816 ripley 82
  Both, spine plots and spinograms, are essentially mosaic plots with
83
  special formatting of spacing and shading.  Conceptually, they plot
84
  \eqn{P(y | x)} against \eqn{P(x)}.  For the spine plot (where both
85
  \eqn{x} and \eqn{y} are categorical), both quantities are approximated
86
  by the corresponding empirical relative frequencies.  For the
87
  spinogram (where \eqn{x} is numerical), \eqn{x} is first discretized
40284 ripley 88
  (by calling \code{\link{hist}} with \code{breaks} argument) and then
36816 ripley 89
  empirical relative frequencies are taken.
61433 ripley 90
 
36816 ripley 91
  Thus, spine plots can also be seen as a generalization of stacked bar
92
  plots where not the heights but the widths of the bars corresponds to
93
  the relative frequencies of \code{x}. The heights of the bars then
94
  correspond to the conditional relative frequencies of \code{y} in
95
  every \code{x} group. Analogously, spinograms extend stacked
96
  histograms.
35517 hornik 97
}
98
\value{
99
  The table visualized is returned invisibly.
100
}
101
\seealso{
102
  \code{\link{mosaicplot}}, \code{\link{hist}}, \code{\link{cdplot}}
103
}
104
\references{
88593 hornik 105
  \bibshow{R:Friendly:1994a,
106
    R:Hartigan+Kleiner:1984,
88603 hornik 107
    R:Hofmann+Theus:2005,
88593 hornik 108
    R:Hummel:1996}
35517 hornik 109
}
110
\author{
85904 hornik 111
  \I{Achim Zeileis} \email{Achim.Zeileis@R-project.org}
35517 hornik 112
}
113
\examples{
114
## treatment and improvement of patients with rheumatoid arthritis
115
treatment <- factor(rep(c(1, 2), c(43, 41)), levels = c(1, 2),
116
                    labels = c("placebo", "treated"))
117
improved <- factor(rep(c(1, 2, 3, 1, 2, 3), c(29, 7, 7, 13, 7, 21)),
44243 ripley 118
                   levels = c(1, 2, 3),
119
                   labels = c("none", "some", "marked"))
35517 hornik 120
 
121
## (dependence on a categorical variable)
122
(spineplot(improved ~ treatment))
123
 
35533 hornik 124
## applications and admissions by department at UC Berkeley
35517 hornik 125
## (two-way tables)
77701 pd 126
(spineplot(marginSums(UCBAdmissions, c(3, 2)),
36816 ripley 127
           main = "Applications at UCB"))
77701 pd 128
(spineplot(marginSums(UCBAdmissions, c(3, 1)),
36816 ripley 129
           main = "Admissions at UCB"))
35517 hornik 130
 
131
## NASA space shuttle o-ring failures
36816 ripley 132
fail <- factor(c(2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 2, 1, 2, 1,
133
                 1, 1, 1, 2, 1, 1, 1, 1, 1),
35517 hornik 134
               levels = c(1, 2), labels = c("no", "yes"))
36816 ripley 135
temperature <- c(53, 57, 58, 63, 66, 67, 67, 67, 68, 69, 70, 70,
136
                 70, 70, 72, 73, 75, 75, 76, 76, 78, 79, 81)
35517 hornik 137
 
138
## (dependence on a numerical variable)
139
(spineplot(fail ~ temperature))
140
(spineplot(fail ~ temperature, breaks = 3))
141
(spineplot(fail ~ temperature, breaks = quantile(temperature)))
41355 hornik 142
 
143
## highlighting for failures
144
spineplot(fail ~ temperature, ylevels = 2:1)
35517 hornik 145
}
146
\keyword{hplot}