The R Project SVN R

Rev

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

Rev Author Line No. Line
42333 ripley 1
% File src/library/stats/man/interaction.plot.Rd
68948 ripley 2
% Part of the R package, https://www.R-project.org
81938 smeyer 3
% Copyright 1995-2022 R Core Team
42333 ripley 4
% Distributed under GPL 2 or later
5
 
27442 ripley 6
\name{interaction.plot}
56186 murdoch 7
\alias{interaction.plot}
27442 ripley 8
\title{Two-way Interaction Plot}
9
\description{
10
  Plots the mean (or other summary) of the response for two-way
11
  combinations of factors, thereby illustrating possible interactions.
12
}
13
\usage{
14
interaction.plot(x.factor, trace.factor, response, fun = mean,
57121 maechler 15
                 type = c("l", "p", "b", "o", "c"), legend = TRUE,
77594 maechler 16
                 trace.label = deparse1(substitute(trace.factor)),
30912 ripley 17
                 fixed = FALSE,
77594 maechler 18
                 xlab = deparse1(substitute(x.factor)),
36816 ripley 19
                 ylab = ylabel,
61160 ripley 20
                 ylim = range(cells, na.rm = TRUE),
27442 ripley 21
                 lty = nc:1, col = 1, pch = c(1:9, 0, letters),
22
                 xpd = NULL, leg.bg = par("bg"), leg.bty = "n",
36816 ripley 23
                 xtick = FALSE, xaxt = par("xaxt"), axes = TRUE,
24
                 \dots)
27442 ripley 25
}
26
\arguments{
27
  \item{x.factor}{a factor whose levels will form the x axis.}
28
  \item{trace.factor}{another factor whose levels will form the traces.}
81938 smeyer 29
  \item{response}{a numeric variable giving the response.}
27442 ripley 30
  \item{fun}{the function to compute the summary. Should return a single
31
    real value.}
57121 maechler 32
  \item{type}{the type of plot (see \code{\link{plot.default}}): lines
33
    or points or both.}
27442 ripley 34
  \item{legend}{logical. Should a legend be included?}
35
  \item{trace.label}{overall label for the legend.}
36
  \item{fixed}{logical.  Should the legend be in the order of the levels
81938 smeyer 37
    of \code{trace.factor} (\code{TRUE}) or in the order of the
38
    traces at their right-hand ends (\code{FALSE}, the default)?}
27442 ripley 39
  \item{xlab,ylab}{the x and y label of the plot each with a sensible default.}
40
  \item{ylim}{numeric of length 2 giving the y limits for the plot.}
41
  \item{lty}{line type for the lines drawn, with sensible default.}
42
  \item{col}{the color to be used for plotting.}
43
  \item{pch}{a vector of plotting symbols or characters, with sensible
44
    default.}
45
  \item{xpd}{determines clipping behaviour for the \code{\link{legend}}
46
    used, see \code{\link{par}(xpd)}.  Per default, the legend is
47
    \emph{not} clipped at the figure border.}
48
  \item{leg.bg, leg.bty}{arguments passed to \code{\link{legend}()}.}
49
  \item{xtick}{logical. Should tick marks be used on the x axis?}
50
  \item{xaxt, axes, \dots}{graphics parameters to be passed to the plotting routines.}
51
}
52
\details{
53
  By default the levels of \code{x.factor} are plotted on the x axis in
81938 smeyer 54
  their given order, with extra space on the right for the legend
27442 ripley 55
  (if specified). If \code{x.factor} is an ordered factor and the levels
56
  are numeric, these numeric values are used for the x axis.
57
 
58
  The response and hence its summary can contain missing values. If so,
59
  the missing values and the line segments joining them are omitted from
60
  the plot (and this can be somewhat disconcerting).
61
 
62
  The graphics parameters \code{xlab}, \code{ylab}, \code{ylim},
63
  \code{lty}, \code{col} and \code{pch} are given suitable defaults
40281 ripley 64
  (and \code{xlim} and \code{xaxs} are set and cannot be overridden).
27442 ripley 65
  The defaults are to cycle through the line types, use the foreground
81938 smeyer 66
  colour, and to use the symbols 1:9, 0, and the small letters to plot
27442 ripley 67
  the traces.
68
}
69
\note{
70
  Some of the argument names and the precise behaviour are chosen for
71
  S-compatibility.
72
}
73
\references{
88585 hornik 74
  \bibshow{R:Chambers+Freeny+Heiberger:1992}
27442 ripley 75
}
76
 
77
\examples{
41508 ripley 78
require(graphics)
79
 
37838 ripley 80
with(ToothGrowth, {
61160 ripley 81
interaction.plot(dose, supp, len, fixed = TRUE)
27442 ripley 82
dose <- ordered(dose)
81938 smeyer 83
interaction.plot(dose, supp, len, fixed = TRUE,
84
                 col = 2:3, leg.bty = "o", xtick = TRUE)
61160 ripley 85
interaction.plot(dose, supp, len, fixed = TRUE, col = 2:3, type = "p")
37838 ripley 86
})
27442 ripley 87
 
88
with(OrchardSprays, {
89
  interaction.plot(treatment, rowpos, decrease)
61160 ripley 90
  interaction.plot(rowpos, treatment, decrease, cex.axis = 0.8)
27442 ripley 91
  ## order the rows by their mean effect
44243 ripley 92
  rowpos <- factor(rowpos,
93
                   levels = sort.list(tapply(decrease, rowpos, mean)))
27442 ripley 94
  interaction.plot(rowpos, treatment, decrease, col = 2:9, lty = 1)
95
})
96
 
79479 hornik 97
%% FIXME esoph
98
%% with(esoph, {
99
%%   interaction.plot(agegp, alcgp, ncases/ncontrols, main = "'esoph' Data")
100
%%   interaction.plot(agegp, tobgp, ncases/ncontrols, trace.label = "tobacco",
101
%%                    fixed = TRUE, xaxt = "n")
102
%% })
103
%% ## deal with NAs:
104
%% esoph[66,] # second to last age group: 65-74
105
%% esophNA <- esoph; esophNA$ncases[66] <- NA
106
%% with(esophNA, {
107
%%   interaction.plot(agegp, alcgp, ncases/ncontrols, col = 2:5)
108
%%                                 # doesn't show *last* group either
109
%%   interaction.plot(agegp, alcgp, ncases/ncontrols, col = 2:5, type = "b")
88896 smeyer 110
%%   ## alternative take non-NAs  {"cheating"}
79479 hornik 111
%%   interaction.plot(agegp, alcgp, ncases/ncontrols, col = 2:5,
112
%%                    fun = function(x) mean(x, na.rm = TRUE),
113
%%                    sub = "function(x) mean(x, na.rm=TRUE)")
114
%% })
115
%% rm(esophNA) # to clear up
27442 ripley 116
}
117
\keyword{hplot}