7.4 Adding attributes to a network object
Load node attributes (metadata)
<- "https://raw.githubusercontent.com/mahoffman/social_network_analysis/master/Data/attribute_df.csv"
data_url2 <- read_csv(data_url2) attributes
## Rows: 6 Columns: 4
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): Name, Gender, Role
## dbl (1): Age
##
## ℹ 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.
head(attributes)
## # A tibble: 6 × 4
## Name Age Gender Role
## <chr> <dbl> <chr> <chr>
## 1 Greg 53 Male Father
## 2 Maria 52 Female Mother
## 3 Mark 25 Male Son
## 4 Lexi 23 Female Daughter
## 5 Grace 19 Female Daughter
## 6 Nick 14 Male Son
add attributes to the igraph object
<- graph_from_data_frame(money_edgelist,
moneyNetwork_ig directed = T,
vertices = attributes)
moneyNetwork_ig
## IGRAPH c60f3e3 DN-- 6 11 --
## + attr: name (v/c), Age (v/n), Gender (v/c), Role (v/c)
## + edges from c60f3e3 (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
V(moneyNetwork_ig)$Gender
## [1] "Male" "Female" "Male" "Female" "Female" "Male"
and the ggraph object
<- tbl_graph(edges = money_edgelist,
moneyNetwork_tg directed = T,
nodes = attributes)
moneyNetwork_tg
## # A tbl_graph: 6 nodes and 11 edges
## #
## # A directed acyclic simple graph with 1 component
## #
## # A tibble: 6 × 4
## Name Age Gender Role
## <chr> <dbl> <chr> <chr>
## 1 Greg 53 Male Father
## 2 Maria 52 Female Mother
## 3 Mark 25 Male Son
## 4 Lexi 23 Female Daughter
## 5 Grace 19 Female Daughter
## 6 Nick 14 Male Son
## #
## # A tibble: 11 × 2
## from to
## <int> <int>
## 1 1 2
## 2 1 3
## 3 1 4
## # ℹ 8 more rows
Let’s set a layout for the network
<- layout.kamada.kawai(moneyNetwork_ig) kk_layout