Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 831
kali pradeep Avatar asked Mar 06 '23 12:03

kali pradeep


1 Answers

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, 
        ...)
}
like image 168
Hugh Avatar answered Apr 07 '23 04:04

Hugh