[R] Conditionally Updating Lattice Plots

Deepayan Sarkar deepayan.sarkar at gmail.com
Tue Jul 22 02:31:40 CEST 2008

On 7/20/08, Bryan Hanson <hanson at depauw.edu> wrote:
> Hi All...
>  I can¹t seem to find an answer to this in the help pages, archives, or
>  Deepayan¹s Lattice Book.
>  I want to do a Lattice plot, and then update it, possibly more than once,
>  depending upon some logical options.  Code below; it produces a second plot
>  page when the second update is called, from which I would infer that you
>  can't update the update or I'm not calling it correctly.  I have a nagging
>  sense too that the "real" way to do this is with a non-standard use of
>  panel.superpose but I don't quite see how to do that from available
>  examples.

I think you are misunderstanding the concept of updating and the
purpose of the 'more' argument. What you want is most easily done as

fancy.lm <- function(x, y, fit = TRUE, resid = TRUE){
    model <- lm(y ~ x)
    y.pred <- predict(model)
    res.x <- as.vector(rbind(x, x, rep(NA,length(x))))
    res.y <- as.vector(rbind(y, y.pred, rep(NA,length(x))))

    xyplot(y ~ x, pch = 20,
           panel = function(...) {
               panel.xyplot(...) # not strictly necessary if I
understand correctly
               if (fit) {
                   panel.abline(model, col = "red")
               if (resid) {
                   panel.xyplot(res.x, res.y, col = "lightblue", type = "l")

x <- jitter(c(1:10), factor = 5)
y <- jitter(c(1:10), factor = 10)
fancy.lm(x, y, fit = TRUE, resid = TRUE)


More information about the R-help mailing list