[R] Conditionally Updating Lattice Plots

Bryan Hanson hanson at depauw.edu
Sun Jul 20 18:44:25 CEST 2008

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

TIF for any suggestions, Bryan

************ Example: a function then, the call to the function

fancy.lm <- function(x, y, fit = TRUE, resid = TRUE){
model <- lm(y ~ x)

y.pred <- predict(model) # Compute residuals for plotting
res.x <- as.vector(rbind(x, x, rep(NA,length(x)))) # NAs induce breaks in
res.y <- as.vector(rbind(y, y.pred, rep(NA,length(x)))) # after Fig 5.1 of
DAAG (clever!)

p <- xyplot(y ~ x, pch = 20,
    panel = function(...) {
        panel.xyplot(...) # not strictly necessary if I understand correctly

plot(p, more = TRUE)

if (fit) {
    plot(update(p, more = TRUE,
    panel = function(...){
        panel.abline(model, col = "red")
if (resid) {
    plot(update(p, more = TRUE,
    panel = function(...){
        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)

************ Session Info
> sessionInfo()
R version 2.7.1 (2008-06-23)


attached base packages:
[1] datasets  grid      grDevices graphics  stats     utils     methods
[8] base     

other attached packages:
 [1] fastICA_1.1-9         DescribeDisplay_0.1.3 ggplot_0.4.2
 [4] RColorBrewer_1.0-2    reshape_0.8.0         MASS_7.2-42
 [7] pcaPP_1.5             mvtnorm_0.9-0         hints_1.0.1-1
[10] mvoutlier_1.3         robustbase_0.2-8      lattice_0.17-8
[13] rggobi_2.1.9          RGtk2_2.12.5-3

loaded via a namespace (and not attached):
[1] tools_2.7.1


More information about the R-help mailing list