<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/another-game-of-thrones-network-analysis-this-time-with-tidygraph-and-ggraph/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://shirinsplayground.netlify.com/2018/03/got_network/"> Shirin's playgRound</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>A while back, I did <a rel="nofollow" href="https://shiring.github.io/networks/2017/05/15/got_final" target="_blank">an analysis of the family network of major characters from the A Song of Ice and Fire books and the Game of Thrones TV show</a>. In that analysis I found out that House Stark (specifically Ned and Sansa) and House Lannister (especially Tyrion) are the most important family connections in Game of Thrones; they also connect many of the story lines and are central parts of the narrative.</p>
<p>I that old post, I used <code>igraph</code> for plotting and calculating network metrics.</p>
<p>But there are two packages that integrate network analysis much more nicely with the <code>tidyverse</code>: <code>tidygraph</code> and <code>ggraph</code>. These, I am going to show how to use for analyzing yet another network of characters from <strong>A Song of Ice and Fire</strong> / <strong>Game of Thrones</strong> (to be correct, this new network here is strictly based on the <strong>A Song of Ice and Fire</strong> books, NOT on the TV show).</p>
<div>
<h2>What can network analysis tell us?</h2>
<p>Network analysis can e.g. be used to explore relationships in social or professional networks. In such cases, we would typically ask questions like:</p>
<ul>
<li>How many connections does each person have?</li>
<li>Who is the most connected (i.e. influential or “important”) person?</li>
<li>Are there clusters of tightly connected people?</li>
<li>Are there a few key players that connect clusters of people?</li>
<li>etc.</li>
</ul>
<p>These answers can give us a lot of information about the patterns of how people interact.</p>
<p>So, how do we find out who the most important characters are in this network? We consider a character “important” if he has connections to many other characters. There are a few network properties, that tell us more about this, like node centrality and which characters are key-players in the books.</p>
<p><strong>A word of caution before you read on: BEWARE of SPOILERS for all books!</strong></p>
<div>
<img alt="A Song of Ice and Fire character network across all five books; find out how I made it by following the code below…" src="https://i1.wp.com/shirinsplayground.netlify.com/post/2018-03-03_got_network_files/figure-html/unnamed-chunk-18-1.png?w=456&ssl=1" data-recalc-dims="1">
<p>A Song of Ice and Fire character network across all five books; find out how I made it by following the code below…</p>
</div>
<pre><code>library(readr)     # fast reading of csv files
library(tidyverse) # tidy data analysis
library(tidygraph) # tidy graph analysis
library(ggraph)    # for plotting</code></pre>
</div>
<div>
<h2>The Data</h2>
<p>I obtained the following data by cloning <a rel="nofollow" href="https://github.com/mathbeveridge/asoiaf" target="_blank">this Github repository</a> from Andrew Beveridge:</p>
<blockquote>
<p>Character Interaction Networks for George R. R. Martin’s “A Song of Ice and Fire” saga These networks were created by connecting two characters whenever their names (or nicknames) appeared within 15 words of one another in one of the books in “A Song of Ice and Fire.” The edge weight corresponds to the number of interactions. You can use this data to explore the dynamics of the Seven Kingdoms using network science techniques. For example, community detection finds coherent plotlines. Centrality measures uncover the multiple ways in which characters play important roles in the saga.</p>
</blockquote>
<p>Andrew already did a great job analyzing these character networks and you can read all his conclusions on his site <a rel="nofollow" href="https://networkofthrones.wordpress.com/" target="_blank">https://networkofthrones.wordpress.com</a>. Here, I don’t aim to replicate his analyses but I want to show how you could do this or similar analyses with <code>tidygraph</code> and <code>ggraph</code>. Thus, I am also not going to use all of his node and edge files.</p>
<pre><code>path &lt;- &quot;/Users/shiringlander/Documents/Github/Data/asoiaf/data/&quot;
files &lt;- list.files(path = path, full.names = TRUE)
files</code></pre>
<pre><code>##  [1] &quot;/Users/shiringlander/Documents/Github/Data/asoiaf/data//asoiaf-all-edges.csv&quot;   
##  [2] &quot;/Users/shiringlander/Documents/Github/Data/asoiaf/data//asoiaf-all-nodes.csv&quot;   
##  [3] &quot;/Users/shiringlander/Documents/Github/Data/asoiaf/data//asoiaf-book1-edges.csv&quot; 
##  [4] &quot;/Users/shiringlander/Documents/Github/Data/asoiaf/data//asoiaf-book1-nodes.csv&quot; 
##  [5] &quot;/Users/shiringlander/Documents/Github/Data/asoiaf/data//asoiaf-book2-edges.csv&quot; 
##  [6] &quot;/Users/shiringlander/Documents/Github/Data/asoiaf/data//asoiaf-book2-nodes.csv&quot; 
##  [7] &quot;/Users/shiringlander/Documents/Github/Data/asoiaf/data//asoiaf-book3-edges.csv&quot; 
##  [8] &quot;/Users/shiringlander/Documents/Github/Data/asoiaf/data//asoiaf-book3-nodes.csv&quot; 
##  [9] &quot;/Users/shiringlander/Documents/Github/Data/asoiaf/data//asoiaf-book4-edges.csv&quot; 
## [10] &quot;/Users/shiringlander/Documents/Github/Data/asoiaf/data//asoiaf-book4-nodes.csv&quot; 
## [11] &quot;/Users/shiringlander/Documents/Github/Data/asoiaf/data//asoiaf-book45-edges.csv&quot;
## [12] &quot;/Users/shiringlander/Documents/Github/Data/asoiaf/data//asoiaf-book45-nodes.csv&quot;
## [13] &quot;/Users/shiringlander/Documents/Github/Data/asoiaf/data//asoiaf-book5-edges.csv&quot; 
## [14] &quot;/Users/shiringlander/Documents/Github/Data/asoiaf/data//asoiaf-book5-nodes.csv&quot;</code></pre>
</div>
<div>
<h2>Characters across all books</h2>
<p>The first data set I am going to use are the character interactions in all five books. I am not using the node files here, because I find the edge names sufficient for this demonstration. If you wanted to have nice name labels, you could use the node files.</p>
<pre><code>cooc_all_edges &lt;- read_csv(files[1])</code></pre>
<p>Because there are so many characters in the books, many of them minor, I am subsetting the data to the 100 characters with the most interactions across all books.The edges are undirected, therefore there are no redundant Source-Target combination; because of this, I gathered Source and Target data before summing up the weights.</p>
<pre><code>main_ch &lt;- cooc_all_edges %>%
  select(-Type) %>%
  gather(x, name, Source:Target) %>%
  group_by(name) %>%
  summarise(sum_weight = sum(weight)) %>%
  ungroup()

main_ch_l &lt;- main_ch %>%
  arrange(desc(sum_weight)) %>%
  top_n(100, sum_weight)
main_ch_l</code></pre>
<pre><code>## # A tibble: 100 x 2
##    name               sum_weight
##    </code></pre></div>