dplyr verbs for Spatial

Apply dplyr verbs to the attribute data of sp objects with dplyr verbs.

See ?dplyr-Spatial' for supported verbs.

coordinates(quakes) <- ~long+lat
## plot a subset of locations by number of stations
quakes %>% dplyr::filter(mag <5.5 & mag > 4.5) %>% dplyr::select(stations) %>% spplot()

We can use polygons and lines objects as well.

## put the centre-of-mass centroid on wrld_simpl as an attribute and filter/select
worldcorner <- wrld_simpl %>% 
  mutate(lon = coordinates(wrld_simpl)[,1], lat = coordinates(wrld_simpl)[,2]) %>% 
  filter(lat < -20, lon > 60) %>% 

## demonstrate that we have a faithful subset of the original object
plot(worldcorner, asp = "")
text(coordinates(worldcorner), label = worldcorner$NAME, cex = 0.6)

This approach is limited to the simple verbs arrange, distinct, filter, mutate, rename, select, slice, transmute, and summarize. Summarize either on its own or after a group_by collapses to a non-topological union of all sub-geometries within a group without any consideration of topology or relationships.

BEWARE: This is not a proven method for dealing with Spatial data, use at your own risk.