| 1 |
## ----------------------------------------------------------------------------- |
| 2 |
library(sp) |
| 3 |
demo(meuse, ask = FALSE, echo = FALSE) # loads the meuse data sets |
| 4 |
class(meuse) |
| 5 |
|
| 6 |
## ----------------------------------------------------------------------------- |
| 7 |
library(sf) |
| 8 |
nc <- as(st_read(system.file("gpkg/nc.gpkg", package="sf")), "Spatial") |
| 9 |
|
| 10 |
## ----------------------------------------------------------------------------- |
| 11 |
plot(meuse) |
| 12 |
|
| 13 |
## ----------------------------------------------------------------------------- |
| 14 |
plot(meuse, pch = 1, cex = sqrt(meuse$zinc)/12, axes = TRUE) |
| 15 |
v = c(100,200,400,800,1600) |
| 16 |
legend("topleft", legend = v, pch = 1, pt.cex = sqrt(v)/12) |
| 17 |
plot(meuse.riv, add = TRUE, col = grey(.9, alpha = .5)) |
| 18 |
|
| 19 |
## ----------------------------------------------------------------------------- |
| 20 |
proj4string(meuse) |
| 21 |
|
| 22 |
## ----fig.height=8------------------------------------------------------------- |
| 23 |
crs.longlat = CRS("+init=epsg:4326") |
| 24 |
meuse.longlat = spTransform(meuse, crs.longlat) |
| 25 |
plot(meuse.longlat, axes = TRUE) |
| 26 |
|
| 27 |
## ----------------------------------------------------------------------------- |
| 28 |
par(mar = rep(0,4)) |
| 29 |
plot(meuse.longlat, asp = 1) |
| 30 |
|
| 31 |
## ----fig.height=7.5----------------------------------------------------------- |
| 32 |
par(mar = c(0, 0, 1, 0)) |
| 33 |
library(methods) # as |
| 34 |
plot(as(meuse, "Spatial"), expandBB = c(.05, 0, 0, 0)) |
| 35 |
plot(gridlines(meuse), add = TRUE, col = grey(.8)) |
| 36 |
plot(meuse, add = TRUE) |
| 37 |
text(labels(gridlines(meuse)), col = grey(.7)) |
| 38 |
title("default gridlines with Meuse projected data") |
| 39 |
|
| 40 |
## ----------------------------------------------------------------------------- |
| 41 |
par(mar = c(0, 0, 1, 0)) |
| 42 |
grd <- gridlines(meuse.longlat) |
| 43 |
grd_x <- spTransform(grd, CRS(proj4string(meuse))) |
| 44 |
plot(as(meuse, "Spatial"), expandBB = c(.05, 0, 0, 0)) |
| 45 |
plot(grd_x, add=TRUE, col = grey(.8)) |
| 46 |
plot(meuse, add = TRUE) |
| 47 |
text(labels(grd_x, crs.longlat), col = grey(.7)) |
| 48 |
title("longitude latitude gridlines and labels") |
| 49 |
|
| 50 |
## ----------------------------------------------------------------------------- |
| 51 |
# demonstrate axis labels with angle, both sides: |
| 52 |
|
| 53 |
maps2sp = function(xlim, ylim, l.out = 100, clip = TRUE) { |
| 54 |
stopifnot(require(maps)) |
| 55 |
m = map(xlim = xlim, ylim = ylim, plot = FALSE, fill = TRUE) |
| 56 |
as(st_as_sf(m), "Spatial") |
| 57 |
} |
| 58 |
par(mar = c(0, 0, 1, 0)) |
| 59 |
m = maps2sp(c(-100,-20), c(10,55)) |
| 60 |
sp = SpatialPoints(rbind(c(-101,9), c(-101,55), c(-19,9), c(-19,55)), CRS("+init=epsg:4326")) |
| 61 |
laea = CRS("+proj=laea +lat_0=30 +lon_0=-40") |
| 62 |
m.laea = spTransform(m, laea) |
| 63 |
sp.laea = spTransform(sp, laea) |
| 64 |
plot(as(m.laea, "Spatial"), expandBB = c(.1, 0.05, .1, .1)) |
| 65 |
plot(m.laea, col = grey(.8), add = TRUE) |
| 66 |
gl = gridlines(sp, easts = c(-100,-80,-60,-40,-20), norths = c(20,30,40,50)) |
| 67 |
gl.laea = spTransform(gl, laea) |
| 68 |
plot(gl.laea, add = TRUE) |
| 69 |
text(labels(gl.laea, crs.longlat)) |
| 70 |
text(labels(gl.laea, crs.longlat, side = 3:4), col = 'red') |
| 71 |
title("curved text label demo") |
| 72 |
|
| 73 |
# polar: |
| 74 |
par(mar = c(0, 0, 1, 0)) |
| 75 |
pts=SpatialPoints(rbind(c(-180,-70),c(0,-70),c(180,-89),c(180,-70)), CRS("+init=epsg:4326")) |
| 76 |
gl = gridlines(pts, easts = seq(-180,180,20), ndiscr = 100) |
| 77 |
polar = CRS("+init=epsg:3031") |
| 78 |
plot(spTransform(pts, polar), expandBB = c(.05, 0, .05, 0)) |
| 79 |
gl.polar = spTransform(gl, polar) |
| 80 |
lines(gl.polar) |
| 81 |
l = labels(gl.polar, crs.longlat, side = 3) |
| 82 |
l$pos = NULL # pos is too simple, use adj: |
| 83 |
text(l, adj = c(0.5, -0.5), cex = .8) |
| 84 |
l = labels(gl.polar, crs.longlat, side = 4) |
| 85 |
l$srt = 0 # otherwise they end up upside-down |
| 86 |
text(l, cex = .8) |
| 87 |
title("grid line labels on polar projection, epsg 3031") |
| 88 |
|
| 89 |
par(mar = c(0, 0, 1, 0)) |
| 90 |
m = maps2sp(xlim = c(-180,180), ylim = c(-90,-70), clip = FALSE) |
| 91 |
gl = gridlines(m, easts = seq(-180,180,20)) |
| 92 |
polar = CRS("+init=epsg:3031") |
| 93 |
gl.polar = spTransform(gl, polar) |
| 94 |
plot(as(gl.polar, "Spatial"), expandBB = c(.05, 0, .05, 0)) |
| 95 |
plot(gl.polar, add = TRUE) |
| 96 |
plot(spTransform(m, polar), add = TRUE, col = grey(0.8, 0.8)) |
| 97 |
l = labels(gl.polar, crs.longlat, side = 3) |
| 98 |
# pos is too simple here, use adj: |
| 99 |
l$pos = NULL |
| 100 |
text(l, adj = c(0.5, -0.3), cex = .8) |
| 101 |
l = labels(gl.polar, crs.longlat, side = 2) |
| 102 |
l$srt = 0 # otherwise they are upside-down |
| 103 |
text(l, cex = .8) |
| 104 |
title("grid line labels on polar projection, epsg 3031") |
| 105 |
|
| 106 |
|
| 107 |
## ----------------------------------------------------------------------------- |
| 108 |
par(mar = c(0, 0, 1, 0)) |
| 109 |
plot(nc) |
| 110 |
invisible(text(coordinates(nc), labels=as.character(nc$NAME), cex=0.4)) |
| 111 |
|
| 112 |
## ----------------------------------------------------------------------------- |
| 113 |
names(nc) |
| 114 |
rrt <- nc$SID74/nc$BIR74 |
| 115 |
brks <- quantile(rrt, seq(0,1,1/7)) |
| 116 |
cols <- grey((length(brks):2)/length(brks)) |
| 117 |
dens <- (2:length(brks))*3 |
| 118 |
par(mar = c(0, 0, 1, 0)) |
| 119 |
plot(nc, col=cols[findInterval(rrt, brks, all.inside=TRUE)]) |
| 120 |
|
| 121 |
## ----------------------------------------------------------------------------- |
| 122 |
rrt <- nc$SID74/nc$BIR74 |
| 123 |
brks <- quantile(rrt, seq(0,1,1/7)) |
| 124 |
cols <- grey((length(brks):2)/length(brks)) |
| 125 |
dens <- (2:length(brks))*3 |
| 126 |
par(mar = rep(0,4)) |
| 127 |
plot(nc, density=dens[findInterval(rrt, brks, all.inside=TRUE)]) |
| 128 |
|
| 129 |
## ----------------------------------------------------------------------------- |
| 130 |
image(meuse.grid) |
| 131 |
|
| 132 |
## ----------------------------------------------------------------------------- |
| 133 |
plot(meuse.grid["dist"]) |
| 134 |
points(meuse, col = 'green') |
| 135 |
|
| 136 |
## ----------------------------------------------------------------------------- |
| 137 |
plot(meuse.grid["dist"], zlim = c(0,1)) |
| 138 |
plot(geometry(meuse.grid), add = TRUE, col = grey(.8)) |
| 139 |
|
| 140 |
## ----------------------------------------------------------------------------- |
| 141 |
spplot(meuse, "zinc", do.log = TRUE, |
| 142 |
key.space=list(x = 0.1, y = 0.95, corner = c(0, 1)), |
| 143 |
scales=list(draw = TRUE)) |
| 144 |
|
| 145 |
## ----------------------------------------------------------------------------- |
| 146 |
spplot(meuse, "zinc", do.log = TRUE, |
| 147 |
key.space=list(x=0.2,y=0.9,corner=c(0,1)), |
| 148 |
scales=list(draw = TRUE), cuts = 3, |
| 149 |
legendEntries = c("low", "intermediate", "high")) |
| 150 |
|
| 151 |
## ----------------------------------------------------------------------------- |
| 152 |
scale = list("SpatialPolygonsRescale", layout.scale.bar(), |
| 153 |
offset = c(178600,332490), scale = 500, fill=c("transparent","black")) |
| 154 |
text1 = list("sp.text", c(178600,332590), "0") |
| 155 |
text2 = list("sp.text", c(179100,332590), "500 m") |
| 156 |
arrow = list("SpatialPolygonsRescale", layout.north.arrow(), |
| 157 |
offset = c(178750,332000), scale = 400) |
| 158 |
spplot(meuse, "zinc", do.log=T, |
| 159 |
key.space=list(x=0.1,y=0.93,corner=c(0,1)), |
| 160 |
sp.layout=list(scale,text1,text2,arrow), |
| 161 |
main = "Zinc (top soil)") |
| 162 |
|
| 163 |
## ----------------------------------------------------------------------------- |
| 164 |
rv = list("sp.polygons", meuse.riv, fill = "lightblue") |
| 165 |
scale = list("SpatialPolygonsRescale", layout.scale.bar(), |
| 166 |
offset = c(180500,329800), scale = 500, fill=c("transparent","black"), which = 1) |
| 167 |
text1 = list("sp.text", c(180500,329900), "0", which = 1) |
| 168 |
text2 = list("sp.text", c(181000,329900), "500 m", which = 1) |
| 169 |
arrow = list("SpatialPolygonsRescale", layout.north.arrow(), |
| 170 |
offset = c(178750,332500), scale = 400) |
| 171 |
spplot(meuse["zinc"], do.log = TRUE, |
| 172 |
key.space = "bottom", |
| 173 |
sp.layout = list(rv, scale, text1, text2), |
| 174 |
main = "Zinc (top soil)", |
| 175 |
legend = list(right = list(fun = mapLegendGrob(layout.north.arrow())))) |
| 176 |
|
| 177 |
## ----------------------------------------------------------------------------- |
| 178 |
spplot(meuse, "zinc", panel = function(x, y, ...) { |
| 179 |
sp.polygons(meuse.riv, fill = "lightblue") |
| 180 |
SpatialPolygonsRescale(layout.scale.bar(), offset = c(179900,329600), |
| 181 |
scale = 500, fill=c("transparent","black")) |
| 182 |
sp.text(c(179900,329700), "0") |
| 183 |
sp.text(c(180400,329700), "500 m") |
| 184 |
SpatialPolygonsRescale(layout.north.arrow(), |
| 185 |
offset = c(178750,332500), scale = 400) |
| 186 |
panel.pointsplot(x, y, ...) |
| 187 |
}, |
| 188 |
do.log = TRUE, cuts = 7, |
| 189 |
key.space = list(x = 0.1, y = 0.93, corner = c(0,1)), |
| 190 |
main = "Top soil zinc concentration (ppm)") |
| 191 |
|
| 192 |
## ----------------------------------------------------------------------------- |
| 193 |
rv = list("sp.polygons", meuse.riv, fill = "lightblue") |
| 194 |
scale = list("SpatialPolygonsRescale", layout.scale.bar(), |
| 195 |
offset = c(180500,329800), scale = 500, fill=c("transparent","black"), which = 4) |
| 196 |
text1 = list("sp.text", c(180500,329900), "0", cex = .5, which = 4) |
| 197 |
text2 = list("sp.text", c(181000,329900), "500 m", cex = .5, which = 4) |
| 198 |
arrow = list("SpatialPolygonsRescale", layout.north.arrow(), |
| 199 |
offset = c(181300,329800), |
| 200 |
scale = 400, which = 4) |
| 201 |
cuts = c(.2,.5,1,2,5,10,20,50,100,200,500,1000,2000) |
| 202 |
spplot(meuse, c("cadmium", "copper", "lead", "zinc"), do.log = TRUE, |
| 203 |
key.space = "right", as.table = TRUE, |
| 204 |
sp.layout=list(rv, scale, text1, text2, arrow), # note that rv is up front! |
| 205 |
main = "Heavy metals (top soil), ppm", cex = .7, cuts = cuts) |
| 206 |
|
| 207 |
## ----------------------------------------------------------------------------- |
| 208 |
rv = list("sp.polygons", meuse.riv, fill = "blue", alpha = 0.1) |
| 209 |
pts = list("sp.points", meuse, pch = 3, col = "grey", alpha = .5) |
| 210 |
text1 = list("sp.text", c(180500,329900), "0", cex = .5, which = 4) |
| 211 |
text2 = list("sp.text", c(181000,329900), "500 m", cex = .5, which = 4) |
| 212 |
scale = list("SpatialPolygonsRescale", layout.scale.bar(), |
| 213 |
offset = c(180500,329800), scale = 500, fill=c("transparent","black"), which = 4) |
| 214 |
library(gstat) |
| 215 |
v.ok = variogram(log(zinc)~1, meuse) |
| 216 |
ok.model = fit.variogram(v.ok, vgm(1, "Exp", 500, 1)) |
| 217 |
# plot(v.ok, ok.model, main = "ordinary kriging") |
| 218 |
v.uk = variogram(log(zinc)~sqrt(dist), meuse) |
| 219 |
uk.model = fit.variogram(v.uk, vgm(1, "Exp", 300, 1)) |
| 220 |
# plot(v.uk, uk.model, main = "universal kriging") |
| 221 |
meuse[["ff"]] = factor(meuse[["ffreq"]]) |
| 222 |
meuse.grid[["ff"]] = factor(meuse.grid[["ffreq"]]) |
| 223 |
v.sk = variogram(log(zinc)~ff, meuse) |
| 224 |
sk.model = fit.variogram(v.sk, vgm(1, "Exp", 300, 1)) |
| 225 |
# plot(v.sk, sk.model, main = "stratified kriging") |
| 226 |
zn.ok = krige(log(zinc)~1, meuse, meuse.grid, model = ok.model, debug.level = 0) |
| 227 |
zn.uk = krige(log(zinc)~sqrt(dist), meuse, meuse.grid, model = uk.model, debug.level = 0) |
| 228 |
zn.sk = krige(log(zinc)~ff, meuse, meuse.grid, model = sk.model, debug.level = 0) |
| 229 |
zn.id = krige(log(zinc)~1, meuse, meuse.grid, debug.level = 0) |
| 230 |
|
| 231 |
zn = zn.ok |
| 232 |
zn[["a"]] = zn.ok[["var1.pred"]] |
| 233 |
zn[["b"]] = zn.uk[["var1.pred"]] |
| 234 |
zn[["c"]] = zn.sk[["var1.pred"]] |
| 235 |
zn[["d"]] = zn.id[["var1.pred"]] |
| 236 |
spplot(zn, c("a", "b", "c", "d"), |
| 237 |
names.attr = c("ordinary kriging", "universal kriging with dist to river", |
| 238 |
"stratified kriging with flood freq", "inverse distance"), |
| 239 |
as.table = TRUE, main = "log-zinc interpolation", |
| 240 |
sp.layout = list(rv, scale, text1, text2) |
| 241 |
) |
| 242 |
|
| 243 |
## ----------------------------------------------------------------------------- |
| 244 |
rv = list("sp.polygons", meuse.riv, fill = "blue", alpha = 0.1) |
| 245 |
pts = list("sp.points", meuse, pch = 3, col = "grey", alpha = .7) |
| 246 |
spplot(zn.uk, "var1.pred", |
| 247 |
sp.layout = list(rv, scale, text1, text2, pts), |
| 248 |
main = "log(zinc); universal kriging using sqrt(dist to Meuse)") |
| 249 |
zn.uk[["se"]] = sqrt(zn.uk[["var1.var"]]) |
| 250 |
spplot(zn.uk, "se", sp.layout = list(rv, pts), |
| 251 |
main = "log(zinc); universal kriging standard errors") |
| 252 |
|
| 253 |
## ----------------------------------------------------------------------------- |
| 254 |
arrow = list("SpatialPolygonsRescale", layout.north.arrow(), |
| 255 |
offset = c(-76,34), scale = 0.5, which = 2) |
| 256 |
spplot(nc, c("SID74", "SID79"), names.attr = c("1974","1979"), |
| 257 |
colorkey=list(space="bottom"), scales = list(draw = TRUE), |
| 258 |
main = "SIDS (sudden infant death syndrome) in North Carolina", |
| 259 |
sp.layout = list(arrow), as.table = TRUE) |
| 260 |
|
| 261 |
## ----------------------------------------------------------------------------- |
| 262 |
arrow = list("SpatialPolygonsRescale", layout.north.arrow(), |
| 263 |
offset = c(-76,34), scale = 0.5, which = 2) |
| 264 |
#scale = list("SpatialPolygonsRescale", layout.scale.bar(), |
| 265 |
# offset = c(-77.5,34), scale = 1, fill=c("transparent","black"), which = 2) |
| 266 |
#text1 = list("sp.text", c(-77.5,34.15), "0", which = 2) |
| 267 |
#text2 = list("sp.text", c(-76.5,34.15), "1 degree", which = 2) |
| 268 |
# create a fake lines data set: |
| 269 |
## multi-panel plot with coloured lines: North Carolina SIDS |
| 270 |
spplot(nc, c("SID74","SID79"), names.attr = c("1974","1979"), |
| 271 |
colorkey=list(space="bottom"), |
| 272 |
main = "SIDS (sudden infant death syndrome) in North Carolina", |
| 273 |
sp.layout = arrow, as.table = TRUE) |
| 274 |
|
| 275 |
## ----------------------------------------------------------------------------- |
| 276 |
b1 = bubble(meuse, "cadmium", maxsize = 1.5, main = "cadmium concentrations (ppm)", |
| 277 |
key.entries = 2^(-1:4)) |
| 278 |
b2 = bubble(meuse, "zinc", maxsize = 1.5, main = "zinc concentrations (ppm)", |
| 279 |
key.entries = 100 * 2^(0:4)) |
| 280 |
print(b1, split = c(1,1,2,1), more = TRUE) |
| 281 |
print(b2, split = c(2,1,2,1), more = FALSE) |
| 282 |
|
| 283 |
## ----------------------------------------------------------------------------- |
| 284 |
# create two dummy factor variables, with equal labels: |
| 285 |
set.seed(31) |
| 286 |
nc$f = factor(sample(1:5, 100,replace = TRUE),labels=letters[1:5]) |
| 287 |
nc$g = factor(sample(1:5, 100,replace = TRUE),labels=letters[1:5]) |
| 288 |
library(RColorBrewer) |
| 289 |
## Two (dummy) factor variables shown with qualitative colour ramp; degrees in axes |
| 290 |
spplot(nc, c("f","g"), col.regions=brewer.pal(5, "Set3"), scales=list(draw = TRUE)) |
| 291 |
|
| 292 |
|
| 293 |
## ----results="markup"--------------------------------------------------------- |
| 294 |
library(mapview) |
| 295 |
mapview(meuse, zcol = c("zinc", "lead"), legend = TRUE) |
| 296 |
|
| 297 |
## ----results="markup"--------------------------------------------------------- |
| 298 |
mapview(meuse.grid, zcol = c("soil", "dist"), legend = TRUE) |
| 299 |
|
| 300 |
## ----results="markup"--------------------------------------------------------- |
| 301 |
mapview(nc, zcol = c("SID74", "SID79"), alpha.regions = 1.0, legend = TRUE) |
| 302 |
|
| 303 |
## ----results="markup",eval=FALSE---------------------------------------------- |
| 304 |
# m1 <- mapview(meuse, zcol = "soil", burst = TRUE, legend = TRUE) |
| 305 |
# m2 <- mapview(meuse, zcol = "lead", legend = TRUE) |
| 306 |
# m3 <- mapview(meuse, zcol = "landuse", map.types = "Esri.WorldImagery", legend = TRUE) |
| 307 |
# m4 <- mapview(meuse, zcol = "dist.m", legend = TRUE) |
| 308 |
# sync(m1, m2, m3, m4) # 4 panels synchronised |
| 309 |
# # latticeView(m1, m2, m3, m4) # 4 panels |
| 310 |
|
| 311 |
## ----------------------------------------------------------------------------- |
| 312 |
sessionInfo() |
| 313 |
|