[R] help with xyplot

Deepayan Sarkar deepayan at stat.wisc.edu
Sat May 24 22:58:56 CEST 2003


On Saturday 24 May 2003 15:14, Ronaldo Reis Jr. wrote:
> Hi,
>
> I make a graphic using xyplot, it is very good.
>
> xyplot((ocorrencia/isca)~frag|especie)
>
> But I need to plot a curve for each especie's plot.
>
> I try to use panel.abline and others panel., but I dont succeed.
>
> I need to plot the function exp(a+b*x+c*x^2)/1+exp(a+b*x+c*x^2), a b and c
> are different for each especie.
>
> Normally I use the curve function to add a curve in a points graphics, but
> it dont work with xyplot.

Here's a version of curve modified to work with lattice:

lcurve <-
    function (expr, from, to, n = 101, curve.type = "l",
              col = add.line$col, lty = add.line$lty,
              lwd = add.line$lwd, type = NULL,
              ...)
{
    add.line <- trellis.par.get("add.line")
    sexpr <- substitute(expr)
    if (is.name(sexpr)) {
        fcall <- paste(sexpr, "(x)")
        expr <- parse(text = 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
    }
    lims <- current.viewport()$xscale
    if (missing(from)) 
        from <- lims[1]
    if (missing(to)) 
        to <- lims[2]
    x <- seq(from, to, length = n)
    y <- eval(expr, envir = list(x = x), enclos = parent.frame())
    llines(x, y, type = curve.type, col = col, lty = lty, lwd = lwd, ...)
}


With this defined, you can do something like 


xyplot((ocorrencia/isca)~frag|especie, 
       panel = function(x, y, ...) {
           panel.xyplot(x, y, ...)
           ## calculate a, b, c based on x and y
           lcurve(exp(a+b*x+c*x^2)/1+exp(a+b*x+c*x^2), ...)
       })


I will add a lcurve() similar to this for the next release of lattice.

Deepayan




More information about the R-help mailing list