Skip to content

Instantly share code, notes, and snippets.

@Robinlovelace
Created April 27, 2016 07:20
Show Gist options
  • Save Robinlovelace/557fdb07f94fecc14c559f30b29fa128 to your computer and use it in GitHub Desktop.
Save Robinlovelace/557fdb07f94fecc14c559f30b29fa128 to your computer and use it in GitHub Desktop.
Generate the inverse of a spatial subset
# Aim: illustrate how to invert a spatial subset
# load the sp library and data
library(sp)
data("meuse")
coordinates(meuse) = ~x+y
data("meuse.riv")
meuse.sr = SpatialPolygons(list(Polygons(list(Polygon(meuse.riv)),"meuse.riv")))
# plot the basics
plot(meuse)
plot(meuse.sr, add = T)
# subset the points
meuse_inside = meuse[meuse.sr,]
sel = over(meuse, meuse.sr)
meuse_outside = meuse[is.na(sel),]
# put it all together in a plot
par(mfrow = c(1, 3))
plot(meuse_inside, col = "white", main = "All points")
plot(meuse, add = T)
plot(meuse.sr, add = T)
plot(meuse_inside, col = "white", main = "Inside:\nx[y,]")
plot(meuse.sr, add = T)
plot(meuse_inside, add = T)
plot(meuse_inside, col = "white", main = "Outside:\ns = over(x, y)\nx[is.na(s),]")
plot(meuse.sr, add = T)
plot(meuse_outside, add = T)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment