可视化 Twitter 网络

用 R 分析社交媒体数据

Sowmya Vivek

Data Science Coach

课程概览

  • 使用默认参数绘制网络
  • 应用格式属性以提升可读性
  • 用中心性和属性强化图形
用 R 分析社交媒体数据

查看转推网络

# View the retweet network
print.igraph(nw_rtweet)
IGRAPH e7e618c DN-- 21 39 -- 
+ attr: name (v/c), followers (v/c)
+ edges from e7e618c (vertex names):
 [1] w3daily    ->RealFirstBuzz   w3daily    ->RealFirstBuzz  
 [3] w3daily    ->Giasaysthat     w3daily    ->RealFirstBuzz  
 [5] VanesEtim  ->PotionVanity    VanesEtim  ->DAVIDxCGN      
 [7] VanesEtim  ->PotionVanity    VanesEtim  ->Avinash_galaxy 
 [9] VanesEtim  ->PotionVanity    VanesEtim  ->BklynLeague    
[11] RedNileShop->Macaw_Blink     RedNileShop->leuqimcouture  
[13] RedNileShop->StoreDiv        RedNileShop->PotionVanity   
[15] RedNileShop->SCISSORSAPPAREL RedNileShop->Themoon12961958
+ ... omitted several edges
用 R 分析社交媒体数据

创建基础网络图

# Create the base network plot
set.seed(1234)  
plot.igraph(nw_rtweet)
用 R 分析社交媒体数据

查看基础网络图

基础网络图

用 R 分析社交媒体数据

格式化该图

# Format the network plot with attributes
set.seed(1234)
plot(nw_rtweet, asp = 9/16, 
     vertex.size = 10,
     vertex.color = "lightblue",
     edge.arrow.size = 0.5,
     edge.color = "black",
     vertex.label.cex = 0.9,
     vertex.label.color = "black")
用 R 分析社交媒体数据

查看格式化后的图

格式化的网络图

用 R 分析社交媒体数据

按出度设置顶点大小

# Create a variable for out-degree
deg_out <- degree(nw_rtweet, mode = c("out"))
deg_out

出度值

vert_size <- (deg_out * 2) + 10
用 R 分析社交媒体数据

将 vert_size 赋给顶点大小属性

# Assign vert_size to vertex size attribute and plot network
set.seed(1234)
plot(nw_rtweet, asp = 9/16, 
     vertex.size = vert_size,
     vertex.color = "lightblue", 
     edge.arrow.size = 0.5,
     edge.color = "black",
          vertex.label.cex = 1.2,
     vertex.label.color = "black")
用 R 分析社交媒体数据

查看带新属性的图

带顶点大小属性的图

用 R 分析社交媒体数据

添加网络属性

  • 最常转推且粉丝多的用户价值更高
  • 这些用户的网络图
  • 将粉丝数添加为网络属性
用 R 分析社交媒体数据

网络用户的粉丝数

# Import the followers count data frame
followers <- readRDS("follower_count.rds")
用 R 分析社交媒体数据

查看粉丝数据框

# View the follower count
head(followers)
screen_name    followers_count
<fct>             <dbl>
adyo312            58        
AllesUndNix_       18        
Avinash_galaxy     1536        
BklynLeague        40        
DAVIDxCGN          267        
Giasaysthat        9139
用 R 分析社交媒体数据

网络用户的粉丝数

# Categorize high and low follower count
followers$follow <- ifelse(followers$followers_count > 500, "1", "0")
用 R 分析社交媒体数据

查看粉丝数据框

# View the data frame with the new column
head(followers)
screen_name    followers_count    follow
<fct>              <dbl>          <chr>
adyo312              58              0    
AllesUndNix_         18              0    
Avinash_galaxy     1536              1    
BklynLeague          40              0    
DAVIDxCGN           267              0    
Giasaysthat        9139              1
用 R 分析社交媒体数据

分配网络属性

# Assign external network attributes to retweet network
V(nw_rtweet)$followers <- followers$follow
用 R 分析社交媒体数据

查看顶点属性

# View the vertex attributes
vertex_attr(nw_rtweet)

顶点属性

用 R 分析社交媒体数据

更改顶点颜色

# Set the vertex colors for the plot
sub_color <- c("lightgreen", "tomato")
set.seed(1234)
plot(nw_rtweet, asp = 9/16,
     vertex.size = vert_size,
     edge.arrow.size = 0.5,
     vertex.label.cex = 1.3,
     vertex.color = sub_color[as.factor(vertex_attr(nw_rtweet, "followers"))],
     vertex.label.color = "black",
     vertex.frame.color = "grey")
用 R 分析社交媒体数据

查看按顶点属性格式化的图

基于顶点属性的图

用 R 分析社交媒体数据

让我们来练习!

用 R 分析社交媒体数据

Preparing Video For Download...