The R Project SVN R

Rev

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

Rev Author Line No. Line
42333 ripley 1
% File src/library/graphics/man/plotdefault.Rd
68948 ripley 2
% Part of the R package, https://www.R-project.org
88790 maechler 3
% Copyright 1995-2025 R Core Team
42333 ripley 4
% Distributed under GPL 2 or later
5
 
27442 ripley 6
\name{plot.default}
56186 murdoch 7
\alias{plot.default}
77912 deepayan 8
\alias{plot}
27442 ripley 9
\title{The Default Scatterplot Function}
10
\description{
42961 ripley 11
  Draw a scatter plot with decorations such as axes and titles
27442 ripley 12
  in the active graphics window.
13
}
14
\usage{
15
\method{plot}{default}(x, y = NULL, type = "p",  xlim = NULL, ylim = NULL,
88790 maechler 16
     log = "", lim2 = FALSE, main = NULL, sub = NULL, xlab = NULL, ylab = NULL,
27442 ripley 17
     ann = par("ann"), axes = TRUE, frame.plot = axes,
74856 maechler 18
     panel.first = NULL, panel.last = NULL, asp = NA,
19
     xgap.axis = NA, ygap.axis = NA,
20
     \dots)
27442 ripley 21
}
22
\arguments{
36110 ripley 23
  \item{x, y}{the \code{x} and \code{y} arguments provide the x and y
27442 ripley 24
    coordinates for the plot.  Any reasonable way of defining the
25
    coordinates is acceptable.  See the function \code{\link{xy.coords}}
45364 ripley 26
    for details.  If supplied separately, they must be of the same length.}
27442 ripley 27
  \item{type}{1-character string giving the type of plot desired.  The
87653 smeyer 28
    following values are possible, for details, see \code{\link[base]{plot}}:
29
    \code{"p"} for points,
30
    \code{"l"} for lines,
58047 murdoch 31
    \code{"b"} for both points and lines,
61433 ripley 32
    \code{"c"} for empty points joined by lines,
27442 ripley 33
    \code{"o"} for overplotted points and lines,
34
    \code{"s"} and \code{"S"} for stair steps and
35
    \code{"h"} for histogram-like vertical lines.  Finally,
36
    \code{"n"} does not produce any points or lines.}
39607 ripley 37
  \item{xlim}{the x limits (x1, x2) of the plot.  Note that \code{x1 > x2}
56108 ripley 38
    is allowed and leads to a \sQuote{reversed axis}.
39
 
40
    The default value, \code{NULL}, indicates that the range of the
41
    \link{finite} values to be plotted should be used.}
88790 maechler 42
  \item{ylim}{the y limits of the plot; see \code{xlim} for default and details.}
27442 ripley 43
  \item{log}{a character string which contains \code{"x"} if the x axis
44
    is to be logarithmic, \code{"y"} if the y axis is to be logarithmic
45
    and \code{"xy"} or \code{"yx"} if both axes are to be logarithmic.}
88790 maechler 46
  \item{lim2}{logical, relevant only when at least one of \code{xlim} or
47
    \code{ylim} are \code{NULL} and hence corresponding axis limits are
48
    computed from \code{\link{range}(<x/y-coord>[<finite-coord>])}.
49
    When true, \emph{both} x- and y-values need to be finite to be used in
50
    the \code{range(.)} computation.}
38167 maechler 51
  \item{main}{a main title for the plot, see also \code{\link{title}}.}
81315 smeyer 52
  \item{sub}{a subtitle for the plot.}
36110 ripley 53
  \item{xlab}{a label for the x axis, defaults to a description of \code{x}.}
54
  \item{ylab}{a label for the y axis, defaults to a description of \code{y}.}
27442 ripley 55
 
56
  \item{ann}{a logical value indicating whether the default annotation
57
    (title and x and y axis labels) should appear on the plot.}
36269 ripley 58
  \item{axes}{a logical value indicating whether both axes should be drawn on
56113 ripley 59
    the plot.  Use \link{graphical parameter} \code{"xaxt"} or \code{"yaxt"}
36269 ripley 60
    to suppress just one of the axes.}
27442 ripley 61
  \item{frame.plot}{a logical indicating whether a box should be drawn
62
    around the plot.}
63
 
56108 ripley 64
  \item{panel.first}{an \sQuote{expression} to be evaluated after the
65
    plot axes are set up but before any plotting takes place.  This can
66
    be useful for drawing background grids or scatterplot smooths.  Note
67
    that this works by lazy evaluation: passing this argument from other
55983 ripley 68
    \code{plot} methods may well not work since it may be evaluated too
69
    early.}
61433 ripley 70
 
27442 ripley 71
  \item{panel.last}{an expression to be evaluated after plotting has
56010 hornik 72
    taken place but before the axes, title and box are added.  See the
56108 ripley 73
    comments about \code{panel.first}.}
27442 ripley 74
 
75
  \item{asp}{the \eqn{y/x} aspect ratio, see \code{\link{plot.window}}.}
74856 maechler 76
 
77
  \item{xgap.axis, ygap.axis}{the \eqn{x/y} axis gap factors, passed as
78
    \code{gap.axis} to the two \code{\link{axis}()} calls (when
79
    \code{axes} is true, as per default).}
80
 
56113 ripley 81
  \item{\dots}{other \link{graphical parameters} (see \code{\link{par}} and
38167 maechler 82
    section \sQuote{Details} below).}
27442 ripley 83
}
36110 ripley 84
\details{
56113 ripley 85
  Commonly used \link{graphical parameters} are:
36110 ripley 86
  \describe{
87
    \item{\code{col}}{The colors for lines and points.  Multiple colors can be
88
      specified so that each point can be given its own color.  If there
89
      are fewer colors than points they are recycled in the standard
90
      fashion.  Lines will all be plotted in the first colour specified.}
91
    \item{\code{bg}}{a vector of background colors for open plot symbols, see
92
      \code{\link{points}}.  Note: this is \bold{not} the same setting
93
      as \code{\link{par}("bg")}.}
94
    \item{\code{pch}}{a vector of plotting characters or symbols:
95
      see \code{\link{points}}.}
96
    \item{\code{cex}}{a numerical vector giving the amount by which
45975 ripley 97
      plotting characters and symbols should be scaled relative to the
36110 ripley 98
      default.  This works as a multiple of \code{\link{par}("cex")}.
45975 ripley 99
      \code{NULL} and \code{NA} are equivalent to \code{1.0}.  Note that
100
      this does not affect annotation: see below.}
59510 ripley 101
    \item{\code{lty}}{a vector of line types, see \code{\link{par}}.}
49612 hornik 102
    \item{\code{cex.main}, \code{col.lab}, \code{font.sub},
103
      etc}{settings for main- and sub-title and axis annotation, see
104
      \code{\link{title}} and \code{\link{par}}.}
59510 ripley 105
    \item{\code{lwd}}{a vector of line widths, see \code{\link{par}}.}
36110 ripley 106
  }
107
}
58321 ripley 108
\note{
109
  The presence of \code{panel.first} and \code{panel.last} is a
110
  historical anomaly: default plots do not have \sQuote{panels}, unlike
66444 hornik 111
  e.g.\sspace{}\code{\link{pairs}} plots.  For more control, use lower-level
58321 ripley 112
  plotting functions: \code{plot.default} calls in turn some of
113
  \code{\link{plot.new}}, \code{\link{plot.window}},
114
  \code{\link{plot.xy}}, \code{\link{axis}}, \code{\link{box}} and
115
  \code{\link{title}}, and plots can be built up by calling these
116
  individually, or by calling \code{plot(type = "n")} and adding further
117
  elements.
77912 deepayan 118
 
119
  The \code{plot} generic was moved from the \pkg{graphics} package to
120
  the \pkg{base} package in \R 4.0.0. It is currently re-exported from
121
  the \pkg{graphics} namespace to allow packages importing it from there
122
  to continue working, but this may change in future versions of \R.
58321 ripley 123
}
124
 
27442 ripley 125
\references{
88598 hornik 126
  \bibshow{R:Becker+Chambers+Wilks:1988,
127
    R:Cleveland:1985,
128
    R:Murrell:2005}
27442 ripley 129
}
130
\seealso{
87653 smeyer 131
  \code{\link[base]{plot}}, \code{\link{plot.window}}, \code{\link{xy.coords}}.
68988 maechler 132
  For thousands of points, consider using \code{\link{smoothScatter}}
133
  instead.
27442 ripley 134
}
135
\examples{
136
Speed <- cars$speed
137
Distance <- cars$dist
61168 ripley 138
plot(Speed, Distance, panel.first = grid(8, 8),
27442 ripley 139
     pch = 0, cex = 1.2, col = "blue")
140
plot(Speed, Distance,
41502 ripley 141
     panel.first = lines(stats::lowess(Speed, Distance), lty = "dashed"),
27442 ripley 142
     pch = 0, cex = 1.2, col = "blue")
143
 
144
## Show the different plot types
145
x <- 0:12
146
y <- sin(pi/5 * x)
147
op <- par(mfrow = c(3,3), mar = .1+ c(2,2,3,1))
148
for (tp in c("p","l","b",  "c","o","h",  "s","S","n")) {
61156 ripley 149
   plot(y ~ x, type = tp, main = paste0("plot(*, type = \"", tp, "\")"))
27442 ripley 150
   if(tp == "S") {
61168 ripley 151
      lines(x, y, type = "s", col = "red", lty = 2)
61156 ripley 152
      mtext("lines(*, type = \"s\", ...)", col = "red", cex = 0.8)
27442 ripley 153
   }
154
}
155
par(op)
156
 
88790 maechler 157
## Log Plots -- default xlim / ylim  from range(<finite>)
158
set.seed(2); noise <- rnorm(100)/100; xe <- sort(rexp(100, 8))
159
x <- sort(xe + noise); y <- exp(-x^2 + (x < 0)*10)*sin(pi*x)
160
str(data.frame(x,y))
161
plot(y ~ x) # (2 outliers, x < 0)
162
plot(y ~ x, log="x") # --> Warning:  2 x values <= 0 omitted from log..plot
163
                     # still same y-axis limits [~ -570, 0 ]
164
plot(y ~ x, log="x", lim2 = TRUE) # still Warning, but
165
                                        # nice - reasonable y-axis [0, 0.8]
166
 
27442 ripley 167
##--- Log-Log Plot  with  custom axes
80079 hornik 168
lx <- seq(1, 5, length.out = 41)
27442 ripley 169
yl <- expression(e^{-frac(1,2) * {log[10](x)}^2})
170
y <- exp(-.5*lx^2)
70685 maechler 171
op <- par(mfrow = c(2,1), mar = par("mar")-c(1,0,2,0), mgp = c(2, .7, 0))
61153 ripley 172
plot(10^lx, y, log = "xy", type = "l", col = "purple",
173
     main = "Log-Log plot", ylab = yl, xlab = "x")
61168 ripley 174
plot(10^lx, y, log = "xy", type = "o", pch = ".", col = "forestgreen",
61153 ripley 175
     main = "Log-Log plot with custom axes", ylab = yl, xlab = "x",
27442 ripley 176
     axes = FALSE, frame.plot = TRUE)
40660 ripley 177
my.at <- 10^(1:5)
61153 ripley 178
axis(1, at = my.at, labels = formatC(my.at, format = "fg"))
70685 maechler 179
e.y <- -5:-1 ; at.y <- 10^e.y
180
axis(2, at = at.y, col.axis = "red", las = 1,
181
     labels = as.expression(lapply(e.y, function(E) bquote(10^.(E)))))
27442 ripley 182
par(op)
183
}
184
\keyword{hplot}