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