4.4 Loading Packages

  • Packages are collections of R functions, data, and compiled code.

  • igraph is collection of network analysis tools with the emphasis on efficiency, portability and ease of use

install.packages("ggraph",repos = "http://cran.us.r-project.org")

The library function tells R to add the package to the current R session

suppressPackageStartupMessages(library(igraph))
suppressPackageStartupMessages(library(ggraph))
suppressPackageStartupMessages(library(tidygraph))
suppressPackageStartupMessages(library(tidyverse))
matrix_data2 %>%
  graph.adjacency(mode = "undirected")%>%
  plot()

tbl_links <- matrix_data2 %>%
  graph.adjacency()%>%
  get.edgelist() %>%
  as_tibble() %>%
  dplyr::rename(from = "V1", to = "V2")
## Warning: The `x` argument of `as_tibble.matrix()` must have unique column names if
## `.name_repair` is omitted as of tibble 2.0.0.
## ℹ Using compatibility `.name_repair`.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
tbl_nodes <- data.frame(name = tbl_links %>% unlist %>% unique())%>%
  as_tibble()

graph <- graph_from_data_frame(tbl_links, tbl_nodes, directed = FALSE)

graph %>%
  ggraph()+
  geom_edge_link() + 
  geom_node_point()+ 
  geom_node_text(aes(label = name), repel = TRUE, size = 8)
## Using "stress" as default layout
## 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.