Visualizing twitter networks

Analyzing Social Media Data in R

Sowmya Vivek

Data Science Coach

Lesson overview

  • Plot a network with default parameters
  • Apply formatting attributes to improve the readability
  • Use network centrality and attributes to enhance the plot
Analyzing Social Media Data in R

View a retweet network

# 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
Analyzing Social Media Data in R

Create the base network plot

# Create the base network plot
set.seed(1234)  
plot.igraph(nw_rtweet)
Analyzing Social Media Data in R

View the base network plot

Base network plot

Analyzing Social Media Data in R

Format the plot

# 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")
Analyzing Social Media Data in R

View the formatted plot

Formatted network plot

Analyzing Social Media Data in R

Set vertex size based on the out-degree

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

Out-degree values

vert_size <- (deg_out * 2) + 10
Analyzing Social Media Data in R

Assign vert_size to the vertex size attribute

# 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")
Analyzing Social Media Data in R

View plot with new attributes

Plot with vert size attribute

Analyzing Social Media Data in R

Adding network attributes

  • Users who retweet most and have a high follower count add more value
  • Network plot of users who retweet more and have a high follower count
  • Add follower count as a network attribute
Analyzing Social Media Data in R

Follower count of network users

# Import the followers count data frame
followers <- readRDS("follower_count.rds")
Analyzing Social Media Data in R

View the followers data frame

# 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
Analyzing Social Media Data in R

Follower count of network users

# Categorize high and low follower count
followers$follow <- ifelse(followers$followers_count > 500, "1", "0")
Analyzing Social Media Data in R

View the followers data frame

# 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
Analyzing Social Media Data in R

Assign network attributes

# Assign external network attributes to retweet network
V(nw_rtweet)$followers <- followers$follow
Analyzing Social Media Data in R

View vertex attributes

# View the vertex attributes
vertex_attr(nw_rtweet)

Vertex attributes

Analyzing Social Media Data in R

Changing vertex colors

# 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")
Analyzing Social Media Data in R

View plot formatted with vertex attributes

Plot based on vertex attributes

Analyzing Social Media Data in R

Let's practice!

Analyzing Social Media Data in R

Preparing Video For Download...