name: 1 class: center middle main-title section-title-4 # Building Networks from Spatial Data .class-info[ **Session 9** .light[HES597: Introduction to Spatial Data in R<br> Boise State University Human-Environment Systems<br> Fall 2021] ] --- # Outline - Introducing networks - Applications of networks - Building networks (in `R`) - (Some) Network properties of interest --- # Defining Networks .pull-left[ - Network theory is a subset of __graph theory__ - _Graphs_: mathematical structures used to model the relations between objects - Comprised of _nodes_ (vertices, points) and _edges_ (links) - In network analysis, our _nodes_ and/or _edges_ have __names__ (or attributes) ] .pull-right[ <img src="05-slides_files/figure-html/simpnet-1.png" width="360" style="display: block; margin: auto;" /> ] --- ## What sorts of things might comprise nodes and edges in your work? --- # Types of Networks .pull-left[ - Undirected <img src="05-slides_files/figure-html/undigraph-1.png" width="504" style="display: block; margin: auto;" /> ] .pull-right[ - Directed <img src="05-slides_files/figure-html/digraph-1.png" width="504" style="display: block; margin: auto;" /> ] --- # Types of Networks .pull-left[ - Uweighted <img src="05-slides_files/figure-html/unwtgraph-1.png" width="504" style="display: block; margin: auto;" /> ] .pull-right[ - Weighted <img src="05-slides_files/figure-html/wtgraph-1.png" width="504" style="display: block; margin: auto;" /> ] --- class: middle # Types of Graphs - Many additional common types of graphs (beyond the scope of this course) - Connected graphs, bipartite graphs, acyclic graphs are common in our field(s) - Check out Vicken Hillis' HES 597: Social Network Analysis course for more --- name: apps class: center middle main-title section-title-4 # Common of Applications of Networks --- # Transportation and infrastructure .pull-left[ <figure> <img src="img/05/airports-map-small.png" alt="ZZZ" title="From [Martin Grandjean](http://www.martingrandjean.ch/connected-world-air-traffic-network/)" width="100%"> </figure> .caption[ From [Martin Grandjean](http://www.martingrandjean.ch/connected-world-air-traffic-network/)] ] .pull-right[ <figure> <img src="img/05/powernetwork.png" alt="ZZZ" title="From Jasny et al. 2015" width="100%"> </figure> .caption[ Paul Cuffe, [CC BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0), via Wikimedia Commons ] ] --- # Social Sciences .pull-left[ <figure> <img src="img/05/socialnetwork.png" alt="ZZZ" title="From Buyalskaya et al. 2021" width="100%"> </figure> .captiona[ From Buyalskaya et al. 2021 ] ] .pull-right[ <figure> <img src="img/05/ClimateEgo_jasny.png" alt="ZZZ" title="From Jasny et al. 2015" width="100%"> </figure> .caption[ From Jasny et al. 2015 ] ] --- # Ecology .pull-left[ <figure> <img src="img/05/burgessshale.png" alt="ZZZ" title="From Jasny et al. 2015" width="100%"> </figure> .caption[ Jennifer Dunne, Santa Fe Institute ] ] .pull-right[ <figure> <img src="img/05/pa_network.png" alt="ZZZ" title="From Jasny et al. 2015" width="100%"> </figure> .caption[ Adapted from Palomo et. al 2014 ] ] --- # Constructing networks ## Adjacency matrices .pull-left[ - unweighted: 1 if 'adjacent' 0 if not - undirected: matrix is symmetric ``` ## A B C D E F ## A 0 0 1 0 1 0 ## B 0 0 0 0 0 0 ## C 1 0 0 0 1 0 ## D 0 0 0 0 1 0 ## E 1 0 1 1 0 1 ## F 0 0 0 0 1 0 ``` ] .pull-right[ - weighted: value reflects the strength of connections - directed: not all connections are reciprocal ``` ## A B C D E F ## A 0.00 0.00 0.00 0.67 0.95 0.87 ## B 0.00 0.68 0.84 0.00 0.00 0.53 ## C 0.66 0.00 0.83 0.00 0.91 0.58 ## D 0.77 0.00 0.00 0.85 0.78 0.00 ## E 0.00 0.51 0.00 0.00 0.91 0.72 ## F 0.85 0.00 0.60 0.00 0.82 0.00 ``` ] --- # We can use the `igraph` package to convert these to graphs .pull-left[ ```r plot(igraph::graph_from_adjacency_matrix(mtx, mode='undirected')) ``` <img src="05-slides_files/figure-html/adjnetwork-1.png" width="360" style="display: block; margin: auto;" /> ] .pull-right[ ```r g <- igraph::graph_from_adjacency_matrix(adjm, mode='directed', weighted = TRUE) plot(g, edge.label=E(g)$weight) ``` <img src="05-slides_files/figure-html/wtdnetwork-1.png" width="360" style="display: block; margin: auto;" /> ] --- # Constructing Networks ## Edge lists - Adjacency matrices become LARGE for relatively simple networks - For many networks, connections are limited (i.e., matrices are sparse) - Often easier to provide a list of connections - Can also provide weights in a dataframe (see the transportation example) --- # Characterizing networks - Degree distribution: the number of edges between each node and other nodes - Clustering coefficient: provides an indication of local spatial structure - Motifs: repeating subgraph shapes in a netwrok - Connectivity: minimum # of elements to remove to generate isolated subraphs - Centrality: characterize the network position of nodes or edges - LOTS of others --- name: spatial class: center middle main-title section-title-4 # Spatial Networks --- # Spatial Networks - Space is often _implicit_ in network analyses - Probability of finding a link decreases with distance - Different edge weights may alter these probabilities - Generally planar --- # Generating Spatial Networks in R ## Defining adjacency .pull-left[ ```r p = st_sfc(st_point(c(0,2)), st_point(c(0,1)), st_point(c(0,4)), st_point(c(1,2)), st_point(c(3,1)), st_point(c(2,4))) round(st_distance(p), digits = 2) ``` ``` ## [,1] [,2] [,3] [,4] [,5] [,6] ## [1,] 0.00 1.00 2.00 1.00 3.16 2.83 ## [2,] 1.00 0.00 3.00 1.41 3.00 3.61 ## [3,] 2.00 3.00 0.00 2.24 4.24 2.00 ## [4,] 1.00 1.41 2.24 0.00 2.24 2.24 ## [5,] 3.16 3.00 4.24 2.24 0.00 3.16 ## [6,] 2.83 3.61 2.00 2.24 3.16 0.00 ``` ] .pull-right[ <img src="05-slides_files/figure-html/pdist-1.png" width="360" style="display: block; margin: auto;" /> ] --- # Generating Spatial Networks in R ## Defining adjacency - With a distance threshold .pull-left[ ```r adjm <- ifelse(st_distance(p) <1.5, 1,0) adjm ``` ``` ## [,1] [,2] [,3] [,4] [,5] [,6] ## [1,] 1 1 0 1 0 0 ## [2,] 1 1 0 1 0 0 ## [3,] 0 0 1 0 0 0 ## [4,] 1 1 0 1 0 0 ## [5,] 0 0 0 0 1 0 ## [6,] 0 0 0 0 0 1 ``` ] .pull-right[ <img src="05-slides_files/figure-html/netplot-1.png" width="360" style="display: block; margin: auto;" /> ] --- # Generating Spatial Networks in R - for rasters - `directions` define window for connected cells - important for connectivity analyses ```r r <- rast(nrows=10, ncols=10) values(r) <- round(runif(ncell(r)) * 5) a <- adjacent(r, 1:ncell(r), directions="queen") ```