# [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",
lwd = add.line\$lwd, type = NULL,
...)
{
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
if (missing(to))
to <- lims
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

```