This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.
# Load packages
library(readr)
library(igraph)
library(dplyr)
library(ggraph)
# Load data
drugnet_edges <- read_csv("DRUGNET_edgelist.csv", show_col_types = FALSE)
# Build directed graph
g <- graph_from_data_frame(drugnet_edges, directed = TRUE)
# Convert to undirected
g_u <- as.undirected(g, mode = "collapse")
# Extract giant component
comp <- components(g_u)
giant_id <- which.max(comp$csize)
keep <- V(g_u)[comp$membership == giant_id]
g_main <- induced_subgraph(g_u, keep)
# Centrality measures
deg <- degree(g_main)
bet <- betweenness(g_main, directed = FALSE, normalized = TRUE)
clo <- closeness(g_main, normalized = TRUE)
# Top nodes
top_deg <- sort(deg, decreasing = TRUE)[1:5]
top_bet <- sort(bet, decreasing = TRUE)[1:5]
top_deg
## 50 30 64 38 55
## 15 11 11 10 7
top_bet
## 83 98 130 50 165
## 0.3668474 0.3556685 0.3267437 0.2750736 0.1944822
# Community detection
comm <- cluster_louvain(g_main)
V(g_main)$cluster <- factor(membership(comm))
V(g_main)$degree <- degree(g_main)
# Centrality table (sorted by betweenness)
centrality_tbl <- tibble(
node = names(bet),
betweenness = as.numeric(bet),
closeness = as.numeric(clo)
) %>%
arrange(desc(betweenness))
centrality_tbl
## # A tibble: 193 × 3
## node betweenness closeness
## <chr> <dbl> <dbl>
## 1 83 0.367 0.215
## 2 98 0.356 0.204
## 3 130 0.327 0.194
## 4 50 0.275 0.214
## 5 165 0.194 0.210
## 6 38 0.177 0.176
## 7 8 0.164 0.205
## 8 64 0.160 0.206
## 9 31 0.149 0.157
## 10 220 0.126 0.187
## # ℹ 183 more rows
# Centrality table (sorted by closeness)
centrality_tbl_closeness <- tibble(
node = names(clo),
betweenness = as.numeric(bet),
closeness = as.numeric(clo)
) %>%
arrange(desc(closeness))
centrality_tbl_closeness
## # A tibble: 193 × 3
## node betweenness closeness
## <chr> <dbl> <dbl>
## 1 83 0.367 0.215
## 2 50 0.275 0.214
## 3 165 0.194 0.210
## 4 64 0.160 0.206
## 5 8 0.164 0.205
## 6 98 0.356 0.204
## 7 55 0.0903 0.194
## 8 68 0.0828 0.194
## 9 130 0.327 0.194
## 10 30 0.120 0.193
## # ℹ 183 more rows
# Identify top 10 nodes
top_bet_nodes <- names(sort(bet, decreasing = TRUE)[1:10])
top_clo_nodes <- names(sort(clo, decreasing = TRUE)[1:10])
V(g_main)$role <- case_when(
V(g_main)$name %in% top_bet_nodes & V(g_main)$name %in% top_clo_nodes ~ "Both",
V(g_main)$name %in% top_bet_nodes ~ "Betweenness",
V(g_main)$name %in% top_clo_nodes ~ "Closeness",
TRUE ~ "Neither"
)
V(g_main)$role <- factor(
V(g_main)$role,
levels = c("Neither","Betweenness","Closeness","Both")
)
# Final network plot
set.seed(100)
ggraph(g_main, layout = "fr", niter = 4000) +
geom_edge_link(alpha = 0.10) +
geom_node_point(aes(size = degree, color = cluster, shape = role), stroke = 1) +
geom_node_text(
data = function(x) x %>% dplyr::filter(role != "Neither"),
aes(label = name),
repel = TRUE,
size = 3
) +
scale_shape_manual(values = c(
"Neither" = 16,
"Betweenness" = 17,
"Closeness" = 15,
"Both" = 8
)) +
theme_void()