<?xml version="1.0" encoding="UTF-8" ?>
<!-- RSS generated by PHPBoost on Sat, 18 Apr 2026 20:48:15 +0200 -->

<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title><![CDATA[Last articles - STHDA : Social Network Analysis]]></title>
		<atom:link href="https://www.sthda.com/english/syndication/rss/articles/33" rel="self" type="application/rss+xml"/>
		<link>https://www.sthda.com</link>
		<description><![CDATA[Last articles - STHDA : Social Network Analysis]]></description>
		<copyright>(C) 2005-2026 PHPBoost</copyright>
		<language>en</language>
		<generator>PHPBoost</generator>
		
		
		<item>
			<title><![CDATA[Interactive Network Visualization using R]]></title>
			<link>https://www.sthda.com/english/articles/33-social-network-analysis/137-interactive-network-visualization-using-r/</link>
			<guid>https://www.sthda.com/english/articles/33-social-network-analysis/137-interactive-network-visualization-using-r/</guid>
			<description><![CDATA[<!-- START HTML -->

  <div id="rdoc">
<p>This chapter describes two key R packages for creating interactive network graphs. These packages include:</p>
<ul>
<li><strong>visNetwork</strong> <span class="citation">(Almende B.V., Thieurmel, and Robert 2017)</span>. Creates an interactive network visualization using the <strong>vis.js</strong> javascript library (<a href="http://visjs.org/" class="uri">http://visjs.org/</a>).</li>
<li><strong>networkD3</strong> <span class="citation">(Allaire et al. 2017)</span>. Creates a D3 JavaScript Network Graphs from R.</li>
</ul>
<p>You’ll learn how to:</p>
<ul>
<li>Create a classic network graph that is interactive</li>
<li>Make an interactive sankey diagram, useful for network flow visualization</li>
<li>Visualize, interactively, classification and regression trees</li>
</ul>
<p>Contents:</p>
<div id="TOC">
<ul>
<li><a href="#load-demo-data-sets-and-r-package">Load demo data sets and R package</a></li>
<li><a href="#networkd3-r-package">networkD3 R package</a><ul>
<li><a href="#key-features">Key features</a></li>
<li><a href="#key-r-functions-and-options">Key R functions and options</a></li>
<li><a href="#prepare-nodes-and-edes-data">Prepare nodes and edes data</a></li>
<li><a href="#create-sankey-diagram">Create sankey diagram</a></li>
<li><a href="#other-network-layouts">Other network layouts</a></li>
</ul></li>
<li><a href="#visnetwork-r-package">visNetwork R package</a><ul>
<li><a href="#key-features-1">Key features</a></li>
<li><a href="#key-r-function-and-options">Key R function and options</a></li>
<li><a href="#create-a-classic-network-graphs">Create a classic network graphs</a></li>
<li><a href="#visualize-classification-and-regression-trees">Visualize classification and regression trees</a></li>
</ul></li>
<li><a href="#references">References</a></li>
</ul>
</div>
<br/>
<div class = "small-block content-privileged-friends navr-book">
  <p>The Book:</p>
        <a href = "https://www.sthda.com/english/web/5-bookadvisor/53-network-analysis-and-visualization-in-r/">
          <img src = "https://www.sthda.com/english/upload/network-analysis-and-visualization-in-r-200px.png" /><br/>
     Network Analysis and Visualization in R: Quick Start Guide
      </a>
</div>
<div class="spacer"></div>
<div id="load-demo-data-sets-and-r-package" class="section level2">
<h2>Load demo data sets and R package</h2>
<p>We’ll use the <code>phone.call2</code> data [in the <code>navdata</code> R package], which is a list containing the nodes and the edges list prepared in the chapter @ref(network-visualization-essentials) from the <code>phone.call</code> data.</p>
<p>Start by loading the tidyverse R package and the phone.call2 demo data sets:</p>
<pre class="r"><code>library(tidyverse)
library("navdata")
data("phone.call2")
nodes <- phone.call2$nodes
edges <- phone.call2$edges</code></pre>
</div>
<div id="networkd3-r-package" class="section level2">
<h2>networkD3 R package</h2>
<div id="key-features" class="section level3">
<h3>Key features</h3>
<p>Can be used to easily create an interactive sankey diagram, as well as, other network layout such as dendrogram, radial and diagnonal networks.</p>
</div>
<div id="key-r-functions-and-options" class="section level3">
<h3>Key R functions and options</h3>
<p><strong>Key R functions</strong>:</p>
<p><code>forceNetwork()</code>. Creates a D3 JavaScript force directed network graph</p>
<pre class="r"><code>forceNetwork(Links, Nodes, Source, Target,
             Value, NodeID, Nodesize, Group)</code></pre>
<p><strong>Key Arguments</strong>:</p>
<ul>
<li><code>Links</code>: edges list. <strong>Edge IDs should start with 0</strong></li>
<li><code>Nodes</code>: Nodes list. <strong>Node IDs should start with 0</strong></li>
<li><code>Source</code>, <code>Target</code>: the names of the column, in the edges data, containing the network source and target variables, respectively.</li>
<li><code>Value</code>: the name of the column, in the edge data, containing the weight values for edges. Used to indicate how wide the links are.</li>
<li><code>NodeID</code>: the name of the column, in the nodes data, containing the node IDs. Used for labeling the nodes.</li>
<li><code>Nodesize</code>: the name of the column, in the nodes data, with some value to vary the node radius’s with.</li>
<li><code>Group</code>: the name of the column, in the nodes data, specifying the group of each node.</li>
</ul>
</div>
<div id="prepare-nodes-and-edes-data" class="section level3">
<h3>Prepare nodes and edes data</h3>
<div class="warning">
<p>
As specified above, the IDs in nodes and edges lists should be numeric values starting with 0. This can be easily done by substracting 1 from the existing IDs in the two data frames.
</p>
</div>
<ol style="list-style-type: decimal">
<li>Prepare the nodes and the edges data:</li>
</ol>
<pre class="r"><code>nodes_d3 <- mutate(nodes, id = id - 1)
edges_d3 <- mutate(edges, from = from - 1, to = to - 1)</code></pre>
<ol start="2" style="list-style-type: decimal">
<li>Create the interactive network:</li>
</ol>
<pre class="r"><code>library(networkD3)</code></pre>
<pre class="r"><code>forceNetwork(
  Links = edges_d3, Nodes = nodes_d3,  
  Source = "from", Target = "to",      # so the network is directed.
  NodeID = "label", Group = "id", Value = "weight", 
  opacity = 1, fontSize = 16, zoom = TRUE
  )</code></pre>
<iframe width="100%" height="400" src="https://www.sthda.com/english/sthda-upload/images/network-analysis-and-visualization/networkd3.html" frameborder="1">
</iframe>
<div class="success">
<p>
Note that, a color is attributed to each group. Here, as we specified the column “id” as the node Group value, we have different colors for each individual nodes.
</p>
</div>
</div>
<div id="create-sankey-diagram" class="section level3">
<h3>Create sankey diagram</h3>
<p>You can create a <a href="https://bost.ocks.org/mike/sankey/">d3-styled sankey diagram</a>. A Sankey diagram is a good fit for the phone call data. There are not too many nodes in the data, making it easier to visualize the flow of phone calls.</p>
<p>Create a sankey diagram:</p>
<pre class="r"><code>sankeyNetwork(
  Links = edges_d3, Nodes = nodes_d3, 
  Source = "from", Target = "to", 
  NodeID = "label", Value = "weight", 
  fontSize = 16, unit = "Letter(s)")</code></pre>
<iframe width="100%" height="400" src="https://www.sthda.com/english/sthda-upload/images/network-analysis-and-visualization/sankey-network.html" frameborder="1">
</iframe>
<p>Other hierarchical layouts exist in the network3D package to visualize tree-like graphs. In the example below, we start by computing hierarchical clustering using a sample of the USArrests data set:</p>
<pre class="r"><code>set.seed(123)
hc <- USArrests %>% sample_n(15) %>%
  scale() %>% dist() %>%
  hclust(method = "complete")</code></pre>
</div>
<div id="other-network-layouts" class="section level3">
<h3>Other network layouts</h3>
<ul>
<li><strong>dendroNetwork</strong>:</li>
</ul>
<pre class="r"><code>dendroNetwork(hc, fontSize = 15)</code></pre>
<iframe width="100%" height="450" src="https://www.sthda.com/english/sthda-upload/images/network-analysis-and-visualization/dendronetwork.html" frameborder="1">
</iframe>
<p>Other alternatives are:</p>
<ul>
<li><strong>radialNetwork</strong>:</li>
</ul>
<pre class="r"><code>radialNetwork(
  as.radialNetwork(hc), fontSize = 15
  )</code></pre>
<ul>
<li><strong>diagonalNetwork</strong>:</li>
</ul>
<pre class="r"><code>diagonalNetwork(
  as.radialNetwork(hc), fontSize = 15
  )</code></pre>
</div>
</div>
<div id="visnetwork-r-package" class="section level2">
<h2>visNetwork R package</h2>
<div id="key-features-1" class="section level3">
<h3>Key features</h3>
<ul>
<li>Creates interactive network graphs.</li>
<li>Possible to customize nodes and edge as you want.</li>
<li>Can be used to directly visualize interactively a network generated with the <code>igraph</code> package.</li>
<li>Can be used to visualize recursive partitioning and regression trees generated with the <code>rpart</code> package.</li>
<li>Possible to use images and icons for node shapes.</li>
<li>Supports <code>igraph</code> layouts</li>
</ul>
</div>
<div id="key-r-function-and-options" class="section level3">
<h3>Key R function and options</h3>
<p><strong>Key R function</strong>:</p>
<pre class="r"><code>visNetwork( 
  nodes = NULL, edges = NULL,
  width = NULL, height = NULL, 
  main = NULL, submain = NULL, footer = NULL
  )</code></pre>
<p><strong>Key Arguments</strong>:</p>
<ul>
<li><code>nodes</code>: nodes list information. Should contain at least the column “id”. See <code>visNodes()</code> for more options to control nodes. Other colums can be included in the data, such as:
<ul>
<li>“id” : id of the node, needed in edges information</li>
<li>“label” : label of the node</li>
<li>“group” : group of the node. Groups can be configure with <code>visGroups()</code>.</li>
<li>“value” : size of the node</li>
<li>“title” : tooltip of the node</li>
<li>…</li>
</ul></li>
<li><code>edges</code>: edges list information. Required at least columns “from” and “to”. See <code>visEdges()</code> for more options to control edges.
<ul>
<li>“from” : node id of begin of the edge</li>
<li>“to” : node id of end of the edge</li>
<li>“label” : label of the edge</li>
<li>“value” : size of the node</li>
<li>“title” : tooltip of the node</li>
<li>…</li>
</ul></li>
</ul>
</div>
<div id="create-a-classic-network-graphs" class="section level3">
<h3>Create a classic network graphs</h3>
<div class="success">
<p>
Note that, the function plots the labels for the nodes, using the “label” column in the node list.
</p>
<p>
You can move the nodes and the graph will use an algorithm to keep the nodes properly spaced. You can also zoom in and out on the plot and move it around to re-center it.
</p>
</div>
<p>To have always the same network, you can use the function <code>visLayout(randomSeed = 12)</code>:</p>
<pre class="r"><code>library("visNetwork")</code></pre>
<pre class="r"><code>visNetwork(nodes, edges) %>%
  visLayout(randomSeed = 12) </code></pre>
<iframe width="100%" height="450" src="https://www.sthda.com/english/sthda-upload/images/network-analysis-and-visualization/visnetwork-graph-visualization.html" frameborder="1">
</iframe>
<div class="warning">
<p>
Note that,
</p>
<ul>
<li>
<code>visNetwork</code> can use <code>igraph</code> layouts, which include a large variety of possible layouts.
</li>
<li>
you can use <code>visIgraph()</code> to directly visualize an igraph network object.
</li>
</ul>
</div>
<p>If you want to control the width of edges according to a variable, you should include the column “width” in the edge list data. You should manually calculate and scale the edge width.</p>
<p>In the following R code, we’ll customize the <code>visNetwork()</code> output by using an <code>igraph</code> layout and changing the edges width.</p>
<p>First add the column width in the edges list data frame. Set the minimum width to 1:</p>
<pre class="r"><code>edges <- mutate(edges, width = 1 + weight/5)</code></pre>
<p>Create the network graph with the variable edge widths and the igraph layout = “layout_with_fr”.</p>
<pre class="r"><code>visNetwork(nodes, edges) %>% 
  visIgraphLayout(layout = "layout_with_fr") %>% 
  visEdges(arrows = "middle") %>%
  visLayout(randomSeed = 1234)  </code></pre>
<iframe width="100%" height="450" src="https://www.sthda.com/english/sthda-upload/images/network-analysis-and-visualization/customize-vis-network-with-igraph-layout.html" frameborder="1">
</iframe>
</div>
<div id="visualize-classification-and-regression-trees" class="section level3">
<h3>Visualize classification and regression trees</h3>
<p>As mentioned above, you can visualize classification and regression trees generated using the <code>rpart</code> package.</p>
<p>Key function: <code>visTree()</code> [in <code>visNetwork version >= 2.0.0</code>].</p>
<p>For example, to visualize a classification tree, type the following R code:</p>
<pre class="r"><code># Compute
library(rpart)
res <- rpart(Species~., data=iris)
# Visualize
visTree(res, main = "Iris classification Tree",
        width = "80%",  height = "400px")</code></pre>
<iframe width="100%" height="450" src="https://www.sthda.com/english/sthda-upload/images/network-analysis-and-visualization/classification-tree.html" frameborder="1">
</iframe>
</div>
</div>
<div id="references" class="section level2 unnumbered">
<h2>References</h2>
<div id="refs" class="references">
<div id="ref-R-networkD3">
<p>Allaire, J.J., Christopher Gandrud, Kenton Russell, and CJ Yetman. 2017. <em>NetworkD3: D3 Javascript Network Graphs from R</em>. <a href="https://CRAN.R-project.org/package=networkD3" class="uri">https://CRAN.R-project.org/package=networkD3</a>.</p>
</div>
<div id="ref-R-visNetwork">
<p>Almende B.V., Benoit Thieurmel, and Titouan Robert. 2017. <em>VisNetwork: Network Visualization Using ’Vis.js’ Library</em>. <a href="https://CRAN.R-project.org/package=visNetwork" class="uri">https://CRAN.R-project.org/package=visNetwork</a>.</p>
</div>
</div>
</div>
</div><!--end rdoc-->

<!-- END HTML -->]]></description>
			<pubDate>Tue, 28 Nov 2017 08:22:00 +0100</pubDate>
			
		</item>
		
		<item>
			<title><![CDATA[Network Analysis and Manipulation using R]]></title>
			<link>https://www.sthda.com/english/articles/33-social-network-analysis/136-network-analysis-and-manipulation-using-r/</link>
			<guid>https://www.sthda.com/english/articles/33-social-network-analysis/136-network-analysis-and-manipulation-using-r/</guid>
			<description><![CDATA[<!-- START HTML -->


  <div id="rdoc">

<p>This chapter describes how to manipulate and analyze a network graph in R using the <code>tidygraph</code> package.</p>
<p>The <code>tidygraph</code> package provides a tidy framework to easily manipulate different types of relational data, including: graph, network and trees.</p>
<p>In the tidygraph framework, network data are considered as two tidy data tables, one describing the node data and the other is for edge data. The package provides a simple solution to switch between the two tables and provides <code>dplyr</code> verbs for manipulating them.</p>
<p>You will learn methods for detecting important or central entities in a network graph. We’ll also introduce how to detect community (or cluster) in a network.</p>
<p>Contents:</p>
<div id="TOC">
<ul>
<li><a href="#load-required-packages">Load required packages</a></li>
<li><a href="#create-network-objects">Create network objects</a><ul>
<li><a href="#use-tbl_graph">Use tbl_graph</a></li>
<li><a href="#use-as_tbl_graph-r-function">Use as_tbl_graph R function</a></li>
</ul></li>
<li><a href="#print-out-a-network-object">Print out a network object</a></li>
<li><a href="#network-graph-manipulation">Network graph manipulation</a></li>
<li><a href="#network-analysis">Network analysis</a><ul>
<li><a href="#centrality">Centrality</a></li>
<li><a href="#clustering">Clustering</a></li>
</ul></li>
<li><a href="#read-more">Read more</a></li>
</ul>
</div>
<br/>
<div class = "small-block content-privileged-friends navr-book">
  <p>The Book:</p>
        <a href = "https://www.sthda.com/english/web/5-bookadvisor/53-network-analysis-and-visualization-in-r/">
          <img src = "https://www.sthda.com/english/upload/network-analysis-and-visualization-in-r-200px.png" /><br/>
     Network Analysis and Visualization in R: Quick Start Guide
      </a>
</div>
<div class="spacer"></div>


<div id="load-required-packages" class="section level2">
<h2>Load required packages</h2>
<ul>
<li><code>tidyverse</code> for general data manipulation and visualization.</li>
<li><code>tidygraph</code> for manipulating and analyzing network graphs.</li>
<li><code>ggraph</code> for visualizing network objects created using the tidygraph package.</li>
</ul>
<pre class="r"><code>library(tidyverse)
library(tidygraph)
library(ggraph)</code></pre>
</div>
<div id="create-network-objects" class="section level2">
<h2>Create network objects</h2>
<p><strong>Key R functions</strong>:</p>
<ul>
<li><code>tbl_graph()</code>. Creates a network object from nodes and edges data</li>
<li><code>as_tbl_graph()</code>. Converts network data and objects to a tbl_graph network.</li>
</ul>
<p><strong>Demo data set</strong>: <code>phone.call2</code> data [in the <code>navdata</code> R package], which is a list containing the nodes and the edges list prepared in the chapter @ref(network-visualization-essentials).</p>
<div id="use-tbl_graph" class="section level3">
<h3>Use tbl_graph</h3>
<ul>
<li>Create a tbl_graph network object using the phone call data:</li>
</ul>
<pre class="r"><code>library("navdata")
data("phone.call2")
phone.net <- tbl_graph(
  nodes = phone.call2$nodes, 
  edges = phone.call2$edges,
  directed = TRUE
  )</code></pre>
<ul>
<li>Visualize:</li>
</ul>
<pre class="r"><code>ggraph(phone.net, layout = "graphopt") + 
  geom_edge_link(width = 1, colour = "lightgray") +
  geom_node_point(size = 4, colour = "#00AFBB") +
  geom_node_text(aes(label = label), repel = TRUE)+
  theme_graph()</code></pre>
<p><img src="https://www.sthda.com/english/sthda-upload/figures/network-analysis-and-visualization/008-network-analysis-and-manipulation-network-graph-visualization-1.png" width="576" /></p>
</div>
<div id="use-as_tbl_graph-r-function" class="section level3">
<h3>Use as_tbl_graph R function</h3>
<p>One can also use the <code>as_tbl_graph()</code> function to converts the following data structure and network objects:</p>
<ul>
<li><code>data.frame</code>, <code>list</code> and <code>matrix</code> data [R base]</li>
<li><code>igraph</code> network objects [igraph package]</li>
<li><code>network</code> network objects [network pakage]</li>
<li><code>dendrogram</code> and <code>hclust</code> [stats package]</li>
<li><code>Node</code> [data.tree package]</li>
<li><code>phylo</code> and <code>evonet</code> [ape package]</li>
<li><code>graphNEL</code>, <code>graphAM</code>, <code>graphBAM</code> [graph package (in Bioconductor)]</li>
</ul>
<p>In the following example, we’ll create a correlation matrix network graph. The <code>mtcars</code> data set will be used.</p>
<p><strong>Compute the correlation matrix</strong> between cars using the <code>corrr</code> package:</p>
<ul>
<li><ol style="list-style-type: decimal">
<li>Use the mtcars data set</li>
</ol></li>
<li><ol start="2" style="list-style-type: decimal">
<li>Compute the correlation matrix: <code>correlate()</code></li>
</ol></li>
<li><ol start="3" style="list-style-type: decimal">
<li>Convert the upper triangle to NA: <code>shave()</code></li>
</ol></li>
<li><ol start="4" style="list-style-type: decimal">
<li>Stretch the correlation data frame into long format</li>
</ol></li>
<li><ol start="5" style="list-style-type: decimal">
<li>Keep only high correlation</li>
</ol></li>
</ul>
<pre class="r"><code>library(corrr)
res.cor <- mtcars [, c(1, 3:6)] %>%  # (1)
  t() %>% correlate() %>%            # (2)
  shave(upper = TRUE) %>%            # (3)
  stretch(na.rm = TRUE) %>%          # (4)
  filter(r >= 0.998)                 # (5)
res.cor</code></pre>
<pre><code>## # A tibble: 59 x 3
##               x             y     r
##           <chr>         <chr> <dbl>
## 1     Mazda RX4 Mazda RX4 Wag 1.000
## 2     Mazda RX4      Merc 230 1.000
## 3     Mazda RX4      Merc 280 0.999
## 4     Mazda RX4     Merc 280C 0.999
## 5     Mazda RX4    Merc 450SL 0.998
## 6 Mazda RX4 Wag      Merc 230 1.000
## # ... with 53 more rows</code></pre>
<p><strong>Create the correlation network graph</strong>:</p>
<pre class="r"><code>set.seed(1)
cor.graph <- as_tbl_graph(res.cor, directed = FALSE)
ggraph(cor.graph) + 
  geom_edge_link() + 
  geom_node_point() +
  geom_node_text(
    aes(label = name), size = 3, repel = TRUE
    ) +
  theme_graph()</code></pre>
<p><img src="https://www.sthda.com/english/sthda-upload/figures/network-analysis-and-visualization/008-network-analysis-and-manipulation-correlation-network-using-ggraph-1.png" width="480" /></p>
</div>
</div>
<div id="print-out-a-network-object" class="section level2">
<h2>Print out a network object</h2>
<pre class="r"><code>cor.graph</code></pre>
<pre><code>## # A tbl_graph: 24 nodes and 59 edges
## #
## # An undirected simple graph with 3 components
## #
## # Node Data: 24 x 1 (active)
##                name
##               <chr>
## 1         Mazda RX4
## 2     Mazda RX4 Wag
## 3        Datsun 710
## 4    Hornet 4 Drive
## 5 Hornet Sportabout
## 6           Valiant
## # ... with 18 more rows
## #
## # Edge Data: 59 x 3
##    from    to     r
##   <int> <int> <dbl>
## 1     1     2 1.000
## 2     1    20 1.000
## 3     1     8 0.999
## # ... with 56 more rows</code></pre>
<p>The output shows:</p>
<ul>
<li>a tbl_graph object with 24 nodes and 59 edges. Nodes are the car names and the edges are the correlation links.</li>
<li>the first six rows of “Node Data”" and the first three of “Edge Data”.</li>
<li>that the Node Data is <strong>active</strong>.</li>
</ul>
<p>The notion of an active tibble within a tbl_graph object makes it possible to manipulate the data in one tibble at a time. The nodes tibble is activated by default, but you can change which tibble is active with the <code>activate()</code> function.</p>
<p>If you want to rearrange the rows in the edges tibble to list those with the highest “r” first, you could use <code>activate()</code> and then <code>arrange()</code>. For example, type the following R code:</p>
<pre class="r"><code>cor.graph %>% 
  activate(edges) %>% 
  arrange(desc(r))</code></pre>
<div class="success">
<p>
Note that, to extract the current active data as a tibble, you can use the function <code>as_tibble(cor.graph)</code>.
</p>
</div>
</div>
<div id="network-graph-manipulation" class="section level2">
<h2>Network graph manipulation</h2>
<p>With the <code>tidygraph</code> package, you can easily manipulate the nodes and the edges data in the network graph object using <code>dplyr</code> verbs. For example, you can add new columns or rename columns in the nodes/edges data.</p>
<p>You can also filter and arrange the data. Note that, applying <code>filter()/slice()</code> on node data will remove the edges terminating at the removed nodes.</p>
<p>In this section we’ll manipulate the correlation network graph.</p>
<ol style="list-style-type: decimal">
<li><strong>Modify the nodes data</strong>:</li>
</ol>
<ul>
<li><ol style="list-style-type: lower-alpha">
<li>Group the cars by the “cyl” variable (number of cylinders) in the original mtcars data set. We’ll color the cars by groups.</li>
</ol></li>
<li><ol start="2" style="list-style-type: lower-alpha">
<li>Join the group info to the nodes data</li>
</ol></li>
<li><ol start="3" style="list-style-type: lower-alpha">
<li>Rename the column “name”, in the nodes data, to “label”</li>
</ol></li>
</ul>
<p>You can use the <code>dplyr</code> verbs as follow:</p>
<pre class="r"><code># Car groups info
cars.group <- data_frame(
  name = rownames(mtcars),
  cyl = as.factor(mtcars$cyl)
)

# Modify the nodes data
cor.graph <- cor.graph %>%
  activate(nodes) %>%
  left_join(cars.group, by = "name") %>%
  rename(label = name)</code></pre>
<ol start="2" style="list-style-type: decimal">
<li><strong>Modify the edge data</strong>. Rename the column “r” to “weight”.</li>
</ol>
<pre class="r"><code>cor.graph <- cor.graph %>%
  activate(edges) %>%
  rename(weight = r)</code></pre>
<ol start="3" style="list-style-type: decimal">
<li><strong>Display the final modified graphs object</strong>:</li>
</ol>
<pre class="r"><code>cor.graph</code></pre>
<pre><code>## # A tbl_graph: 24 nodes and 59 edges
## #
## # An undirected simple graph with 3 components
## #
## # Edge Data: 59 x 3 (active)
##    from    to weight
##   <int> <int>  <dbl>
## 1     1     2  1.000
## 2     1    20  1.000
## 3     1     8  0.999
## 4     1     9  0.999
## 5     1    11  0.998
## 6     2    20  1.000
## # ... with 53 more rows
## #
## # Node Data: 24 x 2
##           label    cyl
##           <chr> <fctr>
## 1     Mazda RX4      6
## 2 Mazda RX4 Wag      6
## 3    Datsun 710      4
## # ... with 21 more rows</code></pre>
<ol start="4" style="list-style-type: decimal">
<li><strong>Visualize the correlation network</strong>.</li>
</ol>
<ul>
<li>Change the edges width according to the variable weight</li>
<li>Scale the edges width by setting the minimum width to 0.2 and the maximum to 1.</li>
<li>Change the color of cars (nodes) according to the grouping variable cyl.</li>
</ul>
<pre class="r"><code>set.seed(1)
ggraph(cor.graph) + 
  geom_edge_link(aes(width = weight), alpha = 0.2) + 
  scale_edge_width(range = c(0.2, 1)) +
  geom_node_point(aes(color = cyl), size = 2) +
  geom_node_text(aes(label = label), size = 3, repel = TRUE) +
  theme_graph()</code></pre>
<p><img src="https://www.sthda.com/english/sthda-upload/figures/network-analysis-and-visualization/008-network-analysis-and-manipulation-customize-correlation-network-using-ggraph-1.png" width="576" /></p>
</div>
<div id="network-analysis" class="section level2">
<h2>Network analysis</h2>
<p>In this sections, we described methods for detecting important or central entities in a network graph. We’ll also introduce how to detect community (or cluster) in a network.</p>
<div id="centrality" class="section level3">
<h3>Centrality</h3>
<p>Centrality is an important concept when analyzing network graph. The centrality of a node / edge measures how central (or important) is a node or edge in the network.</p>
<p>We consider an entity important, if he has connections to many other entities. Centrality describes the number of edges that are connected to nodes.</p>
<p>There many types of scores that determine centrality. One of the famous ones is the pagerank algorithm that was powering Google Search in the beginning.</p>
<p>Examples of common approaches of measuring centrality include:</p>
<ul>
<li><p><strong>betweenness centrality</strong>. The betweenness centrality for each nodes is the number of the shortest paths that pass through the nodes.</p></li>
<li><p><strong>closeness centrality</strong>. Closeness centrality measures how many steps is required to access every other nodes from a given nodes. It describes the distance of a node to all other nodes. The more central a node is, the closer it is to all other nodes.</p></li>
<li><p><strong>eigenvector centrality</strong>. A node is important if it is linked to by other important nodes. The centrality of each node is proportional to the sum of the centralities of those nodes to which it is connected. In general, nodes with high eigenvector centralities are those which are linked to many other nodes which are, in turn, connected to many others (and so on).</p></li>
<li><p><strong>Hub</strong> and <strong>authority centarlities</strong> are generalization of eigenvector centrality. A high hub node points to many good authorities and a high authority node receives from many good hubs.</p></li>
</ul>
<p>The <code>tidygraph</code> package contains more than 10 centrality measures, prefixed with the term <code>centrality_</code>. These measures include:</p>
<pre class="r"><code>centrality_authority()

centrality_betweenness()

centrality_closeness()

centrality_hub()

centrality_pagerank()

centrality_eigen()

centrality_edge_betweenness()</code></pre>
<p>All of these centrality functions returns a numeric vector matching the nodes (or edges in the case of `centrality_edge_betweenness()).</p>
<p>In the following examples, we’ll use the phone call network graph. We’ll change the color and the size of nodes according to their values of centrality.</p>
<pre class="r"><code>set.seed(123)
phone.net %>%
  activate(nodes) %>%
  mutate(centrality = centrality_authority()) %>% 
  ggraph(layout = "graphopt") + 
  geom_edge_link(width = 1, colour = "lightgray") +
  geom_node_point(aes(size = centrality, colour = centrality)) +
  geom_node_text(aes(label = label), repel = TRUE)+
  scale_color_gradient(low = "yellow", high = "red")+
  theme_graph()</code></pre>
<p><img src="https://www.sthda.com/english/sthda-upload/figures/network-analysis-and-visualization/008-network-analysis-and-manipulation-node-and-edge-centrality-1.png" width="576" /></p>
<div class="notice">
<p>
For a given problem at hand, you can test the different centrality score to decide which centrality measure makes most sense for your specific question.
</p>
</div>
</div>
<div id="clustering" class="section level3">
<h3>Clustering</h3>
<p>Clustering is a common operation in network analysis and it consists of grouping nodes based on the graph topology.</p>
<p>It’s sometimes referred to as community detection based on its commonality in social network analysis.</p>
<p>Many clustering algorithms from are available in the <code>tidygraph</code> package and prefixed with the term <code>group_</code>. These include:</p>
<ul>
<li><strong>Infomap community finding</strong>. It groups nodes by minimizing the expected description length of a random walker trajectory. R function: <code>group_infomap()</code></li>
<li><strong>Community structure detection based on edge betweenness</strong>. It groups densely connected nodes. R function: <code>group_edge_betweenness()</code>.</li>
</ul>
<p>In the following example, we’ll use the correlation network graphs to detect clusters or communities:</p>
<pre class="r"><code>set.seed(123)
cor.graph %>%
  activate(nodes) %>%
   mutate(community = as.factor(group_infomap())) %>% 
  ggraph(layout = "graphopt") + 
  geom_edge_link(width = 1, colour = "lightgray") +
  geom_node_point(aes(colour = community), size = 4) +
  geom_node_text(aes(label = label), repel = TRUE)+
  theme_graph()</code></pre>
<p><img src="https://www.sthda.com/english/sthda-upload/figures/network-analysis-and-visualization/008-network-analysis-and-manipulation-clustering-network-community-detection-1.png" width="576" /></p>
<div class="success">
<p>
Three communities are detected.
</p>
</div>
</div>
</div>
<div id="read-more" class="section level2">
<h2>Read more</h2>
<ul>
<li>Thomas Lin Pedersen. Introducing tidygraph. <a href="https://www.data-imaginist.com/2017/introducing-tidygraph/" class="uri">https://www.data-imaginist.com/2017/introducing-tidygraph/</a></li>
<li>Shirin Glander. Network analysis of Game of Thrones. <a href="https://datascienceplus.com/network-analysis-of-game-of-thrones/" class="uri">https://datascienceplus.com/network-analysis-of-game-of-thrones/</a></li>
</ul>
</div>


</div><!--end rdoc-->



<!-- END HTML -->]]></description>
			<pubDate>Tue, 28 Nov 2017 08:09:00 +0100</pubDate>
			
		</item>
		
		<item>
			<title><![CDATA[Network Visualization Essentials in R]]></title>
			<link>https://www.sthda.com/english/articles/33-social-network-analysis/135-network-visualization-essentials-in-r/</link>
			<guid>https://www.sthda.com/english/articles/33-social-network-analysis/135-network-visualization-essentials-in-r/</guid>
			<description><![CDATA[<!-- START HTML -->


  <div id="rdoc">

<p><strong>Network Analysis</strong> is used to investigate and visualize the inter-relationship between entities (individuals, things).</p>
<p>Examples of network structures, include: social media networks, friendship networks, collaboration networks and disease transmission.</p>
<p>Network and graph theory are extensively used across different fields, such as in biology (pathway analysis and protein-protein interaction visualization), finance, social sciences, economics, communication, history, computer science, etc.</p>
<p>In this chapter, you’ll learn:</p>
<ul>
<li>the basic terms of network analysis and visualization.</li>
<li>how to create static networks using igraph (R base plot) and ggraph (ggplot2 system) R packages.</li>
<li>how to create arc diagram, treemap and dendrogram layouts.</li>
</ul>
<p>Contents:</p>
<div id="TOC">
<ul>
<li><a href="#graph-theory-basics-and-key-terms">Graph theory: Basics and key terms</a></li>
<li><a href="#install-required-packages">Install required packages</a></li>
<li><a href="#data-structure">Data structure</a><ul>
<li><a href="#demo-data-set">Demo data set</a></li>
<li><a href="#create-nodes-list">Create nodes list</a></li>
<li><a href="#create-edges-list">Create edges list</a></li>
</ul></li>
<li><a href="#tools-and-visualization">Tools and visualization</a><ul>
<li><a href="#igraph">igraph</a></li>
<li><a href="#tidygraph-and-ggraph">tidygraph and ggraph</a></li>
</ul></li>
<li><a href="#graph-layout">Graph layout</a><ul>
<li><a href="#arc-diagram-layout">Arc diagram layout</a></li>
<li><a href="#treemap-layout">Treemap layout</a></li>
<li><a href="#dendrogram-layout">Dendrogram layout</a></li>
</ul></li>
<li><a href="#read-more">Read more</a></li>
<li><a href="#references">References</a></li>
</ul>
</div>
<br/>
<div class = "small-block content-privileged-friends navr-book">
  <p>The Book:</p>
        <a href = "https://www.sthda.com/english/web/5-bookadvisor/53-network-analysis-and-visualization-in-r/">
          <img src = "https://www.sthda.com/english/upload/network-analysis-and-visualization-in-r-200px.png" /><br/>
     Network Analysis and Visualization in R: Quick Start Guide
      </a>
</div>
<div class="spacer"></div>


<div id="graph-theory-basics-and-key-terms" class="section level2">
<h2>Graph theory: Basics and key terms</h2>
<p>Network graphs are characterized by two key terms: <strong>nodes</strong> and <strong>edges</strong></p>
<ul>
<li><p><strong>nodes</strong>: The entities (individual actors, people, or things) to be connected in the network. Synonyms: <strong>vertices</strong> of a graph.</p></li>
<li><p><strong>edges</strong>: The connections (interactions or relationships) between the entities. Synonyms: <strong>links</strong>, <strong>ties</strong>.</p></li>
<li><p><strong>adjacency matrix</strong>: a square matrix in which the column and row names are the nodes of the network. This is a standard data format accepted by many network analysis packages in R. Synonyms: <strong>sociomatrices</strong>. Within the matrix a 1 specifies that there is a link between the nodes, and a 0 indicates no link.</p></li>
<li><p><strong>edge list</strong>: a data frame containing at least two columns: one column of nodes corresponding to the source of a connection and another column of nodes that contains the target of the connection. The nodes in the data are identified by unique IDs.</p></li>
<li><p><strong>Node list</strong>: a data frame with a single column listing the node IDs found in the edge list. You can also add attribute columns to the data frame such as the names of the nodes or grouping variables.</p></li>
<li><p><strong>Weighted network graph</strong>: An edge list can also contain additional columns describing <strong>attributes</strong> of the edges such as a magnitude aspect for an edge. If the edges have a magnitude attribute the graph is considered weighted.</p></li>
<li><p><strong>Directed and undirected network graph</strong>:</p></li>
</ul>
<ol style="list-style-type: lower-roman">
<li><p>If the distinction between source and target is meaningful, the <strong>network is directed</strong>. Directed edges represent an ordering of nodes, like a relationship extending from one nodes to another, where switching the direction would change the structure of the network. The World Wide Web is an example of a directed network because hyperlinks connect one Web page to another, but not necessarily the other way around <span class="citation">(Tyner, Briatte, and Hofmann 2017)</span>.</p></li>
<li><p>If the distinction is not meaningful, the <strong>network is undirected</strong>. Undirected edges are simply links between nodes where order does not matter. Co-authorship networks represent examples of undirected networks, where nodes are authors and they are connected by an edge if they have written a publication together <span class="citation">(Tyner, Briatte, and Hofmann 2017)</span>.</p></li>
</ol>
<p>Another example: When people send e-mail to each other, the distinction between the sender (source) and the recipient (target) is clearly meaningful, therefore the network is directed.</p>
</div>
<div id="install-required-packages" class="section level2">
<h2>Install required packages</h2>
<ul>
<li><code>navdata</code>: contains data sets required for this book</li>
<li><code>tidyverse</code>: for general data manipulation</li>
<li><code>igraph</code>, <code>tidygraph</code> and <code>ggraph</code>: for network visualization</li>
</ul>
<ol style="list-style-type: decimal">
<li>Install the <code>navdata</code> R package:</li>
</ol>
<pre class="r"><code>if(!require(devtools)) install.packages("devtools")
devtools::install_github("kassambara/navdata")</code></pre>
<ol start="2" style="list-style-type: decimal">
<li>Install the remaining packages:</li>
</ol>
<pre class="r"><code>install.packages(
  c("tidyverse", "igraph", "tidygraph", "ggraph")
)</code></pre>
</div>
<div id="data-structure" class="section level2">
<h2>Data structure</h2>
<div id="demo-data-set" class="section level3">
<h3>Demo data set</h3>
<p>We’ll use a fake demo data set containing the number of phone calls between the president of some EU countries.</p>
<pre class="r"><code>library("navdata")
data("phone.call")
head(phone.call, 3)</code></pre>
<pre><code>## # A tibble: 3 x 3
##    source destination n.call
##     <chr>       <chr>  <dbl>
## 1  France     Germany      9
## 2 Belgium      France      4
## 3  France       Spain      3</code></pre>
<div class="success">
<p>
Nodes are countries in the source and destination columns. The values, in the column <code>n.call</code>, will be used as edges weight.
</p>
</div>
<p>To visualize the network graph, we need to create two data frames from the demo data sets:</p>
<ul>
<li><strong>nodes list</strong>: containing nodes labels and other nodes attributes</li>
<li><strong>edges list</strong>: containing the relationship between the nodes. It consists of the edge list and any additional edge attributes.</li>
</ul>
<p>In the following sections, we start by creating nodes and edges lists. Next, we’ll use the different packages to create network graphs.</p>
</div>
<div id="create-nodes-list" class="section level3">
<h3>Create nodes list</h3>
<p>First, load the <code>tidyverse</code> R package for data manipulation:</p>
<pre class="r"><code>library(tidyverse)</code></pre>
<p>Then, compute the following key steps to create nodes list:</p>
<ol style="list-style-type: decimal">
<li>Take the distinct countries from both the “source” and “destination” columns</li>
<li>Change the column name to <code>label</code></li>
<li>Join the information from the two columns together.</li>
</ol>
<pre class="r"><code>#  Get distinct source names
sources <- phone.call %>%
  distinct(source) %>%
  rename(label = source)

# Get distinct destination names
destinations <- phone.call %>%
  distinct(destination) %>%
  rename(label = destination)

# Join the two data to create node
# Add unique ID for each country
nodes <- full_join(sources, destinations, by = "label") 
nodes <- nodes %>%
  mutate(id = 1:nrow(nodes)) %>%
  select(id, everything())

head(nodes, 3)</code></pre>
<pre><code>## # A tibble: 3 x 2
##      id   label
##   <int>   <chr>
## 1     1  France
## 2     2 Belgium
## 3     3 Germany</code></pre>
</div>
<div id="create-edges-list" class="section level3">
<h3>Create edges list</h3>
<p>Key steps:</p>
<ol style="list-style-type: decimal">
<li>Take the phone.call data, which are already in edges list format, showing the connection between nodes. Rename the column “n.call” to “weight”.</li>
<li>Join the node IDs to the edges list data
<ol style="list-style-type: lower-alpha">
<li>Do this for the “source” column and rename the id column that are brought over from nodes. New name: “from”.</li>
<li>Do this for the “destination” column and rename the id column. New name: “to”</li>
<li>Select only the columns “from” and “to” in the edge data. We don’t need to keep the column “source” and “destination” containing the names of countries. These information are already present in the node data.</li>
</ol></li>
</ol>
<pre class="r"><code># Rename the n.call column to weight
phone.call <- phone.call %>%
  rename(weight = n.call)

# (a) Join nodes id for source column
edges <- phone.call %>% 
  left_join(nodes, by = c("source" = "label")) %>% 
  rename(from = id)

# (b) Join nodes id for destination column
edges <- edges %>% 
  left_join(nodes, by = c("destination" = "label")) %>% 
  rename(to = id)

# (c) Select/keep only the columns from and to
edges <- select(edges, from, to, weight)
head(edges, 3)</code></pre>
<pre><code>## # A tibble: 3 x 3
##    from    to weight
##   <int> <int>  <dbl>
## 1     1     3      9
## 2     2     1      4
## 3     1     8      3</code></pre>
</div>
</div>
<div id="tools-and-visualization" class="section level2">
<h2>Tools and visualization</h2>
<p>There are many tools and software to analyse and visualize network graphs. However, for a reproducible and automatized research you need a programming environment such as in R software.</p>
<p>In this section, we review major R packages for reproducible network analysis and visualization.</p>
<p>We’ll introduce how to create static network graphs using <code>igraph</code> <span class="citation">(file. 2017)</span> and <code>tidygraph</code><span class="citation">(Pedersen 2017b)</span> + <code>ggraph</code> <span class="citation">(Pedersen 2017a)</span> packages.</p>
<p>Note that, <code>igraph</code> packages uses the R base plotting system. The <code>ggraph</code> package is based on ggplot2 plotting system, which is highly flexible.</p>
<p>If you are new to network analysis in R, we highly recommend to learn the <code>tidygraph</code> and the <code>ggraph</code> package for the analysis and the visualization, respectively.</p>
<div class="warning">
<p>
Note that, each time that you create a network graph, you need to set the random generator to always have the same layout. For example, you can type type this: <code>set.seed(123)</code>
</p>
</div>
<div id="igraph" class="section level3">
<h3>igraph</h3>
<ol style="list-style-type: decimal">
<li><strong>Create an igraph network object</strong>:</li>
</ol>
<ul>
<li><p>Key R function: <code>graph_from_data_frame()</code>.</p></li>
<li>Key arguments:
<ul>
<li><code>d</code>: edge list</li>
<li><code>vertices</code>: node list</li>
<li><code>directed</code>: can be either TRUE or FALSE depending on whether the data is directed or undirected.</li>
</ul></li>
</ul>
<pre class="r"><code>library(igraph)
net.igraph <- graph_from_data_frame(
  d = edges, vertices = nodes, 
  directed = TRUE
  )</code></pre>
<ol start="2" style="list-style-type: decimal">
<li><strong>Create a network graph with igraph</strong></li>
</ol>
<pre class="r"><code>set.seed(123)
plot(net.igraph, edge.arrow.size = 0.2,
     layout = layout_with_graphopt)</code></pre>
<p><img src="https://www.sthda.com/english/sthda-upload/figures/network-analysis-and-visualization/002-network-visualization-essentials-create-network-graphs-with-igraph-1.png" width="480" /></p>
<div class="warning">
<p>
See the documentation by typing <code>?plot.igraph</code>, for more options to customize the plot.
</p>
</div>
</div>
<div id="tidygraph-and-ggraph" class="section level3">
<h3>tidygraph and ggraph</h3>
<p><code>tidygraph</code> and <code>ggraph</code> are modern R packages for network data manipulation (<code>tidygraph</code>) and visualization (<code>ggraph</code>). They leverage the power of igraph.</p>
<ol style="list-style-type: decimal">
<li><strong>Create a network object using tidygraph</strong>:</li>
</ol>
<ul>
<li>Key function: <code>tbl_graph()</code>.</li>
<li>key arguments: <code>nodes</code>, <code>edges</code> and <code>directed</code>.</li>
</ul>
<pre class="r"><code>library(tidygraph)
net.tidy <- tbl_graph(
  nodes = nodes, edges = edges, directed = TRUE
  )</code></pre>
<ol start="2" style="list-style-type: decimal">
<li><strong>Visualize network using ggraph</strong></li>
</ol>
<p>Key functions:</p>
<ul>
<li><p><code>geom_node_point()</code>: Draws node points.</p></li>
<li><p><code>geom_edge_link()</code>: Draws edge links. To control the width of edge line according to the weight variable, specify the option <code>aes(width = weight)</code>, where, the weight specify the number of phone.call sent along each route. In this case, you can control the maximum and minimum width of the edges, by using the function <code>scale_edge_width()</code> to set the range (minimum and maximum width value). For example: scale_edge_width(range = c(0.2, 2)).</p></li>
<li><p><code>geom_node_text()</code>: Adds text labels for nodes, by specifying the argument <code>aes(label = label)</code>. To avoid text overlapping, indicate the option <code>repel = TRUE</code>.</p></li>
<li><p><code>labs()</code>: Change main titles, axis labels and legend titles.</p></li>
</ul>
<p>Create a classic node-edge diagrams. Possible values for the argument <code>layout</code> include: <code>'star', 'circle', 'gem', 'dh', 'graphopt', 'grid', 'mds', 'randomly', 'fr', 'kk', 'drl', 'lgl'</code>.</p>
<pre class="r"><code>library(ggraph)
ggraph(net.tidy, layout = "graphopt") + 
  geom_node_point() +
  geom_edge_link(aes(width = weight), alpha = 0.8) + 
  scale_edge_width(range = c(0.2, 2)) +
  geom_node_text(aes(label = label), repel = TRUE) +
  labs(edge_width = "phone.call") +
  theme_graph()</code></pre>
<p><img src="https://www.sthda.com/english/sthda-upload/figures/network-analysis-and-visualization/002-network-visualization-essentials-create-network-with-ggraph-1.png" width="480" /></p>
</div>
</div>
<div id="graph-layout" class="section level2">
<h2>Graph layout</h2>
<p>Layout defines the placement of nodes and edges in a given graph structure. There are different types of possible layouts (<a href="https://www.data-imaginist.com/2017/ggraph-introduction-layouts/" class="uri">https://www.data-imaginist.com/2017/ggraph-introduction-layouts/</a>). You should use the layout that suit the best your graph data structure.</p>
<p>In this section, we’ll describe some of the layouts, including:</p>
<ul>
<li><code>linear</code>: Arranges the nodes linearly or circularly in order to make an arc diagram.</li>
<li><code>treemap</code>: Creates a treemap from the graph, that is, a space-filing subdivision of rectangles showing a weighted hierarchy.</li>
</ul>
<div id="arc-diagram-layout" class="section level3">
<h3>Arc diagram layout</h3>
<p>In the following example, we’ll:</p>
<ul>
<li>Layout the nodes linearly (horizontal line) using <code>layout = "linear"</code>.</li>
<li>Create an arc diagram by drawing the edges as arcs</li>
<li>Add only the label names, instead of including node points.</li>
</ul>
<div class="warning">
<p>
In the following arc diagram, the edges above the horizontal line move from left to right, while the edges below the line move from right to left.
</p>
</div>
<pre class="r"><code># Arc diagram
ggraph(net.tidy, layout = "linear") + 
  geom_edge_arc(aes(width = weight), alpha = 0.8) + 
  scale_edge_width(range = c(0.2, 2)) +
  geom_node_text(aes(label = label), repel = TRUE) +
  labs(edge_width = "Number of calls") +
  theme_graph()+
  theme(legend.position = "top") </code></pre>
<p><img src="https://www.sthda.com/english/sthda-upload/figures/network-analysis-and-visualization/002-network-visualization-essentials-ggraph-arc-diagram-network-1.png" width="624" /></p>
<pre class="r"><code># Coord diagram, circular
ggraph(net.tidy, layout = "linear", circular = TRUE) + 
  geom_edge_arc(aes(width = weight), alpha = 0.8) + 
  scale_edge_width(range = c(0.2, 2)) +
  geom_node_text(aes(label = label), repel = TRUE) +
  labs(edge_width = "Number of calls") +
  theme_graph()+
  theme(legend.position = "top")</code></pre>
<p><img src="https://www.sthda.com/english/sthda-upload/figures/network-analysis-and-visualization/002-network-visualization-essentials-ggraph-arc-diagram-network-2.png" width="624" /></p>
</div>
<div id="treemap-layout" class="section level3">
<h3>Treemap layout</h3>
<p>A treemap is a visual method for displaying hierarchical data that uses nested rectangles to represent the branches of a tree diagram. Each rectangles has an area proportional to the amount of data it represents.</p>
<p>To illustrate this layout, we’ll use the <code>france.trade</code> demo data set [ in <code>navdata</code> package]. It contains the trading percentage between France and different countries.</p>
<div id="demo-data-sets" class="section level4">
<h4>Demo data sets</h4>
<pre class="r"><code>data("france.trade")
france.trade</code></pre>
<pre><code>## # A tibble: 251 x 3
##   source destination trade.percentage
##    <chr>       <chr>            <dbl>
## 1 France       Aruba            0.035
## 2 France Afghanistan            0.035
## 3 France      Angola            0.035
## 4 France    Anguilla            0.035
## 5 France     Albania            0.035
## 6 France     Finland            0.035
## # ... with 245 more rows</code></pre>
</div>
<div id="nodes-list" class="section level4">
<h4>Nodes list</h4>
<p>Nodes are the distinct countries in the source and the destination columns.</p>
<ol style="list-style-type: decimal">
<li>Take the distinct countries and create the nodes list:</li>
</ol>
<pre class="r"><code># Distinct countries
countries <- c(
  france.trade$source, france.trade$destination
) %>%
  unique()
# Create nodes list
nodes <- data_frame(
  id = 1:length(countries),
  label = countries
)</code></pre>
<ol start="2" style="list-style-type: decimal">
<li>Bind the trade percentage and turn the NAs into 0:</li>
</ol>
<pre class="r"><code>nodes <- nodes %>%
  left_join(
    france.trade[, c("destination", "trade.percentage")],
    by = c("label" = "destination" )
    ) %>%
  mutate(
    trade.percentage = ifelse(
      is.na(trade.percentage), 0, trade.percentage
      )
  )
head(nodes, 3)</code></pre>
<pre><code>## # A tibble: 3 x 3
##      id       label trade.percentage
##   <int>       <chr>            <dbl>
## 1     1      France            0.000
## 2     2       Aruba            0.035
## 3     3 Afghanistan            0.035</code></pre>
</div>
<div id="edges-list" class="section level4">
<h4>Edges list</h4>
<pre class="r"><code>per_route <- france.trade %>%  
  select(source, destination)

# (a) Join nodes id for source column
edges <- per_route %>% 
  left_join(nodes, by = c("source" = "label")) %>% 
  rename(from = id)

# (b) Join nodes id for destination column
edges <- edges %>% 
  left_join(nodes, by = c("destination" = "label")) %>% 
  rename(to = id)

# (c) Select/keep only the columns from and to
edges <- select(edges, from, to)
head(edges, 3)</code></pre>
<pre><code>## # A tibble: 3 x 2
##    from    to
##   <int> <int>
## 1     1     2
## 2     1     3
## 3     1     4</code></pre>
</div>
<div id="create-the-treemap" class="section level4">
<h4>Create the treemap</h4>
<ol start="3" style="list-style-type: decimal">
<li>Create network object and visualize:</li>
</ol>
<ul>
<li>Network object:</li>
</ul>
<pre class="r"><code>trade.graph <- tbl_graph(
  nodes = nodes, edges = edges, directed = TRUE
  )</code></pre>
<ul>
<li>Visualize. The ggpubr package is required to generate color palette:</li>
</ul>
<pre class="r"><code># Generate colors for each country
require(ggpubr)
cols <- get_palette("Dark2", nrow(france.trade)+1)

# Visualize
set.seed(123)
ggraph(trade.graph, &amp;#39;treemap&amp;#39;, weight = "trade.percentage") + 
    geom_node_tile(aes(fill = label), size = 0.25, color = "white")+
  geom_node_text(
    aes(label = paste(label, trade.percentage, sep = "\n"),
        size = trade.percentage), color = "white"
    )+
  scale_fill_manual(values = cols)+
  scale_size(range = c(0, 6) )+
  theme_void()+
  theme(legend.position = "none")</code></pre>
<p><img src="https://www.sthda.com/english/sthda-upload/figures/network-analysis-and-visualization/002-network-visualization-essentials-treemap-1.png" width="480" /></p>
</div>
<div id="create-a-choropleth-map" class="section level4">
<h4>Create a choropleth map</h4>
<ul>
<li><ol style="list-style-type: decimal">
<li>Take the world map and Color each country according to the trading percentage with France.</li>
</ol></li>
<li><ol start="2" style="list-style-type: decimal">
<li>Draw the map and color France in red</li>
</ol></li>
</ul>
<pre class="r"><code>require("map")
# (1)
world_map <- map_data("world")
france.trade <- france.trade %>%
  left_join(world_map, by = c("destination" = "region")) 
# (2)
ggplot(france.trade, aes(long, lat, group = group))+
  geom_polygon(aes(fill = trade.percentage ), color = "white")+
  geom_polygon(data = subset(world_map, region == "France"),
               fill = "red")+ # draw france in red
  scale_fill_gradientn(colours =c("lightgray", "yellow", "green"))+
  theme_void()</code></pre>
<p><img src="https://www.sthda.com/english/sthda-upload/figures/network-analysis-and-visualization/002-network-visualization-essentials-map-1.png" width="576" /></p>
</div>
</div>
<div id="dendrogram-layout" class="section level3">
<h3>Dendrogram layout</h3>
<p>Dendrogram layout are suited for hierarchical graph visualization, that is graph structures including trees and hierarchies.</p>
<p>In this section, we’ll compute hierarchical clustering using the USArrests data set. The output is visualized as a dendrogram tree.</p>
<ol style="list-style-type: decimal">
<li>compute hierarchical clustering using the <code>USArrests</code> data set;</li>
<li>then convert the result into a tbl_graph.</li>
</ol>
<pre class="r"><code>res.hclust <- scale(USArrests) %>%
  dist() %>% hclust() 
res.tree <- as.dendrogram(res.hclust)</code></pre>
<ol start="3" style="list-style-type: decimal">
<li>Visualize the dendrogram tree. Key function: <code>geom_edge_diagonal()</code> and <code>geom_edge_elbow()</code></li>
</ol>
<pre class="r"><code># Diagonal layout
ggraph(res.tree, layout = "dendrogram") + 
  geom_edge_diagonal() +
  geom_node_text(
    aes(label = label), angle = 90, hjust = 1,
    size = 3
    )+
  ylim(-1.5, NA)+
  theme_minimal()</code></pre>
<p><img src="https://www.sthda.com/english/sthda-upload/figures/network-analysis-and-visualization/002-network-visualization-essentials-dendrogram-hierarchical-graph-visualization-1.png" width="576" /></p>
<pre class="r"><code># Elbow layout
ggraph(res.tree, layout = "dendrogram") + 
  geom_edge_elbow() +
  geom_node_text(
    aes(label = label), angle = 90, hjust = 1,
    size = 3
    )+
  ylim(-1.5, NA)+theme_minimal()</code></pre>
<p><img src="https://www.sthda.com/english/sthda-upload/figures/network-analysis-and-visualization/002-network-visualization-essentials-dendrogram-hierarchical-graph-visualization-2.png" width="576" /></p>
</div>
</div>
<div id="read-more" class="section level2">
<h2>Read more</h2>
<ul>
<li>Introduction to ggraph: <a href="https://www.data-imaginist.com/2017/ggraph-introduction-edges/">Edges</a>, <a href="https://www.data-imaginist.com/2017/ggraph-introduction-nodes/">Nodes</a> and <a href="https://www.data-imaginist.com/2017/ggraph-introduction-layouts/">layouts</a></li>
<li><a href="https://journal.r-project.org/archive/2017/RJ-2017-023/RJ-2017-023.pdf">Network Visualization with ggplot2</a></li>
</ul>
</div>
<div id="references" class="section level2 unnumbered">
<h2>References</h2>
<div id="refs" class="references">
<div id="ref-R-igraph">
<p>file., See AUTHORS. 2017. <em>Igraph: Network Analysis and Visualization</em>. <a href="https://CRAN.R-project.org/package=igraph" class="uri">https://CRAN.R-project.org/package=igraph</a>.</p>
</div>
<div id="ref-R-ggraph">
<p>Pedersen, Thomas Lin. 2017a. <em>Ggraph: An Implementation of Grammar of Graphics for Graphs and Networks</em>. <a href="https://CRAN.R-project.org/package=ggraph" class="uri">https://CRAN.R-project.org/package=ggraph</a>.</p>
</div>
<div id="ref-R-tidygraph">
<p>———. 2017b. <em>Tidygraph: A Tidy Api for Graph Manipulation</em>. <a href="https://CRAN.R-project.org/package=tidygraph" class="uri">https://CRAN.R-project.org/package=tidygraph</a>.</p>
</div>
<div id="ref-tyner2017">
<p>Tyner, Sam, François Briatte, and Heike Hofmann. 2017. “Network Visualization with ggplot2.” <em>The R Journal</em> 9 (1): 27–59. <a href="https://journal.r-project.org/archive/2017/RJ-2017-023/index.html" class="uri">https://journal.r-project.org/archive/2017/RJ-2017-023/index.html</a>.</p>
</div>
</div>
</div>


</div><!--end rdoc-->

<!-- END HTML -->]]></description>
			<pubDate>Tue, 28 Nov 2017 07:56:00 +0100</pubDate>
			
		</item>
		
	</channel>
</rss>
