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. 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 line 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.xyplot(...) 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) i386-apple-darwin8.10.1 locale: en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8 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
Try this: library(lattice) 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 line res.y <- as.vector(rbind(y, y.pred, rep(NA,length(x)))) # after Fig 5.1 of DAAG (clever!) plot(xyplot(y ~ x, pch = 20)) trellis.focus() if (fit) panel.abline(model, col = "red") if (resid) panel.xyplot(res.x, res.y, col = "lightblue", type = "l") trellis.unfocus() } x <- jitter(c(1:10), factor = 5) y <- jitter(c(1:10), factor = 10) fancy.lm(x, y, fit = TRUE, resid = TRUE) On Sun, Jul 20, 2008 at 12:44 PM, 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. > > 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 > line > 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.xyplot(...) > 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) > i386-apple-darwin8.10.1 > > locale: > en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8 > > 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 > > > > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
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 follows: 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) -Deepayan