소셜 미디어 텍스트 네트워크 분석 (R)
중급
정성 분석
social
r
난이도: 중급 — 실제 연구에 바로 적용 가능한 수준입니다.
개요
소셜 미디어 게시물에서 단어 간 동시 출현(co-occurrence) 네트워크를 구축하고, 커뮤니티 탐지 알고리즘으로 담론 주제를 식별한다. R의 tidytext와 igraph 패키지를 사용한다.
분석 절차
1단계: 토큰화
unnest_tokens()로 텍스트를 단어 단위로 분할하고, 불용어(stop words)를 제거한다.
2단계: 동시 출현 계산
pairwise_count()로 같은 게시물 내에서 함께 등장하는 단어 쌍의 빈도를 계산한다.
3단계: 네트워크 생성
igraph로 네트워크 객체를 생성하고, 노드는 단어, 엣지는 동시 출현 빈도다.
4단계: 커뮤니티 탐지
Louvain 알고리즘으로 밀접하게 연결된 단어 그룹(담론 주제)을 식별한다.
5단계: 시각화
ggraph로 네트워크를 시각화한다. 커뮤니티별로 색상을 구분하여 담론 구조를 확인한다.
코드 예제
library(tidyverse)
library(tidytext)
library(igraph)
library(ggraph)
# 텍스트 전처리
tokens <- df %>%
unnest_tokens(word, text) %>%
anti_join(stop_words)
# 동시 출현 네트워크 생성
cooccurrence <- tokens %>%
group_by(id) %>%
pairwise_count(word, id, sort = TRUE, upper = FALSE)
# 네트워크 객체 생성
network <- graph_from_data_frame(cooccurrence %>% filter(n > 5))
# 커뮤니티 탐지
community <- cluster_louvain(network)
# 시각화
ggraph(network, layout = "fr") +
geom_edge_link(aes(width = n), alpha = 0.3) +
geom_node_point(aes(color = membership(community)), size = 5) +
geom_node_text(aes(label = name), repel = TRUE) +
theme_void()
참고문헌
Silge, J., & Robinson, D. (2017). Text Mining with R. O’Reilly Media.
코드 예제
R
library(tidyverse) library(tidytext) library(igraph) library(ggraph) # 텍스트 전처리 tokens % unnest_tokens(word, text) %>% anti_join(stop_words) # 동시 출현 네트워크 생성 cooccurrence % group_by(id) %>% pairwise_count(word, id, sort = TRUE, upper = FALSE) # 네트워크 객체 생성 network % filter(n > 5)) # 커뮤니티 탐지 community <- cluster_louvain(network) # 시각화 ggraph(network, layout = "fr") + geom_edge_link(aes(width = n), alpha = 0.3) + geom_node_point(aes(color = membership(community)), size = 5) + geom_node_text(aes(label = name), repel = TRUE) + theme_void()
참고문헌
Silge, J., & Robinson, D. (2017). Text Mining with R. O'Reilly Media.