Rev 5264 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
\name{E_interaction}\alias{panel.identify}\alias{panel.identify.qqmath}\alias{panel.identify.cloud}\alias{panel.link.splom}\alias{panel.brush.splom}\alias{trellis.focus}\alias{trellis.unfocus}\alias{trellis.switchFocus}\alias{trellis.panelArgs}\alias{trellis.vpname}\alias{trellis.grobname}\concept{interaction}\concept{augment}\title{Functions to Interact with Lattice Plots}\description{The classic Trellis paradigm is to plot the whole object at once,without the possibility of interacting with it afterwards. However,by keeping track of the grid viewports where the panels and strips aredrawn, it is possible to go back to them afterwards and enhance themone panel at a time. These functions provide convenient interfaces tohelp in this. Note that these are still experimental and the exactdetails may change in future.}\usage{panel.identify(x, y = NULL,subscripts = seq_along(x),labels = subscripts,n = length(x), offset = 0.5,threshold = 18, ## in points, roughly 0.25 inchespanel.args = trellis.panelArgs(),\dots)panel.identify.qqmath(x, distribution, groups, subscripts, labels,panel.args = trellis.panelArgs(),\dots)panel.identify.cloud(x, y, z, subscripts,perspective, distance,xlim, ylim, zlim,screen, R.mat, aspect, scales.3d,\dots,panel.3d.identify,n = length(subscripts),offset = 0.5,threshold = 18,labels = subscripts,panel.args = trellis.panelArgs())panel.link.splom(threshold = 18, verbose = getOption("verbose"), \dots)panel.brush.splom(threshold = 18, verbose = getOption("verbose"), \dots)trellis.vpname(name = c("position", "split", "split.location", "toplevel","figure", "panel", "strip", "strip.left", "legend","main", "sub", "xlab", "ylab", "page"),column, row,side = c("left", "top", "right", "bottom", "inside"),clip.off = FALSE, prefix)trellis.grobname(name, prefix)trellis.focus(name, column, row, side, clip.off,highlight = interactive(), \dots,guess = TRUE, verbose = getOption("verbose"))trellis.switchFocus(name, side, clip.off, highlight, \dots)trellis.unfocus()trellis.panelArgs(x, packet.number)}\arguments{\item{x, y, z}{ variables defining the contents of the panel. In thecase of \code{trellis.panelArgs}, a \code{"trellis"} object. }\item{n}{the number of points to identify by default (overridden by a rightclick)}\item{subscripts}{an optional vector of integer indices associated with each point.See details below.}\item{labels}{an optional vector of labels associated with each point. Defaultsto \code{subscripts}}\item{distribution, groups}{ typical panel arguments of\code{\link{panel.qqmath}}. These will usually be obtained from\code{panel.args}}\item{offset}{the labels are printed either below, above, to the left or to theright of the identified point, depending on the relative location ofthe mouse click. The \code{offset} specifies (in "char" units) howfar from the identified point the labels should be printed.}\item{threshold}{threshold in grid's \code{"points"} units. Points further than thesefrom the mouse click position are not considered}\item{panel.args}{list that contains components names \code{x} (and usually \code{y}),to be used if \code{x} is missing. Typically, when called after\code{trellis.focus}, this would appropriately be the argumentspassed to that panel.}\item{perspective, distance, xlim, ylim, zlim, screen, R.mat, aspect,scales.3d}{arguments as passed to \code{\link{panel.cloud}}. These arerequired to recompute the relevant three-dimensional projections in\code{panel.identify.cloud}.}\item{panel.3d.identify}{the function that is responsible for the actual interaction once thedata rescaling and rotation computations have been done. Bydefault, an internal function similar to \code{panel.identify} isused.}\item{name}{character string indicating which viewport or grob we are lookingfor. Although these do not necessarily provide access to allviewports and grobs created by a lattice plot, they cover most thatusers might find interesting.\code{trellis.vpname} and \code{trellis.focus} deal with viewportnames only, and only accept the values explicitly listed above.\code{trellis.grobname} is meant to create names for grobs, and cancurrently accept any value.If \code{name}, as well as \code{column} and \code{row} is missingin a call to \code{trellis.focus}, the user can click inside a panel(or an associated strip) to focus on that panel. Note however thatthis assumes equal width and height for each panel, and may not workwhen this is not true.When \code{name} is \code{"panel"}, \code{"strip"}, or\code{"strip.left"}, \code{column} and \code{row} must also bespecified. When \code{name} is \code{"legend"}, \code{side} mustalso be specified.}\item{column, row}{integers, indicating position of the panel or strip that should beassigned focus in the Trellis layout. Rows are usually calculatedfrom the bottom up, unless the plot was created with\code{as.table=TRUE}}\item{guess}{logical. If \code{TRUE}, and the display has only one panel, thatpanel will be automatically selected by a call to\code{trellis.focus}.}\item{side}{character string, relevant only for legends (i.e., when\code{name="legend"}), indicating their position. Partial specificationis allowed, as long as it is unambiguous.}\item{clip.off}{logical, whether clipping should be off, relevant when \code{name}is \code{"panel"} or \code{"strip"}. This is necessary if axes areto be drawn outside the panel or strip. Note that setting\code{clip.off=FALSE} does not necessarily mean that clipping is on;that is determined by conditions in effect during printing.}\item{prefix}{character string acting as a prefix, meant to distinguish otherwiseequivalent viewports in different plots. This only becomes relevantwhen a particular page is occupied by more than one plot. Defaultsto the value appropriate for the last \code{"trellis"} object printed, asdetermined by the \code{prefix} argument in\code{\link{print.trellis}}. Users should not usually need tosupply a value for this argument (see note below), however, ifsupplied explicitly, this has to be a valid R symbol name (briefly,it must start with a letter or a period followed by a letter) andmust not contain the grid path separator (currently \code{"::"})}\item{highlight}{logical, whether the viewport being assigned focus should behighlighted. For \code{trellis.focus}, the default is \code{TRUE}in interactive mode, and \code{trellis.switchFocus} by defaultpreserves the setting currently active.}\item{packet.number}{integer, which panel to get data from. See\code{\link{packet.number}} for details on how this is calculated}\item{verbose}{ whether details will be printed }\item{\dots}{For \code{panel.identify.qqmath}, extra parameters are passed on to\code{panel.identify}. For \code{panel.identify}, extra argumentsare treated as graphical parameters and are used for labelling. For\code{trellis.focus} and \code{trellis.switchFocus}, these are used(in combination with \code{\link{lattice.options}}) for highlightingthe chosen viewport if so requested. Graphical parameters can besupplied for \code{panel.link.splom}.}}\details{\code{panel.identify} is similar to \code{\link{identify}}. Whencalled, it waits for the user to identify points (in the panel beingdrawn) via mouse clicks. Clicks other than left-clicks terminate theprocedure. Although it is possible to call it as part of the panelfunction, it is more typical to use it to identify points afterplotting the whole object, in which case a call to\code{trellis.focus} first is necessary.\code{panel.link.splom} is meant for use with \code{\link{splom}},and requires a panel to be chosen using \code{trellis.focus} before itis called. Clicking on a point causes that and the correspondingproections in other pairwise scatter plots to be highlighted.\code{panel.brush.splom} is a (misnamed) alias for\code{panel.link.splom}, retained for back-compatibility.\code{panel.identify.qqmath} is a specialized wrapper meant for usewith the display produced by \code{\link{qqmath}}.\code{panel.identify.qqmath} is a specialized wrapper meant for usewith the display produced by \code{\link{cloud}}. It would be unusualto call them except in a context where default panel functionarguments are available through \code{trellis.panelArgs} (see below).One way in which \code{panel.identify} etc. are different from\code{\link{identify}} is in how it uses the \code{subscripts}argument. In general, when one identifies points in a panel, onewants to identify the origin in the data frame used to produce theplot, and not within that particular panel. This information isavailable to the panel function, but only in certain situations. Oneway to ensure that \code{subscripts} is available is to specify\code{subscripts = TRUE} in the high level call such as \code{xyplot}.If \code{subscripts} is not explicitly specified in the call to\code{panel.identify}, but is available in \code{panel.args}, thenthose values will be used. Otherwise, they default to\code{seq_along(x)}. In either case, the final return value will bethe subscripts that were marked.The process of printing (plotting) a Trellis object builds up a gridlayout with named viewports which can then be accessed to modify theplot further. While full flexibility can only be obtained by usinggrid functions directly, a few lattice functions are available for themore common tasks.\code{trellis.focus} can be used to move to a particular panel orstrip, identified by its position in the array of panels. It can alsobe used to focus on the viewport corresponding to one of the labels ora legend, though such usage would be less useful. The exactviewport is determined by the \code{name} along with the otherarguments, not all of which are relevant for all names. Note thatwhen more than one object is plotted on a page, \code{trellis.focus}will always go to the plot that was created last. For moreflexibility, use grid functions directly (see note below).After a successful call to \code{trellis.focus}, the desired viewport(typically panel or strip area) will be made the \sQuote{current}viewport (plotting area), which can then be enhanced by calls tostandard lattice panel functions as well as grid functions.It is quite common to have the layout of panels chosen when a\code{"trellis"} object is drawn, and not before then. Information onthe layout (specifically, how many rows and columns, and which packetbelongs in which position in this layout) is retained for the last\code{"trellis"} object plotted, and is available through\code{trellis.currentLayout}.\code{trellis.unfocus} unsets the focus, and makes the top levelviewport the current viewport.\code{trellis.switchFocus} is a convenience function to switch fromone viewport to another, while preserving the current \code{row} and\code{column}. Although the rows and columns only make sense forpanels and strips, they would be preserved even when the user switchesto some other viewport (where row/column is irrelevant) and thenswitches back.Once a panel or strip is in focus, \code{trellis.panelArgs} can beused to retrieve the arguments that were available to the panelfunction at that position. In this case, it can be called withoutarguments as\preformatted{trellis.panelArgs()}This usage is also allowed when a \code{"trellis"} object is beingprinted, e.g. inside the panel functions or the axis function (but notinside the prepanel function). \code{trellis.panelArgs} can alsoretrieve the panel arguments from any \code{"trellis"} object. Notethat for this usage, one needs to specify the \code{packet.number} (asdescribed under the \code{panel} entry in \code{\link{xyplot}}) andnot the position in the layout, because a layout determines the panelonly \bold{after} the object has been printed.It is usually not necessary to call \code{trellis.vpname} and\code{trellis.grobname} directly. However, they can be useful ingenerating appropriate names in a portable way when using gridfunctions to interact with the plots directly, as described in thenote below.}\value{\code{panel.identify} returns an integer vector containing thesubscripts of the identified points (see details above). Theequivalent of \code{identify} with \code{pos=TRUE} is not yetimplemented, but can be considered for addition if requested.\code{trellis.panelArgs} returns a named list of arguments that wereavailable to the panel function for the chosen panel.\code{trellis.vpname} and \code{trellis.grobname} return characterstrings.\code{trellis.focus} has a meaningful return value only if it has beenused to focus on a panel interactively, in which case the return valueis a list with components \code{col} and \code{row} giving the columnand row positions respectively of the chosen panel, unless the choicewas cancelled (by a right click), in which case the return value is\code{NULL}. If click was outside a panel, both \code{col} and\code{row} are set to 0.}\note{The viewports created by lattice is accessible to the user only up to acertain extent, as described above. In particular,\code{trellis.focus} can only be used to manipulate the last plotdrawn. For full flexibility, use appropriate functions from the gridpackage directly. For example,\code{\link[grid:current.viewport]{current.vpTree}} can be used toinspect the current viewport tree and\code{\link[grid:viewports]{seekViewport}} or\code{\link[grid:viewports]{downViewport}} can be used to navigate tothese viewports. For such usage, \code{trellis.vpname} and\code{trellis.grobname} (with a non-default \code{prefix} argument)provides a portable way to access the appropriate viewports and grobsby name.}\examples{\dontrun{xyplot(1:10 ~ 1:10)trellis.focus("panel", 1, 1)panel.identify()}xyplot(Petal.Length ~ Sepal.Length | Species, iris, layout = c(2, 2))Sys.sleep(1)trellis.focus("panel", 1, 1)do.call("panel.lmline", trellis.panelArgs())Sys.sleep(0.5)trellis.unfocus()trellis.focus("panel", 2, 1)do.call("panel.lmline", trellis.panelArgs())Sys.sleep(0.5)trellis.unfocus()trellis.focus("panel", 1, 2)do.call("panel.lmline", trellis.panelArgs())Sys.sleep(0.5)trellis.unfocus()## choosing loess smoothing parameterp <- xyplot(dist ~ speed, cars)panel.loessresid <-function(x = panel.args$x,y = panel.args$y,span,panel.args = trellis.panelArgs()){fm <- loess(y ~ x, span = span)xgrid <- do.breaks(current.panel.limits()$xlim, 50)ygrid <- predict(fm, newdata = data.frame(x = xgrid))panel.lines(xgrid, ygrid)pred <- predict(fm)## center residuals so that they fall inside panelresids <- y - pred + mean(y)fm.resid <- loess.smooth(x, resids, span = span)##panel.points(x, resids, col = 1, pch = 4)panel.lines(fm.resid, col = 1)}spans <- c(0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8)update(p, index.cond = list(rep(1, length(spans))))panel.locs <- trellis.currentLayout()i <- 1for (row in 1:nrow(panel.locs))for (column in 1:ncol(panel.locs))if (panel.locs[row, column] > 0){trellis.focus("panel", row = row, column = column,highlight = FALSE)panel.loessresid(span = spans[i])grid::grid.text(paste("span = ", spans[i]),x = 0.25,y = 0.75,default.units = "npc")trellis.unfocus()i <- i + 1}}\seealso{\code{\link{identify}}, \code{\link{Lattice}},\code{\link{print.trellis}}, \code{\link{trellis.currentLayout}},\code{\link[grid:current.viewport]{current.vpTree}},\code{\link[grid:viewports]{viewports}}}\author{ Deepayan Sarkar \email{Deepayan.Sarkar@R-project.org}. FelixAndrews provided initial implementations of\code{panel.identify.qqmath} and support for focusing on panelsinterctively.}\keyword{dplot}