The R Project SVN R

Rev

Rev 68948 | Rev 83419 | 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/matplot.Rd
68948 ripley 2
% Part of the R package, https://www.R-project.org
74526 maechler 3
% Copyright 1995-2018 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}
11
\usage{
48379 maechler 12
matplot(x, y, type = "p", lty = 1:5, lwd = 1, lend = par("lend"),
13
        pch = NULL,
38463 maechler 14
        col = 1:6, cex = NULL, bg = NA,
30915 ripley 15
        xlab = NULL, ylab = NULL, xlim = NULL, ylim = NULL,
74526 maechler 16
        log = "", \dots, add = FALSE, verbose = getOption("verbose"))
30912 ripley 17
 
18
matpoints(x, y, type = "p", lty = 1:5, lwd = 1, pch = NULL,
19
          col = 1:6, \dots)
20
 
21
matlines (x, y, type = "l", lty = 1:5, lwd = 1, pch = NULL,
22
          col = 1:6, \dots)
27442 ripley 23
}
24
\arguments{
25
  \item{x,y}{vectors or matrices of data for plotting.  The number of
26
    rows should match.  If one of them are missing, the other is taken
27
    as \code{y} and an \code{x} vector of \code{1:n} is used.
28
    Missing values (\code{NA}s) are allowed.}
29
  \item{type}{character string (length 1 vector) or vector of 1-character
30
    strings indicating the type of plot for each
31
    column of \code{y}, see \code{\link{plot}} for all possible
32
    \code{type}s.  The first character of \code{type}
33
    defines the first plot, the second character the second, etc.
34
    Characters in \code{type} are cycled through; e.g., \code{"pl"}
35
    alternately plots points and lines.}
48379 maechler 36
  \item{lty,lwd,lend}{vector of line types, widths, and end styles.
27442 ripley 37
    The first element is for the first column, the second element for
38
    the second column, etc., even if lines are not plotted for all
39
    columns. Line types will be used cyclically until all plots are
40
    drawn.}
41
  \item{pch}{character string or vector of 1-characters or integers for
42
    plotting characters, see \code{\link{points}}.
43
    The first character is the plotting-character for the first plot,
44
    the second for the second, etc.  The default is the digits (1
44331 maechler 45
    through 9, 0) then the lowercase and uppercase letters.
27442 ripley 46
%%__FIXME__ Which of the following is still true; what's the "right" UI ?
47
%  To mix special plotting symbols (those specified by integers) and
61153 ripley 48
%  plotting characters, either use character escapes \code{pch = "Xe002"}
27442 ripley 49
%  or figure out the numeric equivalent of the ASCII character,
61168 ripley 50
%  \code{pch = c(88, 2)}.
27442 ripley 51
  }
52
  \item{col}{vector of colors.  Colors are used cyclically.}
35243 ripley 53
  \item{cex}{vector of character expansion sizes, used cyclically.
54
    This works as a multiple of \code{\link{par}("cex")}.  \code{NULL} is
55
    equivalent to \code{1.0}. }
42961 ripley 56
  \item{bg}{vector of background (fill) colors for the open plot
61153 ripley 57
    symbols given by \code{pch = 21:25} as in \code{\link{points}}.  The
38463 maechler 58
    default \code{NA} corresponds to the one of the underlying function
59
    \code{\link{plot.xy}}.}
27442 ripley 60
  \item{xlab, ylab}{titles for x and y axes, as in \code{\link{plot}}.}
61
  \item{xlim, ylim}{ranges of x and y axes, as in \code{\link{plot}}.}
74526 maechler 62
  \item{log, \dots}{Graphical parameters (see \code{\link{par}}) and any further
27442 ripley 63
    arguments of \code{plot}, typically \code{\link{plot.default}}, may also be
74526 maechler 64
    supplied as arguments to this function; even \code{panel.first} etc
65
    now work.  Hence, the high-level
27442 ripley 66
    graphics control arguments described under \code{\link{par}} and the
67
    arguments to \code{\link{title}} may be supplied to this function.}
68
  \item{add}{logical.  If \code{TRUE}, plots are added to current one,
69
    using \code{\link{points}} and \code{\link{lines}}.}
70
  \item{verbose}{logical.  If \code{TRUE}, write one line of what is
71
    done.}
72
}
73
\section{Side Effects}{Function \code{matplot} generates a new plot;
74
  \code{matpoints} and \code{matlines} add to the current one.}
75
\description{
76
  Plot the columns of one matrix against the columns of another.
77
}
78
\details{
79
  Points involving missing values are not plotted.
80
 
81
  The first column of \code{x} is plotted against the first column of
82
  \code{y}, the second column of \code{x} against the second column of
83
  \code{y}, etc.  If one matrix has fewer columns, plotting will cycle
84
  back through the columns again.  (In particular, either \code{x} or
85
  \code{y} may be a vector, against which all columns of the other
86
  argument will be plotted.)
87
 
88
  The first element of \code{col, cex, lty, lwd} is used to plot the axes
89
  as well as the first line.
90
 
91
  Because plotting symbols are drawn with lines and because these
92
  functions may be changing the line style, you should probably specify
61153 ripley 93
  \code{lty = 1} when using plotting symbols.
27442 ripley 94
}
95
\references{
96
  Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)
97
  \emph{The New S Language}.
47262 ripley 98
  Wadsworth & Brooks/Cole.
27442 ripley 99
}
100
\seealso{
101
  \code{\link{plot}}, \code{\link{points}}, \code{\link{lines}},
102
  \code{\link{matrix}}, \code{\link{par}}.
103
}
104
\examples{
41502 ripley 105
require(grDevices)
27442 ripley 106
matplot((-4:5)^2, main = "Quadratic") # almost identical to plot(*)
107
sines <- outer(1:20, 1:4, function(x, y) sin(x / 20 * pi * y))
108
matplot(sines, pch = 1:4, type = "o", col = rainbow(ncol(sines)))
38463 maechler 109
matplot(sines, type = "b", pch = 21:23, col = 2:5, bg = 2:5,
110
        main = "matplot(...., pch = 21:23, bg = 2:5)")
27442 ripley 111
 
112
x <- 0:50/50
113
matplot(x, outer(x, 1:8, function(x, k) sin(k*pi * x)),
114
        ylim = c(-2,2), type = "plobcsSh",
115
        main= "matplot(,type = \"plobcsSh\" )")
116
## pch & type =  vector of 1-chars :
117
matplot(x, outer(x, 1:4, function(x, k) sin(k*pi * x)),
118
        pch = letters[1:4], type = c("b","p","o"))
119
 
48379 maechler 120
lends <- c("round","butt","square")
121
matplot(matrix(1:12, 4), type="c", lty=1, lwd=10, lend=lends)
122
text(cbind(2.5, 2*c(1,3,5)-.4), lends, col= 1:3, cex = 1.5)
123
 
30448 ripley 124
table(iris$Species) # is data.frame with 'Species' factor
27442 ripley 125
iS <- iris$Species == "setosa"
126
iV <- iris$Species == "versicolor"
127
op <- par(bg = "bisque")
61153 ripley 128
matplot(c(1, 8), c(0, 4.5), type =  "n", xlab = "Length", ylab = "Width",
27442 ripley 129
        main = "Petal and Sepal Dimensions in Iris Blossoms")
130
matpoints(iris[iS,c(1,3)], iris[iS,c(2,4)], pch = "sS", col = c(2,4))
131
matpoints(iris[iV,c(1,3)], iris[iV,c(2,4)], pch = "vV", col = c(2,4))
132
legend(1, 4, c("    Setosa Petals", "    Setosa Sepals",
133
               "Versicolor Petals", "Versicolor Sepals"),
134
       pch = "sSvV", col = rep(c(2,4), 2))
135
 
136
nam.var <- colnames(iris)[-5]
137
nam.spec <- as.character(iris[1+50*0:2, "Species"])
44243 ripley 138
iris.S <- array(NA, dim = c(50,4,3),
139
                dimnames = list(NULL, nam.var, nam.spec))
27442 ripley 140
for(i in 1:3) iris.S[,,i] <- data.matrix(iris[1:50+50*(i-1), -5])
141
 
61168 ripley 142
matplot(iris.S[, "Petal.Length",], iris.S[, "Petal.Width",], pch = "SCV",
61153 ripley 143
        col = rainbow(3, start = 0.8, end = 0.1),
27442 ripley 144
        sub = paste(c("S", "C", "V"), dimnames(iris.S)[[3]],
145
                    sep = "=", collapse= ",  "),
146
        main = "Fisher's Iris Data")
29354 ripley 147
par(op)
27442 ripley 148
}
149
\keyword{hplot}
150
\keyword{aplot}
151
\keyword{array}