class: center, middle, inverse, title-slide # DSBA 5122: Visual Analytics ## Class 8: Networks and Trees ### Ryan Wesslen ### October 21, 2019 --- class: center, middle, inverse # Networks and Trees <img src="../images/slides/08-class/mazzo.png" width="400px" style="display: block; margin: auto;" /> Read Chapter 5: Networks and Hiearchies of Mazza --- # Networks (a.k.a. graphs) <img src="../images/slides/08-class/net1.png" width="600px" style="display: block; margin: auto;" /> - Graphs are visual representations in which the points, called **nodes** or **vertices**, represent instances of the data. - Nodes are correlated by connections, called **edges**, which represent relationships between the instances. - Possible features of a network: weights, direction, labels. --- class: center, middle .pull-left[ <img src="../images/slides/08-class/undirected.jpeg" width="400px" style="display: block; margin: auto;" /> ] .pull-right[ <img src="../images/slides/08-class/directed.jpeg" width="400px" style="display: block; margin: auto;" /> ] [Vaidehi Joshi Medium post](https://medium.com/basecs/from-theory-to-practice-representing-graphs-cfd782c5be38) --- class: middle, center <img src="../images/slides/08-class/concept1.png" width="600px" style="display: block; margin: auto;" /> --- class: middle, center <img src="../images/slides/08-class/concept2.png" width="600px" style="display: block; margin: auto;" /> [BNOSAC: R NLP packages ecosystem](http://www.bnosac.be/index.php/blog/87-an-overview-of-the-nlp-ecosystem-in-r-nlproc-textasdata) --- class: middle, center <img src="../images/slides/08-class/net2.png" width="600px" style="display: block; margin: auto;" /> --- # Layouts <img src="../images/slides/08-class/net3.png" width="600px" style="display: block; margin: auto;" /> See [R-Graph-Gallery Network Layouts](https://www.r-graph-gallery.com/247-network-chart-layouts.html) for more. --- # Which is the most important node? ```r library(igraph) library(netrankr) g <- graph.empty(n = 11,directed = FALSE) g <- add_edges(g,c(1,11,2,4,3,5,3,11,4,8,5,9,5,11,6,7,6,8, 6,10,6,11,7,9,7,10,7,11,8,9,8,10,9,10)) ``` <img src="../images/slides/08-class/network.png" width="600px" style="display: block; margin: auto;" /> --- # Centrality Measures <img src="../images/slides/08-class/network.png" width="600px" style="display: block; margin: auto;" /> .pull-left[ ```r # from igraph cent_scores <- tibble( node = 1:11, degree = degree(g), betweenness = round(betweenness(g),4), closeness = round(closeness(g),4), eigenvector = round(eigen_centrality(g)$vector,4), subgraph = round(subgraph_centrality(g),4)) cent_scores %>% arrange(desc(betweenness)) ``` ] .pull-right[ ``` ## # A tibble: 11 x 6 ## node degree betweenness closeness eigenvector subgraph ## <int> <dbl> <dbl> <dbl> <dbl> <dbl> ## 1 8 4 16.3 0.0556 0.839 6.67 ## 2 11 5 14.7 0.0556 0.845 7.39 ## 3 6 4 9.83 0.0588 0.985 7.81 ## 4 4 2 9 0.04 0.242 2.42 ## 5 9 4 7.33 0.0556 0.911 7.03 ## 6 5 3 3.83 0.05 0.571 4.39 ## 7 7 4 2.67 0.0526 1 7.94 ## 8 10 4 1.33 0.0526 0.999 8.24 ## 9 1 1 0 0.037 0.226 1.83 ## 10 2 1 0 0.0294 0.0646 1.60 ## 11 3 2 0 0.04 0.379 3.15 ``` ] --- # Centrality Measures using [DT DataTable](https://rstudio.github.io/DT/) <img src="../images/slides/08-class/network.png" width="400px" style="display: block; margin: auto;" />
--- class: middle, center <img src="../images/slides/08-class/net4.png" width="600px" style="display: block; margin: auto;" /> --- <img src="../images/slides/08-class/tree1.png" width="650px" style="display: block; margin: auto;" /> - A **tree** (hierarchy) can be represented through a graph with a starting node called root. - Each node has zero or more child nodes its ancestor is called the **parent** node. A node has at most one parent. --- class: middle, center <img src="../images/slides/08-class/tree2.png" width="650px" style="display: block; margin: auto;" /> --- class: middle, center <img src="../images/slides/08-class/tree3.png" width="550px" style="display: block; margin: auto;" /> --- # json files: Tweet Example <blockquote class="twitter-tweet"><p lang="en" dir="ltr">1/ Today we’re sharing our vision for the future of the Twitter API platform!<a href="https://t.co/XweGngmxlP">https://t.co/XweGngmxlP</a></p>— Twitter Dev (@TwitterDev) <a href="https://twitter.com/TwitterDev/status/850006245121695744?ref_src=twsrc%5Etfw">April 6, 2017</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script> --- # json files: Tweet Example <img src="../images/slides/08-class/json.png" width="700px" style="display: block; margin: auto;" /> - See [Twitter Developer JSON intro](https://developer.twitter.com/en/docs/tweets/data-dictionary/overview/intro-to-tweet-json) for more details. - See ['jsonlite' package](https://cran.r-project.org/web/packages/jsonlite/vignettes/json-aaquickstart.html) for handling json files in R. --- # listviewer: viewing json in R .pull-left[ ```r library(listviewer) jsonedit( list( array = c(1,2,3) ,boolean = TRUE ,null = NULL ,number = 123 ,object = list( a="b", c="d" ) ,string = "Hello World. " ) ) ``` ] .pull-right[
] --- class: middle, center <img src="../images/slides/08-class/tree4.png" width="650px" style="display: block; margin: auto;" /> --- class: middle, center <img src="../images/slides/08-class/tree5.png" width="600px" style="display: block; margin: auto;" /> --- class: center, middle, inverse ## Katya Ognyanova's "Network visualization with R." <img src="../images/slides/08-class/katato.png" width="600px" style="display: block; margin: auto;" /> http://kateto.net/network-visualization --- class: center, middle <img src="../images/slides/08-class/kateto1.png" width="800px" style="display: block; margin: auto;" /> --- class: center, middle <img src="../images/slides/08-class/katato2.png" width="800px" style="display: block; margin: auto;" /> --- class: center, middle <img src="../images/slides/08-class/katato3.png" width="800px" style="display: block; margin: auto;" />