\HeaderA{stack}{rearrange data in SpatialPointsDataFrame or SpatialGridDataFrame 
for plotting with spplot (levelplot/xyplot wrapper)}{stack}
\aliasA{map.to.lev}{stack}{map.to.lev}
\aliasA{stack.SpatialGridDataFrame}{stack}{stack.SpatialGridDataFrame}
\aliasA{stack.SpatialPointsDataFrame}{stack}{stack.SpatialPointsDataFrame}
\keyword{dplot}{stack}
\begin{Description}\relax
rearrange SpatialPointsDataFrame for plotting with spplot or levelplot
\end{Description}
\begin{Usage}
\begin{verbatim}
map.to.lev(data, zcol = 1:n, n = 2, names.attr)
stack.SpatialPointsDataFrame(x, select, ...)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{data}] object of class (or extending) SpatialDataFrame 
\item[\code{zcol}] z-coordinate column name(s), or a column number (range) (after
removing the spatial coordinate columns: 1 refers to the first non-coordinate
column, etc. ) 
\item[\code{names.attr}] names of the set of z-columns (these names
will appear in the plot); if omitted, column names of \code{zcol} 
\item[\code{n}] number of columns to be stacked 
\item[\code{x}] same as \code{data}
\item[\code{select}] same as \code{zcol}
\item[\code{...}] ignored 
\end{ldescription}
\end{Arguments}
\begin{Value}
\code{map.to.lev} returns a data frame with the following elements:
\begin{ldescription}
\item[\code{x}] x-coordinate for each row
\item[\code{y}] y-coordinate for each row
\item[\code{z}] column vector with each of the elements in columns \code{zcol}
of \code{data} stacked 
\item[\code{name}] factor; name of each of the stacked \code{z} columns 
\end{ldescription}
 

\code{stack} is an S3 method: it return a data.frame with a column
\code{values} that has the stacked coordinates and attributes, and a
column \code{ind} that indicates the variable stacked; it also
replicates the coordinates.
\end{Value}
\begin{SeeAlso}\relax
\LinkA{spplot}{spplot}, \LinkA{levelplot}{levelplot} in package \code{lattice}, and \LinkA{stack}{stack}
\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
library(lattice)
data(meuse.grid) # data frame
coordinates(meuse.grid) = c("x", "y") # promotes to SpatialDataFrame
meuse.grid[["idist"]] = 1 - meuse.grid[["dist"]] # add variable
# the following is made much easier by spplot:
levelplot(z~x+y|name, map.to.lev(meuse.grid, z=c("dist","idist"), names.attr =
        c("distance", "inverse of distance")), aspect = "iso")
levelplot(values~x+y|ind, as.data.frame(stack(meuse.grid)),aspect = "iso")
gridded(meuse.grid) = TRUE
levelplot(z~x+y|name, map.to.lev(meuse.grid, z=c("dist","idist"), names.attr =
        c("distance", "inverse of distance")), aspect = "iso")
levelplot(values~x+y|ind, as.data.frame(stack(meuse.grid)), asp = "iso")

\end{ExampleCode}
\end{Examples}

