7.1 The Basics
suppressPackageStartupMessages(library(igraph))
suppressPackageStartupMessages(library(tidygraph))
suppressPackageStartupMessages(library(ggraph))
suppressPackageStartupMessages(library(tidyverse))
Visualization and statistics are the primary tools at our disposable for conveying convincing stories about the structure and dynamics of the networks we study.
Load data and create an igraph object from edgelist
#read data
<- "https://raw.githubusercontent.com/mahoffman/social_network_analysis/master/Data/money_edgelist.csv"
data_url <- read_csv(data_url) money_edgelist
## Rows: 11 Columns: 2
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): Ego, Alter
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
#igraph
<- money_edgelist %>%
moneyNetwork_ig as.matrix() %>%
graph_from_edgelist(directed=TRUE)
#tidygraph
<- as_tbl_graph(moneyNetwork_ig) moneyNetwork_tg
money_edgelist
## # A tibble: 11 × 2
## Ego Alter
## <chr> <chr>
## 1 Greg Maria
## 2 Greg Mark
## 3 Greg Lexi
## 4 Greg Grace
## 5 Greg Nick
## 6 Maria Mark
## 7 Maria Lexi
## 8 Maria Grace
## 9 Maria Nick
## 10 Mark Nick
## 11 Lexi Nick
moneyNetwork_ig
## IGRAPH f4a5967 DN-- 6 11 --
## + attr: name (v/c)
## + edges from f4a5967 (vertex names):
## [1] Greg ->Maria Greg ->Mark Greg ->Lexi Greg ->Grace Greg ->Nick
## [6] Maria->Mark Maria->Lexi Maria->Grace Maria->Nick Mark ->Nick
## [11] Lexi ->Nick
moneyNetwork_tg
## # A tbl_graph: 6 nodes and 11 edges
## #
## # A directed acyclic simple graph with 1 component
## #
## # A tibble: 6 × 1
## name
## <chr>
## 1 Greg
## 2 Maria
## 3 Mark
## 4 Lexi
## 5 Grace
## 6 Nick
## #
## # A tibble: 11 × 2
## from to
## <int> <int>
## 1 1 2
## 2 1 3
## 3 1 4
## # ℹ 8 more rows
Basic igraph plot
set.seed(1992)
plot(moneyNetwork_ig)
set.seed(1992)
plot(moneyNetwork_ig)
Save “nice” layout
<- layout_nicely(moneyNetwork_ig) nt_layout
Set the layout
plot(moneyNetwork_ig, layout = nt_layout)
Basic ggraph plot
ggraph(moneyNetwork_tg, layout = nt_layout) +
geom_edge_link() +
geom_node_point()
## Warning: Using the `size` aesthetic in this geom was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` in the `default_aes` field and elsewhere instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
7.1.1 Nodes
We can control the size of the nodes
plot(moneyNetwork_ig, layout = nt_layout, vertex.size = 50)
plot(moneyNetwork_ig, layout = nt_layout, vertex.size = 10)
ggraph(moneyNetwork_tg, layout = nt_layout) +
geom_edge_link() +
geom_node_point(size = 5)
ggraph(moneyNetwork_tg, layout = nt_layout) +
geom_edge_link() +
geom_node_point(size = 10)
We can also control the color of the nodes and their frames
plot(moneyNetwork_ig,
layout = nt_layout,
vertex.size = 10,
vertex.color = "tomato",
vertex.frame.color = NA #remove the frames of the nodes
)
ggraph(moneyNetwork_tg, layout = nt_layout) +
geom_edge_link() +
geom_node_point(color = "tomato", size = 5)
Adjust label size with vertex.label.cex. We can adjust the color with vertex.label.color
plot(moneyNetwork_ig,
layout = nt_layout,
vertex.size = 10,
vertex.color = "tomato",
vertex.frame.color = NA,
vertex.label.cex = .7, #adjust label size
vertex.label.color = "black" #adjust label color
)
ggraph(moneyNetwork_tg, layout = nt_layout) +
geom_edge_link() +
geom_node_point(color = "tomato", size = 5)+
geom_node_text(aes(label = name))
Alternatively, if we want to get rid of the labels, we can just set vertex.label to NA.
plot(moneyNetwork_ig,
layout = nt_layout,
vertex.size = 10,
vertex.color = "tomato",
vertex.frame.color = NA,
vertex.label = NA#remove label
)