class: center, middle, inverse, title-slide .title[ #
sfnetworks
: Tidy Geospatial Networks in R ] .subtitle[ ## SICSS - Covenant: The Summer Institutes in Computational Social Science ] .author[ ### Lucas van der Meer, Lorena Abad,
Andrea Gilardi
, Robin Lovelace ] .date[ ### 2022-06-27 ] --- class: center, middle, hide-logo <STYLE type='text/css' scoped> PRE.fansi SPAN {padding-top: .25em; padding-bottom: .25em}; </STYLE> ## About me ... I'm a research fellow in Statistics at the [University of Milano - Bicocca](https://en.unimib.it/), [Department of Economics, Management and Statistics](https://dems.unimib.it/en). I'm a passionate R user and author of two R 📦(s): [`osmextract`](https://cran.r-project.org/package=osmextract) and [`sfnetworks`](https://cran.r-project.org/package=sfnetworks). If you want to read more details, check my webpage: [https://agila5.github.io/](https://agila5.github.io/) --- class: center, middle, hide-logo ## Let's start! The slides can be found here: https://sfnetworks.github.io/SICSS-Covenant/slides All the R code can be found here: https://github.com/sfnetworks/SICSS-Covenant ??? Don't forget to copy the links in chat! --- class: middle, hide-logo ## Schedule - 9.15 - 9.35: Brief introduction to the R package `sfnetworks` * What are Geospatial Networks? * Motivations and philosophy behind `sfnetworks` - 9.35 - 9.45: Showcase the main pre-processing functions - 9.45 - 10.15: Live demo. Check the code at [sfnetworks-demo.R](https://github.com/sfnetworks/SICSS-Covenant/raw/main/sfnetworks-demo.R) - 10.15 - 10.30: Q&A or additional examples You can install the stable version of `sfnetworks` from [CRAN](https://CRAN.R-project.org/package=sfnetworks) and the development version from the [develop](https://github.com/luukvdmeer/sfnetworks/tree/develop) branch of the github repo: ```r remotes::install_github("luukvdmeer/sfnetworks", "develop") ``` This tutorial is based on the github version of the package. ??? Again, don't forget to copy the links in chat! --- class: center, top, hide-logo ## <span style="color:#b3b3b3">Tidy</span> <span style="color:#ffa500">Geospatial Networks</span> <span style="color:#b3b3b3">in R</span> .left[ .pull-left-70[ ![](figs/river_network.png) ] ] .pull-right-30[ Graphs embedded in geographical space Nodes usually associated with **POINT** geometries Edges usually associated with **LINESTRING** geometries ] ??? Underline that we cannot split the spatial and graph dimensions and that the topological information is not enough to describe the network, we need the spatial component. --- class: center, top, hide-logo ## <span style="color:#ffa500">Tidy</span> <span style="color:#b3b3b3">Geospatial Networks</span> <span style="color:#ffa500">in R</span> .left[ .pull-left-60[ ```r library(tidyverse) library(sfnetworks) roxel %>% as_tibble() %>% select(name, type) ``` <PRE class="fansi fansi-output"><CODE><span style='color: #555555;'># A tibble: 851 x 2</span> name type <span style='color: #555555; font-style: italic;'><chr></span> <span style='color: #555555; font-style: italic;'><fct></span> <span style='color: #555555;'>1</span> Havixbecker Strasse residential <span style='color: #555555;'>2</span> Pienersallee secondary <span style='color: #555555;'>3</span> Schulte-Bernd-Strasse residential <span style='color: #555555;'>4</span> <span style='color: #BB0000;'>NA</span> path <span style='color: #555555;'>5</span> Welsingheide residential <span style='color: #555555;'># ... with 846 more rows</span> </CODE></PRE> ] ] .pull-right-40[ <img src="https://raw.githubusercontent.com/tidyverse/tidyverse/main/man/figures/logo.png" width="40%" /> **The tidyverse** Collection of packages for data science Shared design philosophy Tidy data structures ] ??? In that tibble we have that each row is an observation and columns are variables. --- class: center, top, hide-logo ## <span style="color:#b3b3b3">Tidy</span> <span style="color:#ffa500">Geospatial</span> <span style="color:#b3b3b3">Networks</span> <span style="color:#ffa500">in R</span> .left[ .pull-left-60[ ```r library(sf) roxel[1:3, "name"] ``` <PRE class="fansi fansi-output"><CODE>Simple feature collection with 3 features and 1 field Geometry type: LINESTRING Dimension: XY Bounding box: xmin: 7.532 ymin: 51.95 xmax: 7.534 ymax: 51.96 Geodetic CRS: WGS 84 <span style='color: #555555;'># A tibble: 3 x 2</span> name geometry <span style='color: #555555; font-style: italic;'><chr></span> <span style='color: #555555; font-style: italic;'><LINESTRING [°]></span> <span style='color: #555555;'>1</span> Havixbecker Strasse (7.534 51.96, 7.533 51.96) <span style='color: #555555;'>2</span> Pienersallee (7.532 51.95, 7.532 51.95, 7.532 51.95) <span style='color: #555555;'>3</span> Schulte-Bernd-Strasse (7.533 51.95, 7.533 51.95, 7.533 51.95) </CODE></PRE> ] ] .pull-right-40[ <img src="https://user-images.githubusercontent.com/520851/34887433-ce1d130e-f7c6-11e7-83fc-d60ad4fae6bd.gif" width="45%" /> **The sf package** R package for spatial data science Standardized storage of geographic features (e.g. points, lines, polygons) Geometric operations (intersections, unions, ..) ] ??? Underline it's compatible with most dplyr and tidyr verbs. Each obs has it own geometry/location in space. --- class: center, top, hide-logo ## <span style="color:#b3b3b3">Tidy Geospatial</span> <span style="color:#ffa500">Networks in R</span> .pull-left-60[ .left[ ```r nodes = tibble(name = letters[1:3]) edges = tibble(from = c(1,2,3), to = c(2,3,1)) tidygraph::tbl_graph(nodes, edges) ``` <PRE class="fansi fansi-output"><CODE><span style='color: #555555;'># A tbl_graph: 3 nodes and 3 edges # # A directed simple graph with 1 component # # Node Data: 3 x 1 (active)</span> name <span style='color: #555555; font-style: italic;'><chr></span> <span style='color: #555555;'>1</span> a <span style='color: #555555;'>2</span> b <span style='color: #555555;'>3</span> c <span style='color: #555555;'># # Edge Data: 3 x 2</span> from to <span style='color: #555555; font-style: italic;'><int></span> <span style='color: #555555; font-style: italic;'><int></span> <span style='color: #555555;'>1</span> 1 2 <span style='color: #555555;'>2</span> 2 3 <span style='color: #555555;'>3</span> 3 1 </CODE></PRE> ] ] .pull-right-40[ <img src="https://github.com/thomasp85/tidygraph/raw/master/man/figures/logo.png" width="45%" /> **The tidygraph package** Tidy interface to the **igraph** library for graph analysis Models a graph as a collection of two tibbles ] ??? It is a wrapper around igraph, which doesn't fit in the tidyverse workflow. Networks cannot really be modelled as tidy data, but the two tables (i.e. edges and nodes) are represented as tidy data. We could simply add a geometry column to a tbl_graph object but that doesn't work since tidygraph doesn't work what to do with the geometry column. --- class: center, top, hide-logo ## Tidy Geospatial Networks in R! .pull-left[ ![](https://user-images.githubusercontent.com/520851/34887433-ce1d130e-f7c6-11e7-83fc-d60ad4fae6bd.gif) ] .pull-right[ <img src="https://github.com/thomasp85/tidygraph/raw/master/man/figures/logo.png" width="38%" /> ] .center[ <img src="https://github.com/luukvdmeer/sfnetworks/raw/master/man/figures/logo.png" width="20%" /> ] --- class: center, top, hide-logo ## Tidy Geospatial Networks in R! **tidygraph:** > Relational data cannot in any meaningful way be encoded as a single tidy data frame. On the other hand, both node and edge data by itself fits very well within the tidy concept as each node and edge is, in a sense, a single observation. Thus, a close approximation of tidyness for relational data is two tidy data frames, one describing the node data and one describing the edge data. **sfnetworks:** > A close approximation of tidyness for relational geospatial data is two sf objects, one describing the node data and one describing the edge data --- class: top, hide-logo ## The sfnetwork class .left[ ```r as_sfnetwork(roxel) ``` <PRE class="fansi fansi-output"><CODE><span style='color: #555555;'># A sfnetwork with</span> <span style='color: #555555;'>701</span> <span style='color: #555555;'>nodes and</span> <span style='color: #555555;'>851</span> <span style='color: #555555;'>edges # # CRS: </span> <span style='color: #555555;'>EPSG:4326</span> <span style='color: #555555;'> # # A directed multigraph with 14 components with spatially explicit edges # # Node Data: 701 x 1 (active)</span> <span style='color: #555555;'># Geometry type: POINT</span> <span style='color: #555555;'># Dimension: XY</span> <span style='color: #555555;'># Bounding box: xmin: 7.523 ymin: 51.94 xmax: 7.547 ymax: 51.96</span> <span style='color: #555555;'># ... with 701 more rows, and 1 variable: geometry <GEOMETRY [°]></span> <span style='color: #555555;'># # Edge Data: 851 x 5</span> <span style='color: #555555;'># Geometry type: LINESTRING</span> <span style='color: #555555;'># Dimension: XY</span> <span style='color: #555555;'># Bounding box: xmin: 7.523 ymin: 51.94 xmax: 7.547 ymax: 51.96</span> <span style='color: #555555;'># ... with 851 more rows, and 5 variables: from <int>, to <int>,</span> <span style='color: #555555;'># name <chr>, type <fct>, geometry <GEOMETRY [°]></span> </CODE></PRE> ] --- class: center, middle ## What can we do? ### Network pre-processing and cleaning --- class: middle ### Network pre-processing and cleaning #### Initial network .center[ <img src="slides_files/figure-html/clean2-1.png" width="864" /> ] --- class: middle ### Network pre-processing and cleaning #### Simplifying edges .center[ <img src="slides_files/figure-html/clean3-1.png" width="864" /> ] --- class: middle ### Network pre-processing and cleaning #### Subdividing edges .center[ <img src="slides_files/figure-html/clean4-1.png" width="864" /> ] --- class: middle ### Network pre-processing and cleaning #### Removing pseudo-nodes .center[ <img src="slides_files/figure-html/clean5-1.png" width="864" /> ] --- class: middle ### Network pre-processing and cleaning #### Simplifying intersections .center[ <img src="slides_files/figure-html/clean6-1.png" width="864" /> ] --- class: middle ## Live demo! Now we will present a few basic concepts and simple examples using R code. Let's switch to [sfnetworks-demo.R](https://github.com/sfnetworks/SICSS-Covenant/raw/main/sfnetworks-demo.R) Please note that we will focus on road network analysis, but, in principle, the package can be used as a general tool for spatial networks (see [here](https://stackoverflow.com/questions/65958776/group-id-for-all-edges-between-points-using-sfnetwork-in-r) and [here](https://stackoverflow.com/questions/68809447/sequencing-of-river-network-calculation-using-sfnetworks-and-r) for examples of stream network analysis and [here](https://shriv-portfolio.netlify.app/post/infrastructure-flows/) for infrastructure flows). We also developed several more tools to analyse spatial networks such as **graph and spatial morphers**, **graph and spatial filters** or **routing tools**. Let's see more examples with live demo coding! --- ## Conclusion ### Have your own examples or ideas for the package? -- Join the [Discussion on GitHub](https://github.com/luukvdmeer/sfnetworks/discussions) -- ### Found a bug? -- Let us know with an [issue here](https://github.com/luukvdmeer/sfnetworks/issues)! -- ### Have a question? Feel free to add any question to [Stack Overflow](https://stackoverflow.com/questions/tagged/sfnetwork) using the tags `sfnetwork`. .note[ We will soon create a link between the tags `sfnetwork` (i.e. the name of the class) and `sfnetworks` (i.e. the name of the package). For the moment, you should use the tag `sfnetwork`. ] --- class: center, middle ### Thank you! #### Q&A .note[ .pull-left-70[ .pull-left[ <img src="https://raw.githubusercontent.com/RConsortium/artwork/main/r_consortium/R_Consortium-logo-horizontal-color.png" width="50%" /> ] .pull-right[ Slides powered by [xaringan](https://github.com/yihui/xaringan), [xaringanthemer](https://github.com/gadenbuie/xaringanthemer) and [flipbookr](https://github.com/EvaMaeRey/flipbookr). Slides originally developed by Luuk and Lorena, thanks!<br><br>. ] ] .pull-right-30[ <img src="https://4.bp.blogspot.com/_OXpMnfxUDqE/R_GrYYCT1EI/AAAAAAAAABY/GbElkcspz6Q/s400/logo.jpg" width="100%" /> ] ]