[Rd] plot function

kjetil halvorsen kjetilh@umsanet.edu.bo
Tue, 16 Oct 2001 09:34:07 -0400


Hola!

It is somewhat inconvenient to use plot.function, when add=TRUE.

The following (miniscule) change makes it behave better:

 plot.function <-
function (fn, from , to, xlim = NULL, ...) 
{
    if (!is.null(xlim)) {
        if (missing(from)) 
            from <- xlim[1]
        if (missing(to)) 
            to <- xlim[2]
    }
    curve(fn, from, to, xlim = xlim, ...)
}

The only change is that the default arguments fro from and to
are omitted. (curve() has better code to give sensible defaults)

But this makes problems for curve when add=FALSE is used.

The following miniscule changes to curve restores its behaviour when
add=FALSE: (changes only local to the delay() expression)

curve <- 
function (expr, from, to, n = 101, add = FALSE, type = "l", ylab = NULL, 
    log = NULL, xlim = NULL, ...) 
{
    sexpr <- substitute(expr)
    if (is.name(sexpr)) {
        fcall <- paste(sexpr, "(x)")
        expr <- parse(text = fcall)
        if (is.null(ylab)) 
            ylab <- fcall
    }
    else {
        if (!(is.call(sexpr) && match("x", all.vars(sexpr), nomatch =
0))) 
            stop("'expr' must be a function or an expression containing
'x'")
        expr <- sexpr
        if (is.null(ylab)) 
            ylab <- deparse(sexpr)
    }
    lims <- if (is.null(xlim)) 
        delay({
            if (add) {
            pu <- par("usr")[1:2] } else
            pu <- c(0,1)
            if (par("xlog")) 
                10^pu
            else pu
        }, environment())
    else xlim
    if (missing(from)) 
        from <- lims[1]
    if (missing(to)) 
        to <- lims[2]
    lg <- if (length(log)) 
        log
    else paste(if (add && par("xlog")) 
        "x", if (add && par("ylog")) 
        "y", sep = "")
    x <- if (lg != "" && "x" %in% strsplit(lg, NULL)[[1]]) {
        if (any(c(from, to) <= 0)) 
            stop("`from' & `to' must be > 0      with  log=\"x\"")
        exp(seq(log(from), log(to), length = n))
    }
    else seq(from, to, length = n)
    y <- eval(expr, envir = list(x = x), enclos = parent.frame())
    if (add) 
        lines(x, y, type = type, ...)
    else plot(x, y, type = type, ylab = ylab, xlim = xlim, log = lg, 
        ...)
}


Kjetil Halvorsen
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._