1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
|
library(sp)
library(lattice) # required for trellis.par.set():
trellis.par.set(sp.theme()) # sets color ramp to bpy.colors()
data(meuse)
coordinates(meuse)=~x+y
## coloured points plot with legend in plotting area and scales:
spplot(meuse, "zinc", do.log = TRUE,
key.space=list(x=0.2,y=0.9,corner=c(0,1)),
scales=list(draw=T))
library(sp)
library(lattice) # required for trellis.par.set():
trellis.par.set(sp.theme()) # sets color ramp to bpy.colors()
data(meuse)
coordinates(meuse)=~x+y
## coloured points plot with legend in plotting area and scales;
## non-default number of cuts with user-supplied legend entries:
spplot(meuse, "zinc", do.log = TRUE,
key.space=list(x=0.2,y=0.9,corner=c(0,1)),
scales=list(draw=T), cuts = 3,
legendEntries = c("low", "intermediate", "high"))
library(sp)
library(lattice) # required for trellis.par.set():
trellis.par.set(sp.theme()) # sets color ramp to bpy.colors()
data(meuse)
coordinates(meuse)=~x+y
scale = list("SpatialPolygonsRescale", layout.scale.bar(),
offset = c(178600,332990), scale = 500, fill=c("transparent","black"))
text1 = list("sp.text", c(178600,333090), "0")
text2 = list("sp.text", c(179100,333090), "500 m")
arrow = list("SpatialPolygonsRescale", layout.north.arrow(),
offset = c(178750,332500), scale = 400)
## points plot with scale bar, scale bar text, north arrow and title:
spplot(meuse, "zinc", do.log=T,
key.space=list(x=0.1,y=0.93,corner=c(0,1)),
sp.layout=list(scale,text1,text2,arrow),
main = "Zinc (top soil)")
library(sp)
library(lattice) # required for trellis.par.set():
trellis.par.set(sp.theme()) # sets color ramp to bpy.colors()
data(meuse)
coordinates(meuse)=~x+y
data(meuse.riv)
meuse.sr = SpatialPolygons(list(Polygons(list(Polygon(meuse.riv)),"meuse.riv")))
rv = list("sp.polygons", meuse.sr, fill = "lightblue")
scale = list("SpatialPolygonsRescale", layout.scale.bar(),
offset = c(180500,329800), scale = 500, fill=c("transparent","black"), which = 1)
text1 = list("sp.text", c(180500,329900), "0", which = 1)
text2 = list("sp.text", c(181000,329900), "500 m", which = 1)
arrow = list("SpatialPolygonsRescale", layout.north.arrow(),
offset = c(178750,332500), scale = 400)
## plot with north arrow and text outside panels
## (scale can, as of yet, not be plotted outside panels)
spplot(meuse["zinc"], do.log = TRUE,
key.space = "bottom",
sp.layout = list(rv, scale, text1, text2),
main = "Zinc (top soil)",
legend = list(right = list(fun = mapLegendGrob(layout.north.arrow()))))
library(sp)
library(lattice) # required for trellis.par.set():
trellis.par.set(sp.theme()) # sets color ramp to bpy.colors()
data(meuse)
coordinates(meuse)=~x+y
data(meuse.riv)
meuse.sr = SpatialPolygons(list(Polygons(list(Polygon(meuse.riv)),"meuse.riv")))
## same plot; north arrow now inside panel, custom panel function instead of sp.layout
spplot(meuse, "zinc", panel = function(x, y, ...) {
sp.polygons(meuse.sr, fill = "lightblue")
SpatialPolygonsRescale(layout.scale.bar(), offset = c(179900,329600),
scale = 500, fill=c("transparent","black"))
sp.text(c(179900,329700), "0")
sp.text(c(180400,329700), "500 m")
SpatialPolygonsRescale(layout.north.arrow(),
offset = c(178750,332500), scale = 400)
panel.pointsplot(x, y, ...)
},
do.log = TRUE, cuts = 7,
key.space = list(x = 0.1, y = 0.93, corner = c(0,1)),
main = "Top soil zinc concentration (ppm)")
library(sp)
library(lattice) # required for trellis.par.set():
trellis.par.set(sp.theme()) # sets color ramp to bpy.colors()
data(meuse)
coordinates(meuse)=~x+y
data(meuse.riv)
meuse.sr = SpatialPolygons(list(Polygons(list(Polygon(meuse.riv)),"meuse.riv")))
rv = list("sp.polygons", meuse.sr, fill = "lightblue")
## multi-panel plot, scales + north arrow only in last plot:
## using the "which" argument in a layout component
## (if which=4 was set as list component of sp.layout, the river
## would as well be drawn only in that (last) panel)
scale = list("SpatialPolygonsRescale", layout.scale.bar(),
offset = c(180500,329800), scale = 500, fill=c("transparent","black"), which = 4)
text1 = list("sp.text", c(180500,329900), "0", cex = .5, which = 4)
text2 = list("sp.text", c(181000,329900), "500 m", cex = .5, which = 4)
arrow = list("SpatialPolygonsRescale", layout.north.arrow(),
offset = c(181300,329800),
scale = 400, which = 4)
cuts = c(.2,.5,1,2,5,10,20,50,100,200,500,1000,2000)
spplot(meuse, c("cadmium", "copper", "lead", "zinc"), do.log = TRUE,
key.space = "right", as.table = TRUE,
sp.layout=list(rv, scale, text1, text2, arrow), # note that rv is up front!
main = "Heavy metals (top soil), ppm", cex = .7, cuts = cuts)
library(sp)
library(lattice) # required for trellis.par.set():
trellis.par.set(sp.theme()) # sets color ramp to bpy.colors()
alphaChannelSupported = function() {
!is.na(match(names(dev.cur()), c("pdf")))
}
data(meuse)
coordinates(meuse)=~x+y
data(meuse.riv)
meuse.sr = SpatialPolygons(list(Polygons(list(Polygon(meuse.riv)),"meuse.riv")))
rv = list("sp.polygons", meuse.sr,
fill = ifelse(alphaChannelSupported(), "blue", "transparent"),
alpha = ifelse(alphaChannelSupported(), 0.1, 1))
pts = list("sp.points", meuse, pch = 3, col = "grey",
alpha = ifelse(alphaChannelSupported(), .5, 1))
text1 = list("sp.text", c(180500,329900), "0", cex = .5, which = 4)
text2 = list("sp.text", c(181000,329900), "500 m", cex = .5, which = 4)
scale = list("SpatialPolygonsRescale", layout.scale.bar(),
offset = c(180500,329800), scale = 500, fill=c("transparent","black"), which = 4)
library(gstat, pos = match(paste("package", "sp", sep=":"), search()) + 1)
data(meuse.grid)
coordinates(meuse.grid) = ~x+y
gridded(meuse.grid) = TRUE
v.ok = variogram(log(zinc)~1, meuse)
ok.model = fit.variogram(v.ok, vgm(1, "Exp", 500, 1))
# plot(v.ok, ok.model, main = "ordinary kriging")
v.uk = variogram(log(zinc)~sqrt(dist), meuse)
uk.model = fit.variogram(v.uk, vgm(1, "Exp", 300, 1))
# plot(v.uk, uk.model, main = "universal kriging")
meuse[["ff"]] = factor(meuse[["ffreq"]])
meuse.grid[["ff"]] = factor(meuse.grid[["ffreq"]])
v.sk = variogram(log(zinc)~ff, meuse)
sk.model = fit.variogram(v.sk, vgm(1, "Exp", 300, 1))
# plot(v.sk, sk.model, main = "stratified kriging")
zn.ok = krige(log(zinc)~1, meuse, meuse.grid, model = ok.model)
zn.uk = krige(log(zinc)~sqrt(dist), meuse, meuse.grid, model = uk.model)
zn.sk = krige(log(zinc)~ff, meuse, meuse.grid, model = sk.model)
zn.id = krige(log(zinc)~1, meuse, meuse.grid)
zn = zn.ok
zn[["a"]] = zn.ok[["var1.pred"]]
zn[["b"]] = zn.uk[["var1.pred"]]
zn[["c"]] = zn.sk[["var1.pred"]]
zn[["d"]] = zn.id[["var1.pred"]]
spplot(zn, c("a", "b", "c", "d"),
names.attr = c("ordinary kriging", "universal kriging with dist to river",
"stratified kriging with flood freq", "inverse distance"),
as.table = TRUE, main = "log-zinc interpolation",
sp.layout = list(rv, scale, text1, text2)
)
library(sp)
library(lattice) # required for trellis.par.set():
trellis.par.set(sp.theme()) # sets color ramp to bpy.colors()
alphaChannelSupported = function() {
!is.na(match(names(dev.cur()), c("pdf")))
}
data(meuse)
coordinates(meuse)=~x+y
data(meuse.riv)
meuse.sr = SpatialPolygons(list(Polygons(list(Polygon(meuse.riv)),"meuse.riv")))
rv = list("sp.polygons", meuse.sr, fill = "lightblue")
scale = list("SpatialPolygonsRescale", layout.scale.bar(),
offset = c(180500,329800), scale = 500, fill=c("transparent","black"), which = 4)
text1 = list("sp.text", c(180500,329900), "0", cex = .5, which = 4)
text2 = list("sp.text", c(181000,329900), "500 m", cex = .5, which = 4)
arrow = list("SpatialPolygonsRescale", layout.north.arrow(),
offset = c(181300,329800),
scale = 400, which = 4)
library(gstat, pos = match(paste("package", "sp", sep=":"), search()) + 1)
data(meuse.grid)
coordinates(meuse.grid) = ~x+y
gridded(meuse.grid) = TRUE
v.ok = variogram(log(zinc)~1, meuse)
ok.model = fit.variogram(v.ok, vgm(1, "Exp", 500, 1))
# plot(v.ok, ok.model, main = "ordinary kriging")
v.uk = variogram(log(zinc)~sqrt(dist), meuse)
uk.model = fit.variogram(v.uk, vgm(1, "Exp", 300, 1))
# plot(v.uk, uk.model, main = "universal kriging")
meuse[["ff"]] = factor(meuse[["ffreq"]])
meuse.grid[["ff"]] = factor(meuse.grid[["ffreq"]])
v.sk = variogram(log(zinc)~ff, meuse)
sk.model = fit.variogram(v.sk, vgm(1, "Exp", 300, 1))
# plot(v.sk, sk.model, main = "stratified kriging")
zn.ok = krige(log(zinc)~1, meuse, meuse.grid, model = ok.model)
zn.uk = krige(log(zinc)~sqrt(dist), meuse, meuse.grid, model = uk.model)
zn.sk = krige(log(zinc)~ff, meuse, meuse.grid, model = sk.model)
zn.id = krige(log(zinc)~1, meuse, meuse.grid)
rv = list("sp.polygons", meuse.sr,
fill = ifelse(alphaChannelSupported(), "blue", "transparent"),
alpha = ifelse(alphaChannelSupported(), 0.1, 1))
pts = list("sp.points", meuse, pch = 3, col = "grey",
alpha = ifelse(alphaChannelSupported(), .7, 1))
spplot(zn.uk, "var1.pred",
sp.layout = list(rv, scale, text1, text2, pts),
main = "log(zinc); universal kriging using sqrt(dist to Meuse)")
zn.uk[["se"]] = sqrt(zn.uk[["var1.var"]])
## Universal kriging standard errors; grid plot with point locations
## and polygon (river), pdf has transparency on points and river
spplot(zn.uk, "se",
sp.layout = list(rv, scale, text1, text2, pts),
main = "log(zinc); universal kriging standard errors")
library(sp)
library(lattice) # required for trellis.par.set():
trellis.par.set(sp.theme()) # sets color ramp to bpy.colors()
# prepare nc sids data set:
library(sf)
nc <- as(st_read(system.file("shape/nc.shp", package="sf")), "Spatial")
arrow = list("SpatialPolygonsRescale", layout.north.arrow(),
offset = c(-76,34), scale = 0.5, which = 2)
#scale = list("SpatialPolygonsRescale", layout.scale.bar(),
# offset = c(-77.5,34), scale = 1, fill=c("transparent","black"), which = 2)
#text1 = list("sp.text", c(-77.5,34.15), "0", which = 2)
#text2 = list("sp.text", c(-76.5,34.15), "1 degree", which = 2)
## multi-panel plot with filled polygons: North Carolina SIDS
spplot(nc, c("SID74", "SID79"), names.attr = c("1974","1979"),
colorkey=list(space="bottom"), scales = list(draw = TRUE),
main = "SIDS (sudden infant death syndrome) in North Carolina",
sp.layout = list(arrow), as.table = TRUE)
# sp.layout = list(arrow, scale, text1, text2), as.table = TRUE)
library(sp)
library(lattice) # required for trellis.par.set():
trellis.par.set(sp.theme()) # sets color ramp to bpy.colors()
arrow = list("SpatialPolygonsRescale", layout.north.arrow(),
offset = c(-76,34), scale = 0.5, which = 2)
#scale = list("SpatialPolygonsRescale", layout.scale.bar(),
# offset = c(-77.5,34), scale = 1, fill=c("transparent","black"), which = 2)
#text1 = list("sp.text", c(-77.5,34.15), "0", which = 2)
#text2 = list("sp.text", c(-76.5,34.15), "1 degree", which = 2)
# create a fake lines data set:
library(sf)
ncl <- as(st_read(system.file("shape/nc.shp", package="sf")), "Spatial")
## multi-panel plot with coloured lines: North Carolina SIDS
spplot(ncl, c("SID74","SID79"), names.attr = c("1974","1979"),
colorkey=list(space="bottom"),
main = "SIDS (sudden infant death syndrome) in North Carolina",
sp.layout = arrow, as.table = TRUE)
library(sp)
data(meuse.grid)
coordinates(meuse.grid) = ~x+y
gridded(meuse.grid) = TRUE
data(meuse)
coordinates(meuse) = ~x+y
data(meuse.riv)
meuse.sl = SpatialLines(list(Lines(list(Line(meuse.riv)), "ID")))
#meuse.sr = SpatialPolygons(list(Polygons(list(Polygon(meuse.riv)),"meuse.riv")))
## image plot with points and lines
image(meuse.grid["dist"],
main = "meuse river data set; colour indicates distance to river")
points(meuse, pch = 3)
lines(meuse.sl)
library(sp)
library(lattice)
data(meuse)
coordinates(meuse) = ~x+y
## bubble plots for cadmium and zinc
data(meuse)
coordinates(meuse) <- c("x", "y") # promote to SpatialPointsDataFrame
b1 = bubble(meuse, "cadmium", maxsize = 1.5, main = "cadmium concentrations (ppm)",
key.entries = 2^(-1:4))
b2 = bubble(meuse, "zinc", maxsize = 1.5, main = "zinc concentrations (ppm)",
key.entries = 100 * 2^(0:4))
print(b1, split = c(1,1,2,1), more = TRUE)
print(b2, split = c(2,1,2,1), more = FALSE)
library(sp)
## plot for SpatialPolygons, with county name at label point
library(sf)
nc2 <- as(st_read(system.file("shape/nc.shp", package="sf")), "Spatial")
plot(nc2)
invisible(text(coordinates(nc2), labels=as.character(nc2$NAME), cex=0.4))
library(sp)
## plot of SpatialPolygonsDataFrame, using grey shades
library(sf)
nc1 <- as(st_read(system.file("shape/nc.shp", package="sf")), "Spatial")
names(nc1)
rrt <- nc1$SID74/nc1$BIR74
brks <- quantile(rrt, seq(0,1,1/7))
cols <- grey((length(brks):2)/length(brks))
dens <- (2:length(brks))*3
plot(nc1, col=cols[findInterval(rrt, brks, all.inside=TRUE)])
library(sp)
## plot of SpatialPolygonsDataFrame, using line densities
library(sf)
nc <- as(st_read(system.file("shape/nc.shp", package="sf")), "Spatial")
names(nc)
rrt <- nc$SID74/nc$BIR74
brks <- quantile(rrt, seq(0,1,1/7))
cols <- grey((length(brks):2)/length(brks))
dens <- (2:length(brks))*3
plot(nc, density=dens[findInterval(rrt, brks, all.inside=TRUE)])
library(sp)
data(meuse.riv)
meuse.sr = SpatialPolygons(list(Polygons(list(Polygon(meuse.riv)), "x")))
plot(meuse.sr)
## stratified sampling within a polygon
points(spsample(meuse.sr@polygons[[1]], n = 200, "stratified"), pch = 3, cex=.3)
## random sampling over a grid
library(sp)
data(meuse.grid)
gridded(meuse.grid) = ~x+y
image(meuse.grid)
points(spsample(meuse.grid,n=1000,type="random"), pch=3, cex=.4)
## regular sampling over a grid
library(sp)
data(meuse.grid)
gridded(meuse.grid) = ~x+y
image(meuse.grid["dist"])
points(spsample(meuse.grid,n=1000,type="regular"), pch=3, cex=.4)
## nonaligned systematic sampling over a grid
library(sp)
data(meuse.grid)
gridded(meuse.grid) = ~x+y
image(meuse.grid["dist"])
points(spsample(meuse.grid,n=1000,type="nonaligned"), pch=3, cex=.4)
library(sp)
library(lattice) # required for trellis.par.set():
trellis.par.set(sp.theme()) # sets color ramp to bpy.colors()
alphaChannelSupported = function() {
!is.na(match(names(dev.cur()), c("pdf")))
}
data(meuse)
coordinates(meuse)=~x+y
data(meuse.riv)
meuse.sr = SpatialPolygons(list(Polygons(list(Polygon(meuse.riv)),"meuse.riv")))
rv = list("sp.polygons", meuse.sr, fill = "lightblue")
scale = list("SpatialPolygonsRescale", layout.scale.bar(),
offset = c(180500,329800), scale = 500, fill=c("transparent","black"), which = 4)
text1 = list("sp.text", c(180500,329900), "0", cex = .5, which = 4)
text2 = list("sp.text", c(181000,329900), "500 m", cex = .5, which = 4)
arrow = list("SpatialPolygonsRescale", layout.north.arrow(),
offset = c(181300,329800),
scale = 400, which = 4)
library(gstat, pos = match(paste("package", "sp", sep=":"), search()) + 1)
data(meuse.grid)
coordinates(meuse.grid) = ~x+y
gridded(meuse.grid) = TRUE
v.ok = variogram(log(zinc)~1, meuse)
ok.model = fit.variogram(v.ok, vgm(1, "Exp", 500, 1))
# plot(v.ok, ok.model, main = "ordinary kriging")
v.uk = variogram(log(zinc)~sqrt(dist), meuse)
uk.model = fit.variogram(v.uk, vgm(1, "Exp", 300, 1))
# plot(v.uk, uk.model, main = "universal kriging")
meuse[["ff"]] = factor(meuse[["ffreq"]])
meuse.grid[["ff"]] = factor(meuse.grid[["ffreq"]])
v.sk = variogram(log(zinc)~ff, meuse)
sk.model = fit.variogram(v.sk, vgm(1, "Exp", 300, 1))
# plot(v.sk, sk.model, main = "stratified kriging")
zn.ok = krige(log(zinc)~1, meuse, meuse.grid, model = ok.model)
zn.uk = krige(log(zinc)~sqrt(dist), meuse, meuse.grid, model = uk.model)
zn.sk = krige(log(zinc)~ff, meuse, meuse.grid, model = sk.model)
zn.id = krige(log(zinc)~1, meuse, meuse.grid)
rv = list("sp.polygons", meuse.sr,
fill = ifelse(alphaChannelSupported(), "blue", "transparent"),
alpha = ifelse(alphaChannelSupported(), 0.1, 1))
pts = list("sp.points", meuse, pch = 3, col = "grey",
alpha = ifelse(alphaChannelSupported(), .5, 1))
spplot(zn.uk, "var1.pred",
sp.layout = list(rv, scale, text1, text2, pts),
main = "log(zinc); universal kriging using sqrt(dist to Meuse)")
zn.uk[["se"]] = sqrt(zn.uk[["var1.var"]])
## Universal kriging standard errors; grid plot with point locations
## and polygon (river), pdf has transparency on points and river
spplot(zn.uk, "se",
sp.layout = list(rv, scale, text1, text2, pts),
main = "log(zinc); universal kriging standard errors")
library(sp)
library(sf)
nc <- as(st_read(system.file("shape/nc.shp", package="sf")), "Spatial")
names(nc)
# create two dummy factor variables, with equal labels:
set.seed(31)
nc$f = factor(sample(1:5,100,replace=T),labels=letters[1:5])
nc$g = factor(sample(1:5,100,replace=T),labels=letters[1:5])
library(RColorBrewer)
## Two (dummy) factor variables shown with qualitative colour ramp; degrees in axes
spplot(nc, c("f","g"), col.regions=brewer.pal(5, "Set3"), scales=list(draw = TRUE))
|