| 1 |
\name{spplot} |
| 2 |
\docType{methods} |
| 3 |
\alias{spplot} |
| 4 |
\alias{spplot.grid} |
| 5 |
\alias{spplot.locator} |
| 6 |
\alias{spplot.polygons} |
| 7 |
\alias{spplot.points} |
| 8 |
\alias{layout.north.arrow} |
| 9 |
\alias{layout.scale.bar} |
| 10 |
\alias{mapLegendGrob} |
| 11 |
\alias{sp.theme} |
| 12 |
\alias{spplot-methods} |
| 13 |
\alias{spplot,SpatialPixelsDataFrame-method} |
| 14 |
\alias{spplot,SpatialGridDataFrame-method} |
| 15 |
\alias{spplot,SpatialPolygonsDataFrame-method} |
| 16 |
\alias{spplot,SpatialLinesDataFrame-method} |
| 17 |
\alias{spplot,SpatialPointsDataFrame-method} |
| 18 |
\alias{spplot,SpatialMultiPointsDataFrame-method} |
| 19 |
\alias{sppanel} |
| 20 |
\alias{sppanel,Line-method} |
| 21 |
\alias{sppanel,Lines-method} |
| 22 |
\alias{sppanel,NULL-method} |
| 23 |
\alias{sppanel,SpatialGrid-method} |
| 24 |
\alias{sppanel,SpatialLines-method} |
| 25 |
\alias{sppanel,SpatialPixels-method} |
| 26 |
\alias{sppanel,SpatialPoints-method} |
| 27 |
\alias{sppanel,SpatialPolygons-method} |
| 28 |
\alias{sppanel,character-method} |
| 29 |
\alias{sppanel,list-method} |
| 30 |
\alias{panel.RgoogleMaps} |
| 31 |
\alias{panel.ggmap} |
| 32 |
\alias{sppanel-methods} |
| 33 |
\alias{set_col_regions} |
| 34 |
\alias{get_col_regions} |
| 35 |
|
| 36 |
\title{Plot methods for spatial data with attributes} |
| 37 |
\description{Lattice (trellis) plot methods for spatial data with attributes} |
| 38 |
\usage{ |
| 39 |
spplot(obj, ...) |
| 40 |
spplot.grid(obj, zcol = names(obj), ..., names.attr, scales = list(draw = FALSE), |
| 41 |
xlab = NULL, ylab = NULL, aspect = mapasp(obj,xlim,ylim), |
| 42 |
panel = panel.gridplot, sp.layout = NULL, formula, xlim = bbox(obj)[1, ], |
| 43 |
ylim = bbox(obj)[2, ], checkEmptyRC = TRUE, col.regions = get_col_regions()) |
| 44 |
spplot.polygons(obj, zcol = names(obj), ..., names.attr, scales = list(draw = FALSE), |
| 45 |
xlab = NULL, ylab = NULL, aspect = mapasp(obj,xlim,ylim), |
| 46 |
panel = panel.polygonsplot, sp.layout = NULL, formula, xlim = bbox(obj)[1, ], |
| 47 |
ylim = bbox(obj)[2, ], col.regions = get_col_regions()) |
| 48 |
spplot.points(obj, zcol = names(obj), ..., names.attr, scales = list(draw = FALSE), |
| 49 |
xlab = NULL, ylab = NULL, aspect = mapasp(obj,xlim,ylim), |
| 50 |
panel = panel.pointsplot, sp.layout = NULL, identify = FALSE, formula, |
| 51 |
xlim = bbexpand(bbox(obj)[1, ], 0.04), ylim = bbexpand(bbox(obj)[2, ], 0.04), |
| 52 |
edge.col = "transparent", colorkey = FALSE, col.regions = get_col_regions()) |
| 53 |
mapLegendGrob(obj, widths = unit(1, "cm"), heights = unit(1, "cm"), |
| 54 |
fill = "black", just = "right") |
| 55 |
sp.theme(set = FALSE, regions = list(col = bpy.colors(100)), ...) |
| 56 |
layout.north.arrow(type = 1) |
| 57 |
layout.scale.bar(height = 0.05) |
| 58 |
spplot.locator(n = 512, type = "n", ...) |
| 59 |
set_col_regions(value) |
| 60 |
get_col_regions() |
| 61 |
} |
| 62 |
\arguments{ |
| 63 |
\item{obj}{ object of class extending \link{Spatial-class}} |
| 64 |
\item{zcol}{ character; attribute name(s) or column number(s) in attribute table } |
| 65 |
\item{names.attr}{ names to use in panel, if different from zcol names } |
| 66 |
\item{scales}{ scales argument to be passed to Lattice plots; use |
| 67 |
\code{list(draw = TRUE)} to draw axes scales; see \link[lattice]{xyplot} for full options } |
| 68 |
\item{...}{ other arguments passed to \link[lattice]{levelplot} (grids, polygons) or |
| 69 |
\link[lattice]{xyplot} (points) } |
| 70 |
\item{xlab}{label for x-axis } |
| 71 |
\item{ylab}{label for y-axis } |
| 72 |
\item{aspect}{aspect ratio for spatial axes; defaults to "iso" (one unit |
| 73 |
on the x-axis equals one unit on the y-axis) but may be set to more |
| 74 |
suitable values if the data are e.g. if coordinates are latitude/longitude } |
| 75 |
\item{panel}{ depending on the class of obj, \link{panel.polygonsplot} (for |
| 76 |
polygons or lines), \link{panel.gridplot} (grids) or \link{panel.pointsplot} |
| 77 |
(points) is used; for further control custom panel functions can be |
| 78 |
supplied that call one of these panel functions, but do read below how the |
| 79 |
argument \code{sp.layout} may help } |
| 80 |
\item{sp.layout}{NULL or list; see notes below} |
| 81 |
\item{identify}{ if not FALSE, identify plotted objects (currently only working |
| 82 |
for points plots). Labels for identification are the row.names of the |
| 83 |
attribute table \code{row.names(as.data.frame(obj))}. If TRUE, identify |
| 84 |
on panel \code{(1,1)}; for identifying on panel \code{i,j}, pass the |
| 85 |
value \code{c(i,j)}} |
| 86 |
\item{formula}{optional; may be useful to plot a transformed value. Defaults |
| 87 |
to \code{z~x+y} for single and \code{z~x+y|name} for multiple attributes; use |
| 88 |
e.g. \code{exp(x)~x+y|name} to plot the exponent of the z-variable} |
| 89 |
\item{xlim}{ numeric; x-axis limits } |
| 90 |
\item{ylim}{ numeric; y-axis limits } |
| 91 |
\item{edge.col}{ color of symbol edge } |
| 92 |
\item{colorkey}{ if FALSE, use symbol key; if TRUE, use continuous, levelplot-like colorkey; if list, follow syntax of argument \code{colorkey} in \link[lattice:interaction]{levelplot} (see below for an example) } |
| 93 |
\item{widths}{width of grob} |
| 94 |
\item{heights}{heights of grob} |
| 95 |
\item{fill}{fill color of grob} |
| 96 |
\item{just}{grob placement justification} |
| 97 |
\item{set}{ logical; if TRUE, trellis.par.set is called, else a list is returned |
| 98 |
that can be passed to trellis.par.set() } |
| 99 |
\item{regions}{ color ramp for the theme } |
| 100 |
\item{height}{height of scale bar; width is 1.0} |
| 101 |
\item{n}{see locator} |
| 102 |
\item{type}{see locator} |
| 103 |
\item{checkEmptyRC}{logical; if TRUE, a check is done to see if |
| 104 |
empty rows or columns are present, and need to be taken care of. Setting |
| 105 |
to FALSE may improve speed. } |
| 106 |
\item{col.regions}{ vector with fill colours; in case the variable |
| 107 |
to be plotted is a factor, this vector should have length equal to the |
| 108 |
number of factor levels } |
| 109 |
\item{value}{ vector with color values, default for \code{col.regions}} |
| 110 |
} |
| 111 |
\value{ |
| 112 |
spplot returns a lattice plot of class "trellis", if you fail to "see" it, |
| 113 |
explicitly call \code{print(spplot(...))}. If \code{identify} is TRUE, |
| 114 |
the plot is plotted and the return value is a vector with row names of |
| 115 |
the selected points. |
| 116 |
|
| 117 |
\code{spplot.locator} returns a matrix with identified point locations; |
| 118 |
use \code{trellis.focus} first to focus on a given panel. |
| 119 |
|
| 120 |
\code{get_col_regions} returns the default value for \code{col.regions} |
| 121 |
} |
| 122 |
|
| 123 |
\section{Methods}{ |
| 124 |
\describe{ |
| 125 |
\item{obj = "SpatialPixelsDataFrame"}{ see \link{spplot} } |
| 126 |
\item{obj = "SpatialGridDataFrame"}{ see \link{spplot} } |
| 127 |
\item{obj = "SpatialPolygonsDataFrame"}{ see \link{spplot} } |
| 128 |
\item{obj = "SpatialLinesDataFrame"}{ see \link{spplot} } |
| 129 |
\item{obj = "SpatialPointsDataFrame"}{ see \link{spplot} } |
| 130 |
}} |
| 131 |
|
| 132 |
\note{ |
| 133 |
Missing values in the attributes are (currently) not allowed. |
| 134 |
|
| 135 |
\code{spplot.grid}, \code{spplot.polygons} and \code{spplot.points} are S4 |
| 136 |
methods for \code{spplot}; see \link{spplot-methods}. |
| 137 |
|
| 138 |
Useful arguments that can be passed as \code{...} are: |
| 139 |
\describe{ |
| 140 |
\item{\code{layout}}{integer; for the layout of panels (cols,rows)} |
| 141 |
\item{\code{pretty}}{logical; choose colour breaks at pretty numbers?} |
| 142 |
\item{\code{at}}{specify at which values colours change} |
| 143 |
\item{\code{as.table}}{logical; start drawing panels upper-left instead |
| 144 |
of lower-left} |
| 145 |
\item{\code{page}}{to add marks to each plotted page} |
| 146 |
} |
| 147 |
|
| 148 |
for useful values see the appropriate documentation of \link[lattice]{xyplot} |
| 149 |
(in case of points), and \link[lattice]{levelplot} (otherwise). |
| 150 |
|
| 151 |
If \code{obj} is of SpatialPointsDataFrame, the following options are |
| 152 |
useful to pass: |
| 153 |
\describe{ |
| 154 |
\item{\code{key.space}}{ character: "bottom", "right", "left" or "right" to denote |
| 155 |
key location, or list: see argument key in the help for \link[lattice]{xyplot} what |
| 156 |
the options are} |
| 157 |
\item{\code{legendEntries}}{ character; array with key legend (text) entries; |
| 158 |
suitable defaults obtained from data } |
| 159 |
\item{\code{cuts}}{number of cuts, or, for objects of class \link{SpatialPointsDataFrame} only, the actual cuts to use} |
| 160 |
\item{\code{do.log}}{logical; if TRUE use log-linear scale to divide range in equal cuts, |
| 161 |
else use a linear scale if \code{cuts} is only number of cuts } |
| 162 |
\item{\code{pch}}{ integer; plotting character to use; defaults to 16 if fill is TRUE, else 1 } |
| 163 |
\item{\code{cex}}{numeric; character expansion, proportional to default value of 1} |
| 164 |
\item{\code{fill}}{ logical; use filled circles? } |
| 165 |
} |
| 166 |
|
| 167 |
\code{layout.north.arrow} and \code{layout.scale.bar} can be used |
| 168 |
to set a north arrow or scale bar. |
| 169 |
|
| 170 |
The \code{sp.layout} argument is either a single layout item, or a list |
| 171 |
with one or more layout items. A layout item is one of |
| 172 |
\itemize{ |
| 173 |
\item{a list with one or more \code{Spatial*} objects, along with |
| 174 |
style arguments like \code{col}, \code{lty}, \code{pch}, \code{fill} etc.} |
| 175 |
\item{a list with its first argument the layout function or |
| 176 |
the name of the layout function to be called: \code{sp.points} for |
| 177 |
SpatialPoints, \code{sp.polygons} for SpatialPolygons object, \code{sp.lines} |
| 178 |
for a SpatialLines object, and \code{sp.text} for text to place. The |
| 179 |
second argument contains the object (or text) to be plotted; remaining |
| 180 |
arguments are passed to the corresponding \code{panel.*} functions.}} |
| 181 |
|
| 182 |
The order of items in \code{sp.layout} matters; objects are drawn in the |
| 183 |
order they appear. With respect to \code{obj}, default plot order and |
| 184 |
precedence of \code{sp.layout} items is as follows: for points and |
| 185 |
lines, \code{sp.layout} items are drawn over (after) \code{obj}; for |
| 186 |
grids and polygons, \code{sp.layout} items are drawn behind (before) |
| 187 |
\code{obj}. Transparency may further help making multiple things |
| 188 |
visible. Adding a \code{first} argument to a layout item overrides its |
| 189 |
default plotting order with respect to \code{obj}: |
| 190 |
|
| 191 |
Special control elements of \code{sp.layout} items: |
| 192 |
\describe{ |
| 193 |
\item{\code{first}}{logical; should the layout item be drawn before |
| 194 |
the \code{obj} (TRUE), or after (FALSE)? This overrides |
| 195 |
the default order (points and lines in front, polygons and grids behind). } |
| 196 |
\item{\code{which}}{integer; controls to which panel a layout |
| 197 |
item should be added. If \code{which} is present in the main, |
| 198 |
top-level list it applies to all layout items; in sub-lists with |
| 199 |
layout items it denotes the (set of) panel(s) in which the layout |
| 200 |
item should be drawn. Without a \code{which} item, layout items |
| 201 |
are drawn in each panel.} |
| 202 |
} |
| 203 |
|
| 204 |
\code{sp.theme} returns a lattice theme; use, after loading package |
| 205 |
\code{lattice}, the command \code{trellis.par.set(sp.theme())} |
| 206 |
after a device is opened or changed to make this work. Currently, this only |
| 207 |
sets the colors to \link{bpy.colors}. |
| 208 |
|
| 209 |
If the attributes to be plotted are of type factor, spplot tries |
| 210 |
to create a legend that reflects this. In this case, the color ramp |
| 211 |
passed needs to be of the same length as the number of factor levels. |
| 212 |
The factor levels are derived from the first map; subsequent factors |
| 213 |
with different factor levels result in an error. |
| 214 |
|
| 215 |
} |
| 216 |
|
| 217 |
\references{ |
| 218 |
\url{https://edzer.github.io/sp/} |
| 219 |
} |
| 220 |
\seealso{\link[lattice]{xyplot}, \link[lattice]{levelplot}, |
| 221 |
\link[lattice:interaction]{panel.identify} to identify objects } |
| 222 |
\author{ |
| 223 |
Edzer Pebesma, \email{[email protected]} |
| 224 |
} |
| 225 |
\examples{ |
| 226 |
library(lattice) |
| 227 |
trellis.par.set(sp.theme()) # sets bpy.colors() ramp |
| 228 |
demo(meuse, ask = FALSE, echo = FALSE) |
| 229 |
l2 = list("SpatialPolygonsRescale", layout.north.arrow(), offset = c(181300,329800), |
| 230 |
scale = 400) |
| 231 |
l3 = list("SpatialPolygonsRescale", layout.scale.bar(), offset = c(180500,329800), |
| 232 |
scale = 500, fill=c("transparent","black")) |
| 233 |
l4 = list("sp.text", c(180500,329900), "0") |
| 234 |
l5 = list("sp.text", c(181000,329900), "500 m") |
| 235 |
|
| 236 |
spplot(meuse, c("ffreq"), sp.layout=list(l2,l3,l4,l5), col.regions= "black", |
| 237 |
pch=c(1,2,3), key.space=list(x=0.1,y=.95,corner=c(0,1))) |
| 238 |
spplot(meuse, c("zinc", "lead"), sp.layout=list(l2,l3,l4,l5, which = 2), |
| 239 |
key.space=list(x=0.1,y=.95,corner=c(0,1))) |
| 240 |
# plotting factors: |
| 241 |
meuse$f = factor(sample(letters[6:10], 155, replace=TRUE),levels=letters[1:10]) |
| 242 |
meuse$g = factor(sample(letters[1:5], 155, replace=TRUE),levels=letters[1:10]) |
| 243 |
spplot(meuse, c("f","g"), col.regions=bpy.colors(10)) |
| 244 |
|
| 245 |
if (require(RColorBrewer)) { |
| 246 |
spplot(meuse, c("ffreq"), sp.layout=list(l2,l3,l4,l5), |
| 247 |
col.regions=brewer.pal(3, "Set1")) |
| 248 |
} |
| 249 |
|
| 250 |
meuse.grid$g = factor(sample(letters[1:5], 3103, replace=TRUE), |
| 251 |
levels=letters[1:10]) |
| 252 |
meuse.grid$f = factor(sample(letters[6:10], 3103, replace=TRUE), |
| 253 |
levels=letters[1:10]) |
| 254 |
spplot(meuse.grid, c("f","g"), col.regions=bpy.colors(10)) |
| 255 |
|
| 256 |
# example modifying colorkey for points: |
| 257 |
spplot(meuse["dist"], colorkey = list( |
| 258 |
right = list( # see ?levelplot in package trellis, argument colorkey: |
| 259 |
fun = draw.colorkey, |
| 260 |
args = list( |
| 261 |
key = list( |
| 262 |
at = seq(0, 1, .1), # colour breaks |
| 263 |
col = bpy.colors(11), # colours |
| 264 |
labels = list( |
| 265 |
at = c(0, .2, .4, .6, .8, 1), |
| 266 |
labels = c("0x", "20x", "40x", "60x", "80x", "100x") |
| 267 |
) |
| 268 |
) |
| 269 |
) |
| 270 |
) |
| 271 |
)) |
| 272 |
l6 = list(meuse.grid["dist"], col = grey(seq(.5,.9,length.out=10))) |
| 273 |
spplot(meuse, c("zinc", "lead"), sp.layout = l6) |
| 274 |
spplot(meuse, c("zinc", "lead"), |
| 275 |
sp.layout = list(meuse.grid, meuse.riv, col = 'grey')) |
| 276 |
|
| 277 |
# Custom legend placement, taken from |
| 278 |
# http://stackoverflow.com/questions/29344692/custom-placement-of-spplot-legend-in-the-map |
| 279 |
s <- spplot(meuse.grid[,'dist'], colorkey = list(space = "left", height = 0.4)) |
| 280 |
|
| 281 |
args <- s$legend$left$args$key |
| 282 |
|
| 283 |
## Prepare list of arguments needed by `legend=` argument (as described in ?xyplot) |
| 284 |
library(lattice) # draw.colorkey |
| 285 |
legendArgs <- list(fun = draw.colorkey, |
| 286 |
args = list(key = args), |
| 287 |
corner = c(0.05,.75)) |
| 288 |
|
| 289 |
## Call spplot() again, this time passing in to legend the arguments |
| 290 |
## needed to print a color key |
| 291 |
spplot(meuse.grid[,'dist'], colorkey = FALSE, |
| 292 |
legend = list(inside = legendArgs)) |
| 293 |
} |
| 294 |
\keyword{dplot} |