curve {graphics} | R Documentation |
Draws a curve corresponding to a function over the interval
[from, to]
. curve
can plot also an expression in the variable
x
.
curve(expr, from = NULL, to = NULL, n = 101, add = FALSE, type = "l", ylab = NULL, log = NULL, xlim = NULL, ...) ## S3 method for class 'function' plot(x, y = 0, to = 1, from = y, xlim = NULL, ...)
expr |
The name of a function, or a call or an
expression written as a function of |
x |
a ‘vectorizing’ numeric R function. |
y |
alias for |
from, to |
the range over which the function will be plotted. |
n |
integer; the number of x values at which to evaluate. |
add |
logical; if |
xlim |
|
type |
plot type: see |
ylab, log, ... |
labels and graphical parameters can also be
specified as arguments. See ‘Details’ for the
interpretation of the default for For the |
The evaluation of expr
or function x
is at n
points equally spaced over the range [from, to]
. The points
determined in this way are then plotted.
For curve()
, if either of from
or to
is
NULL
, it defaults to the corresponding element of xlim
if that is not NULL
, otherwise that of the x-limits of the
current plot. This is what one would expect for add = TRUE
:
however for add = FALSE
it uses the x-limits of the
previous plot if there was one, and c(1, 26)/27
if not.
For plot(<function>)
, the defaults for (from, to) are
(0, 1) unless xlim
is supplied.
The value of log
is used both to determine the plot axes
(unless add = TRUE
) and how ‘equally spaced’ is
interpreted: if the x component indicates log-scaling, the points at
which the expression or function is plotted are equally spaced on log
scale.
The default value of log
is taken from the current plot when
add
is true, and otherwise defaults to ""
(indicating
linear scales on both axes). (So the default behaviour if add =
TRUE
is to take the x-limits but not the x-log-scaling from the
previous plot. This will change in future versions of R.)
This used to be a quick hack which now seems to serve a useful purpose, but can give bad results for functions which are not smooth.
For expensive-to-compute expr
essions, you should use smarter tools.
The way curve
handles expr
has caused confusion. It
first looks to see if expr
is a name (also known as a
symbol), in which case it is taken to be the name of a function, and
expr
is replaced by the call expr(x)
. Otherwise it
checks that expr
is either a call or an
expression, and that it contains a reference to a variable
x
(using all.vars
): anything else is an error.
Then expr
is evaluated in an environment which supplies a
vector x
of length n
, and should evaluate to an object
of length n
. Note that this means that curve(x, ...)
is
taken as a request to plot a function named x
(and it is used
as such in the function
method for plot
).
A list with components x
and y
of the points that were
drawn is returned invisibly.
For historical reasons, add
is allowed as an argument to the
"function"
method of plot
, but its behaviour may surprise
you. It is recommended to use add
only with curve
.
splinefun
for spline interpolation, lines
.
plot(qnorm) # default range c(0, 1) is appropriate here, # but end values are -/+Inf and so are omitted. plot(qlogis, main = "The Inverse Logit : qlogis()") abline(h = 0, v = 0:2/2, lty = 3, col = "gray") curve(sin, -2*pi, 2*pi) curve(tan, main = "curve(tan) --> same x-scale as previous plot") op <- par(mfrow = c(2, 2)) curve(x^3 - 3*x, -2, 2) curve(x^2 - 2, add = TRUE, col = "violet") ## simple and advanced versions, quite similar: plot(cos, -pi, 3*pi) curve(cos, xlim = c(-pi, 3*pi), n = 1001, col = "blue", add = TRUE) chippy <- function(x) sin(cos(x)*exp(-x/2)) curve(chippy, -8, 7, n = 2001) plot (chippy, -8, -5) for(ll in c("", "x", "y", "xy")) curve(log(1+x), 1, 100, log = ll, sub = paste("log= '", ll, "'", sep = "")) par(op)