네트워크 군집화(Clustering)하기 :: R을 이용한 사회 연결망 분석 - mindscale
Skip to content

네트워크 군집화(Clustering)하기

library(igraph)

간단한 그래프

edge_list <- c(1,2,
               1,3,
               1,5,
               2,3,
               3,1,
               3,4,
               5,1)

g <- graph(edges = edge_list, n = 5)
g

간단한 그래프 군집화

cg <- cluster_edge_betweenness(g)
plot(cg, g)

911 테러리스트 데이터 불러오기

terror_data <- read.table('911_terrorist_network.txt',
                          header = F, encoding = 'UTF-8', sep = ',', stringsAsFactors = F)
colnames(terror_data) <- c('from', 'to', 'edge_str', 'verified')
terror_data <- terror_data[!duplicated(terror_data[,1:2]),]
head(terror_data)

911 테러리스트 그래프 그리기

ter_g <- graph.data.frame(terror_data, directed = T)
E(ter_g)$weight <- I(terror_data[,3])

minC <- rep(-Inf, vcount(ter_g))
maxC <- rep(Inf, vcount(ter_g))
minC[0] <- maxC[0] <- 0

set.seed(1234)
co <- layout_with_fr(ter_g, minx = minC, miny = minC, maxx = maxC, maxy = maxC)

png(file = 'beautiful_plot.png', height = 1200, width = 1600)
plot(ter_g, vertex.size = V(ter_g)$size,
     layout = co,
     main = 'Terrorist Network',
     rescale = F,
     xlim = range(co[,1]), ylim = range(co[,2]),
     vertex.frame.color = 'white',
     vertex.label.color = 'black',
     edge.arrow.size = 0.3)
dev.off()

911 테러리스트 그래프 군집화 (Edge Betweenness Centrality 사용)

ter_cg <- cluster_edge_betweenness(ter_g)

png(file = 'edge_betweenness_plot.png', height = 1200, width = 1600)
plot(ter_cg, ter_g, vertex.size = V(ter_g)$size,
     layout = co,
     main = 'Terrorist Network',
     rescale = F,
     xlim = range(co[,1]), ylim = range(co[,2]),
     vertex.frame.color = 'white',
     vertex.label.color = 'black',
     edge.arrow.size = 0.3)
dev.off()

911 테러리스트 그래프 군집화 (Walktrap 사용)

ter_cg <- cluster_walktrap(ter_g)

png(file = 'walktrap_plot.png', height = 1200, width = 1600)
plot(ter_cg, ter_g, vertex.size = V(ter_g)$size,
     layout = co,
     main = 'Terrorist Network',
     rescale = F,
     xlim = range(co[,1]), ylim = range(co[,2]),
     vertex.frame.color = 'white',
     vertex.label.color = 'black',
     edge.arrow.size = 0.3)
dev.off()