The R Project SVN R

Rev

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

Rev Author Line No. Line
42333 ripley 1
% File src/library/graphics/man/matplot.Rd
68948 ripley 2
% Part of the R package, https://www.R-project.org
87729 maechler 3
% Copyright 1995-2025 R Core Team
42333 ripley 4
% Distributed under GPL 2 or later
5
 
27442 ripley 6
\name{matplot}
56186 murdoch 7
\alias{matplot}
27442 ripley 8
\alias{matpoints}
9
\alias{matlines}
10
\title{Plot Columns of Matrices}
77746 maechler 11
\description{
12
  Plot the columns of one matrix against the columns of another (which
13
  often is just a vector treated as 1-column matrix).
14
}
27442 ripley 15
\usage{
48379 maechler 16
matplot(x, y, type = "p", lty = 1:5, lwd = 1, lend = par("lend"),
17
        pch = NULL,
38463 maechler 18
        col = 1:6, cex = NULL, bg = NA,
30915 ripley 19
        xlab = NULL, ylab = NULL, xlim = NULL, ylim = NULL,
74526 maechler 20
        log = "", \dots, add = FALSE, verbose = getOption("verbose"))
30912 ripley 21
 
22
matpoints(x, y, type = "p", lty = 1:5, lwd = 1, pch = NULL,
23
          col = 1:6, \dots)
24
 
25
matlines (x, y, type = "l", lty = 1:5, lwd = 1, pch = NULL,
26
          col = 1:6, \dots)
27442 ripley 27
}
28
\arguments{
29
  \item{x,y}{vectors or matrices of data for plotting.  The number of
30
    rows should match.  If one of them are missing, the other is taken
31
    as \code{y} and an \code{x} vector of \code{1:n} is used.
83419 ripley 32
    Missing values (\code{NA}s) are allowed. Typically,
77746 maechler 33
    \code{\link{class}(.)}es of \code{x} and \code{y} such as
83419 ripley 34
    \code{"\link{Date}"} are preserved.}
27442 ripley 35
  \item{type}{character string (length 1 vector) or vector of 1-character
36
    strings indicating the type of plot for each
37
    column of \code{y}, see \code{\link{plot}} for all possible
38
    \code{type}s.  The first character of \code{type}
39
    defines the first plot, the second character the second, etc.
40
    Characters in \code{type} are cycled through; e.g., \code{"pl"}
41
    alternately plots points and lines.}
48379 maechler 42
  \item{lty,lwd,lend}{vector of line types, widths, and end styles.
27442 ripley 43
    The first element is for the first column, the second element for
44
    the second column, etc., even if lines are not plotted for all
45
    columns. Line types will be used cyclically until all plots are
46
    drawn.}
47
  \item{pch}{character string or vector of 1-characters or integers for
77746 maechler 48
    plotting characters, see \code{\link{points}} for details.
27442 ripley 49
    The first character is the plotting-character for the first plot,
50
    the second for the second, etc.  The default is the digits (1
44331 maechler 51
    through 9, 0) then the lowercase and uppercase letters.
27442 ripley 52
  }
53
  \item{col}{vector of colors.  Colors are used cyclically.}
35243 ripley 54
  \item{cex}{vector of character expansion sizes, used cyclically.
55
    This works as a multiple of \code{\link{par}("cex")}.  \code{NULL} is
56
    equivalent to \code{1.0}. }
42961 ripley 57
  \item{bg}{vector of background (fill) colors for the open plot
61153 ripley 58
    symbols given by \code{pch = 21:25} as in \code{\link{points}}.  The
38463 maechler 59
    default \code{NA} corresponds to the one of the underlying function
60
    \code{\link{plot.xy}}.}
27442 ripley 61
  \item{xlab, ylab}{titles for x and y axes, as in \code{\link{plot}}.}
62
  \item{xlim, ylim}{ranges of x and y axes, as in \code{\link{plot}}.}
74526 maechler 63
  \item{log, \dots}{Graphical parameters (see \code{\link{par}}) and any further
27442 ripley 64
    arguments of \code{plot}, typically \code{\link{plot.default}}, may also be
74526 maechler 65
    supplied as arguments to this function; even \code{panel.first} etc
66
    now work.  Hence, the high-level
27442 ripley 67
    graphics control arguments described under \code{\link{par}} and the
68
    arguments to \code{\link{title}} may be supplied to this function.}
69
  \item{add}{logical.  If \code{TRUE}, plots are added to current one,
70
    using \code{\link{points}} and \code{\link{lines}}.}
71
  \item{verbose}{logical.  If \code{TRUE}, write one line of what is
72
    done.}
73
}
74
\section{Side Effects}{Function \code{matplot} generates a new plot;
75
  \code{matpoints} and \code{matlines} add to the current one.}
76
\details{
77746 maechler 77
  \code{matplot(x,y, ..)} is basically a wrapper for
78
  \enumerate{
79
    \item calling (the generic function) \code{\link{plot}(x[,1], y[,1], ..)}
80
    for the first columns (only if \code{add = TRUE}).
81
    \item calling (the generic) \code{\link{lines}(x[,j], y[,j], ..)} for
82
    subsequent columns.
83
  }
83419 ripley 84
  Care is taken to keep the \code{\link{class}(.)} of
77746 maechler 85
  \code{x} and \code{y}, such that the corresponding \code{plot()} and
86
  \code{lines()} \emph{methods} will be called.
87
 
27442 ripley 88
  Points involving missing values are not plotted.
89
 
90
  The first column of \code{x} is plotted against the first column of
91
  \code{y}, the second column of \code{x} against the second column of
92
  \code{y}, etc.  If one matrix has fewer columns, plotting will cycle
93
  back through the columns again.  (In particular, either \code{x} or
94
  \code{y} may be a vector, against which all columns of the other
95
  argument will be plotted.)
96
 
97
  The first element of \code{col, cex, lty, lwd} is used to plot the axes
98
  as well as the first line.
99
 
100
  Because plotting symbols are drawn with lines and because these
101
  functions may be changing the line style, you should probably specify
61153 ripley 102
  \code{lty = 1} when using plotting symbols.
27442 ripley 103
}
104
\references{
88581 hornik 105
  \bibshow{R:Becker+Chambers+Wilks:1988}
27442 ripley 106
}
107
\seealso{
108
  \code{\link{plot}}, \code{\link{points}}, \code{\link{lines}},
109
  \code{\link{matrix}}, \code{\link{par}}.
110
}
111
\examples{
41502 ripley 112
require(grDevices)
27442 ripley 113
matplot((-4:5)^2, main = "Quadratic") # almost identical to plot(*)
114
sines <- outer(1:20, 1:4, function(x, y) sin(x / 20 * pi * y))
115
matplot(sines, pch = 1:4, type = "o", col = rainbow(ncol(sines)))
38463 maechler 116
matplot(sines, type = "b", pch = 21:23, col = 2:5, bg = 2:5,
117
        main = "matplot(...., pch = 21:23, bg = 2:5)")
27442 ripley 118
 
119
x <- 0:50/50
120
matplot(x, outer(x, 1:8, function(x, k) sin(k*pi * x)),
121
        ylim = c(-2,2), type = "plobcsSh",
122
        main= "matplot(,type = \"plobcsSh\" )")
123
## pch & type =  vector of 1-chars :
124
matplot(x, outer(x, 1:4, function(x, k) sin(k*pi * x)),
125
        pch = letters[1:4], type = c("b","p","o"))
126
 
48379 maechler 127
lends <- c("round","butt","square")
128
matplot(matrix(1:12, 4), type="c", lty=1, lwd=10, lend=lends)
129
text(cbind(2.5, 2*c(1,3,5)-.4), lends, col= 1:3, cex = 1.5)
130
 
30448 ripley 131
table(iris$Species) # is data.frame with 'Species' factor
27442 ripley 132
iS <- iris$Species == "setosa"
133
iV <- iris$Species == "versicolor"
88990 maechler 134
PS <- c("Petal", "Sepal") ; clr <- c(2,4)
135
psL <- paste(PS, "Length",sep="."); Sps <- paste0("    Setosa ", PS, "s")
136
psW <- paste(PS, "Width", sep="."); Vps <- paste0("Versicolor ", PS, "s")
27442 ripley 137
op <- par(bg = "bisque")
61153 ripley 138
matplot(c(1, 8), c(0, 4.5), type =  "n", xlab = "Length", ylab = "Width",
27442 ripley 139
        main = "Petal and Sepal Dimensions in Iris Blossoms")
88990 maechler 140
matpoints(iris[iS, psL], iris[iS, psW], pch = "sS", col = clr)
141
matpoints(iris[iV, psL], iris[iV, psW], pch = "vV", col = clr)
142
legend(1, 4,    c(Sps, Vps),          pch = "sSvV", col = clr)
27442 ripley 143
 
87729 maechler 144
matplot(iris3[, "Petal L.",], iris3[, "Petal W.",], pch = "SCV",
61153 ripley 145
        col = rainbow(3, start = 0.8, end = 0.1),
87729 maechler 146
        sub = paste(c("S", "C", "V"), dimnames(iris3)[[3]],
27442 ripley 147
                    sep = "=", collapse= ",  "),
148
        main = "Fisher's Iris Data")
29354 ripley 149
par(op)
77746 maechler 150
 
151
## 'x' a "Date" vector :
152
nd <- length(dv <- seq(as.Date("1959-02-21"), by = "weeks", length.out = 100))
153
mSC <- cbind(I=1, sin=sin(pi*(1:nd)/8), cos=cos(pi*(1:nd)/8))
154
matplot(dv, mSC, type = "b", main = "matplot(<Date>, y)")
155
 
156
## 'x' a "POSIXct" date-time vector :
157
ct <- seq(c(ISOdate(2000,3,20)), by = "15 mins", length.out = 100)
158
matplot(ct, mSC, type = "b", main = "matplot(<POSIXct>, y)")
159
## or the same with even more axis flexibility:
160
matplot(ct, mSC, type = "b", main = "matplot(<POSIXct>, y)", xaxt="n")
161
Axis(ct, side=1, at = ct[1+4*(0:24)])
162
 
87729 maechler 163
iS <- 1:3 # indices of subset
164
matplot(gait[, iS, 1], gait[, iS, 2], pch = "123", type = "b",
165
        col = rainbow(3, start = 0.8, end = 0.1),
166
        sub = paste(iS, dimnames(gait)[[2]][iS],
167
                    sep = "=", collapse= ",  "),
168
        xlab = "Hip angle", ylab = "Knee angle",
169
        main = "Hip and knee angle while walking")
170
 
77746 maechler 171
## Also works for data frame columns:
172
matplot(iris[1:50,1:4])
27442 ripley 173
}
174
\keyword{hplot}
175
\keyword{aplot}
176
\keyword{array}