Skip to content
Prev 76214 / 398502 Next

Plotting nls

To get nice looking plots you can use trellis plots from the lattice
package.  First you need:

library(lattice)

Then you can define a custom panel function that will overlay the
fitted curve on top of the data points in a different color (you just
need to do this once; the fit you want plotted is specified as an
argument):

pred.overlay.panel <- function(x, y, fit, ...)
{
   panel.grid()
   panel.xyplot(x, y, ...)
   form <- as.list(sys.call(-2))[[2]]$call$formula
   resp <- deparse(form[[2]])
   covar <- deparse(form[[3]])
   xx <- seq(min(x), max(x), len=101)
   newdat <- data.frame(xx)
   colnames(newdat) <- covar
   panel.superpose(xx, predict(fit, newdata=newdat),
       subscripts=1:length(xx), groups=factor(rep(2, length(xx)),
       levels=1:2), type="l", ...)
}

Finally, you use the custom panel function in a call to xyplot:

xyplot(y ~ x, data=sample, panel=pred.overlay.panel, fit=fit,
scales=list(x=list(log=TRUE)))


Note how you specify that you want the x-axis to be in log-scale with
the scales parameter.

Hope this helps.

Ben
On 8/26/05, Lanre Okusanya <ooo at buffalo.edu> wrote: