Manipulating and visualizing spatial vector data.
include_graphics("sf-classes.png")
st_point
to create a point object,st_linestring
to create a linestring,
and st_polygon
to create a polygon.
sfc
.st_sf
, using geometry
to associate a raw st_geom
each row of a data.frame.world
dataset, provided by the spData
package. Each row of the world
object contains both the boundary of a country (in the geom
column) and information about its location and population characteristics.Simple feature collection with 6 features and 10 fields
geometry type: MULTIPOLYGON
dimension: XY
bbox: xmin: -180 ymin: -18.28799 xmax: 180 ymax: 83.23324
geographic CRS: WGS 84
# A tibble: 6 x 11
iso_a2 name_long continent region_un subregion type area_km2
<chr> <chr> <chr> <chr> <chr> <chr> <dbl>
1 FJ Fiji Oceania Oceania Melanesia Sove… 1.93e4
2 TZ Tanzania Africa Africa Eastern … Sove… 9.33e5
3 EH Western … Africa Africa Northern… Inde… 9.63e4
4 CA Canada North Am… Americas Northern… Sove… 1.00e7
5 US United S… North Am… Americas Northern… Coun… 9.51e6
6 KZ Kazakhst… Asia Asia Central … Sove… 2.73e6
# … with 4 more variables: pop <dbl>, lifeExp <dbl>, gdpPercap <dbl>,
# geom <MULTIPOLYGON [°]>
This makes the plot, using dplyr
to filter down to just the row containing the India geometry.
india_geom <- world %>%
filter(name_long == "India") %>%
st_geometry()
plot(india_geom)
add = TRUE
.geom_sf
in ggplot2. To change the coordinates of the viewing box, we can use coord_sf
.aes
.ggmap
library.source("https://uwmadison.box.com/shared/static/c1mnfdl292fcot0jo58g5wnshquaa7zx.r")
register_google(key = "AIzaSyBW7PU2Ne1CM33WMaPreTbAImzGB22Pc6s")
watercolor <- get_map(c(79.5937, 22.92501), maptype = "watercolor", zoom = 3)
watercolor <- ggmap_bbox(watercolor)
ggmap(watercolor) +
geom_sf(data = world_asia, aes(fill = lifeExp), inherit.aes = FALSE) +
coord_sf(xlim = c(5.5e6, 11e6), ylim = c(5e5, 5e6), crs = st_crs(3857)) +
scale_fill_viridis_b()
The code from the previous lecture gives more examples of how to use ggplot2 and ggmap to visualize vector data over public map backgrounds
Even in this more complex setup, where we work with background images and vector data rather than standard data.frames, most of the usual ggplot2 functions still apply. For example, we can still color code or facet by fields in the vector dataset. To illustrate, we revisit the bus route data from the last lecture and distinguish between buses operated by the cities of Madison vs. Monona. Before plotting, we fetch the underlying data.
bus <- read_sf("https://uwmadison.box.com/shared/static/5neu1mpuh8esmb1q3j9celu73jy1rj2i.geojson")
satellite <- get_map(c(-89.37974, 43.07140), maptype = "satellite", zoom = 12)
satellite <- ggmap_bbox(satellite)
head(bus)
Simple feature collection with 6 features and 13 fields
geometry type: GEOMETRY
dimension: XY
bbox: xmin: -89.53446 ymin: 43.0501 xmax: -89.35841 ymax: 43.11426
geographic CRS: WGS 84
# A tibble: 6 x 14
id X.id fixme name network operator public_transpor… ref
<chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
1 rela… rela… how … Metr… Metro … City of… 1 73
2 rela… rela… <NA> Metr… Metro … City of… 1 84
3 rela… rela… <NA> Metr… Metro … City of… 1 80
4 rela… rela… <NA> Metr… Metro … City of… 1 67
5 rela… rela… veri… Metr… Metro … City of… 1 2
6 rela… rela… <NA> Metr… Metro … City of… 1 70
# … with 6 more variables: route <chr>, type <chr>, website <chr>,
# FIXME <chr>, X.relations <chr>, geometry <GEOMETRY [°]>
Note that operator
is the field containing information about which city is operating the buses. We can color code the routes by this attribute.
Alternatively, we can facet.
ggmap(satellite) +
geom_sf(data = bus, col = "#bc7ab3", size = .5, inherit.aes = FALSE) +
coord_sf(crs = st_crs(3857)) +
facet_wrap(~ operator)