简体   繁体   中英

What is the difference between curve and plot function in R?

f1<-function(t)
    {
    sqrt((t^2)+1)
}

curve(f1,from=0,to = 5,n=10)
plot(f1,from=0,to = 5,n=10)

gives the same output. Then, what is the difference between curve and plot function?

Not a lot for functions. plot eventually calls curve .

plot is a generic function, meaning that it has multiple methods depending on the class of object passed to it (in this case, a function). To find out the code behind a particular method, you can type graphcs:::plot.<method> .

In this case, you can see that plot when applied to a function first checks and tweaks its arguments before eventually just calling curve .

> graphics:::plot.function
function (x, y = 0, to = 1, from = y, xlim = NULL, ylab = NULL, 
    ...) 
{
    if (!missing(y) && missing(from)) 
        from <- y
    if (is.null(xlim)) {
        if (is.null(from)) 
            from <- 0
    }
    else {
        if (missing(from)) 
            from <- xlim[1L]
        if (missing(to)) 
            to <- xlim[2L]
    }
    if (is.null(ylab)) {
        sx <- substitute(x)
        ylab <- if (mode(x) != "name") 
            deparse(sx)[1L]
        else {
            xname <- list(...)[["xname"]]
            if (is.null(xname)) 
                xname <- "x"
            paste0(sx, "(", xname, ")")
        }
    }
    curve(expr = x, from = from, to = to, xlim = xlim, ylab = ylab, 
        ...)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM