Manipulating and visualizing spatial vector data.
include_graphics("https://krisrs1128.github.io/stat479/posts/2021-03-02-week7-2/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
Bounding box: xmin: -180 ymin: -18.28799 xmax: 180 ymax: 83.23324
Geodetic 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 Sahara Africa Africa Northern~ Inde~ 9.63e4
4 CA Canada North Amer~ Americas Northern~ Sove~ 1.00e7
5 US United States North Amer~ Americas Northern~ Coun~ 9.51e6
6 KZ Kazakhstan Asia Asia Central ~ Sove~ 2.73e6
# i 4 more variables: pop <dbl>, lifeExp <dbl>, gdpPercap <dbl>,
# geom <MULTIPOLYGON [arc_degree]>
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
.tm_polygons
in tmap. To change the coordinates of the
viewing box, we can set the bbox
(bounding box) argument.bbox <- c(60, 5, 110, 40)
tm_shape(world_asia, bbox = bbox) +
tm_polygons(col = "white") +
tm_shape(india_geom) +
tm_polygons()
tm_shape(world_asia, bbox = bbox) +
tm_polygons(col = "lifeExp") +
tm_polygons()
Sys.setenv(MAPBOX_API_KEY="pk.eyJ1Ijoia3Jpc3JzMTEyOCIsImEiOiJjbDYzdjJzczQya3JzM2Jtb2E0NWU1a3B3In0.Mk4-pmKi_klg3EKfTw-JbQ")
basemap <- cc_location(loc= c(-89.401230, 43.073051), buffer = 15e3)
bus <- read_sf("https://uwmadison.box.com/shared/static/5neu1mpuh8esmb1q3j9celu73jy1rj2i.geojson")
tm_shape(basemap) +
tm_rgb() +
tm_shape(bus) +
tm_lines(col = "#bc7ab3", size = 1)
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.
For attribution, please cite this work as
Sankaran (2024, Jan. 7). STAT 436 (Spring 2024): Vector Data. Retrieved from https://krisrs1128.github.io/stat436_s24/website/stat436_s24/posts/2024-12-27-week07-02/
BibTeX citation
@misc{sankaran2024vector, author = {Sankaran, Kris}, title = {STAT 436 (Spring 2024): Vector Data}, url = {https://krisrs1128.github.io/stat436_s24/website/stat436_s24/posts/2024-12-27-week07-02/}, year = {2024} }